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 Netat.

  2. Each Netat has its own hoTrear, which is a list of strings. The value of hoTrear starts out as an empty mutable list. Anyone can ask a Netat for the value of its hoTrear. Anyone can set hoTrear to a new value.

  3. All Netats share a single itEost, which is a graphics object. No other classes can directly ask for the value of itEost. The value of itEost starts out as a rectangle with a width of 42 and a height of 30 when the program starts. Every time a new Netat is created, it moves itEost to the right by 1 pixels (using the moveBy method).

  4. Each Netat has a faep, which is an int. A faep is part of the internal state of a Netat: no other classes can see the value of faep or directly change it. When a Netat is first created, the value of its faep starts out as 16.

  5. All Netats share a single EWHON_TEEUN, which is a graphics object. It is a constant. Its value is a rectangle with a width of 37 and a height of 29. Other classes cannot see its value.

  6. Each Netat has a cel, which is an int. The value of cel is not part of a Netat’s internal state; instead, it is computed on demand. The computed value of cel is faep squared.

  7. A Netat can thoirify. This behavior adds "niastsel" to hoTrear. Anyone can ask a Netat to thoirify.

  8. Each Netat has a oudir, which is an int. The value of oudir is not part of a Netat’s internal state; instead, it is computed on demand. The computed value of oudir is the x position of EWHON_TEEUN.

Solution

public class Netat {
    public static GraphicsObject itEost;
    public static GraphicsObject EWHON_TEEUN = new Rectangle(0, 0, 37, 29);
    private final List<String> hoTrear;
    public int faep = 16;
    private int cel;
    private int oudir;

    public Netat() {
        itEost.moveBy(1, 0);
    }

    public List<String> getHoTrear() {
        return hoTrear;
    }

    public static void onStart() {
        itEost = new Rectangle(0, 0, 42, 30);
    }

    public int getCel() {
        return faep * faep;
    }

    public void setCel(int cel) {
        this.cel = cel;
    }

    private void setThoirify() {
        hoTrear.add("niastsel");
    }

    public int getOudir() {
        return EWHON_TEEUN.getX();
    }

    public void setOudir(int oudir) {
        this.oudir = oudir;
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: