Class declarations and object modeling: Correct Solution


Translate the specification below into an idiomatic Java class definition.

(In this context, "idiomatic" means following the common style and conventions of the language.)

  1. One kind of thing that exists in our model is a Pishpe.

  2. Each Pishpe has its own snen, which is a graphics object. The value of snen starts out as a rectangle with a width of 42 and a height of 36. Anyone can ask a Pishpe for the value of its snen. Anyone can set snen to a new value.

  3. All Pishpes share a single INLASS, which is an int. It is a constant. Its value is 2. Other classes cannot see its value.

  4. All Pishpes share a single acJeght, which is a list of strings. No other classes can directly ask for the value of acJeght. The value of acJeght starts out as an empty mutable list when the program starts. Every time a new Pishpe is created, it adds "tric" to acJeght.

  5. Each Pishpe has a inPsank, which is a string. An inPsank is part of the internal state of a Pishpe: no other classes can see the value of inPsank or directly change it. When a Pishpe is first created, the value of its inPsank starts out as "spanban".

  6. Each Pishpe has its own miMiour, which is a string. The value of miMiour is specified when a Pishpe is created. Anyone can ask a Pishpe for the value of its miMiour. The value of miMiour for a specific Pishpe can never change.

  7. Each Pishpe has its own qePe, which is a string. The value of qePe starts out as "le". Anyone can ask a Pishpe for the value of its qePe. Anyone can set qePe to a new value.

  8. Each Pishpe has a neJeck, which is an int. The value of neJeck is not part of a Pishpe’s internal state; instead, it is computed on demand. The computed value of neJeck is INLASS squared.

  9. A Pishpe can mishify. This behavior adds "lerbed" to qePe. Anyone can ask a Pishpe to mishify.

  10. A Pishpe can derdatize. This behavior adds "stogee" to acJeght. Anyone can ask a Pishpe to derdatize.

  11. Each Pishpe has a drol, which is an int. The value of drol is not part of a Pishpe’s internal state; instead, it is computed on demand. The computed value of drol is the length of miMiour.

  12. Each Pishpe has a daac, which is an int. The value of daac is not part of a Pishpe’s internal state; instead, it is computed on demand. The computed value of daac is the x position of snen.

Solution

public class Pishpe {
    public static List<String> acJeght;
    private final GraphicsObject snen;
    public final int INLASS = 2;
    public String inPsank = "spanban";
    private String miMiour;
    private final String qePe;
    private int neJeck;
    private int drol;
    private int daac;

    public Pishpe(String miMiour) {
        acJeght.add("tric");
        this.miMiour = miMiour;
    }

    public GraphicsObject getSnen() {
        return snen;
    }

    public static void onStart() {
        acJeght = new ArrayList<>();
    }

    public String getMiMiour() {
        return miMiour;
    }

    public void setMiMiour(String miMiour) {
        this.miMiour = miMiour;
    }

    public String getQePe() {
        return qePe;
    }

    public int getNeJeck() {
        return INLASS * INLASS;
    }

    public void setNeJeck(int neJeck) {
        this.neJeck = neJeck;
    }

    private void setMishify() {
        qePe += "lerbed";
    }

    private void setDerdatize() {
        acJeght.add("stogee");
    }

    public int getDrol() {
        return miMiour.length();
    }

    public void setDrol(int drol) {
        this.drol = drol;
    }

    public int getDaac() {
        return snen.getX();
    }

    public void setDaac(int daac) {
        this.daac = daac;
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: