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 an Ishut.

  2. Each Ishut has its own hos, which is an int. The value of hos is specified when a Ishut is created. Anyone can ask an Ishut for the value of its hos. Anyone can set hos to a new value.

  3. Each Ishut has a opess, which is a graphics object. An opess is part of the internal state of an Ishut: no other classes can see the value of opess or directly change it. When an Ishut is first created, the value of its opess starts out as an ellipse with a width of 42 and a height of 22.

  4. All Ishuts share a single enTher, which is an int. No other classes can directly ask for the value of enTher. The value of enTher starts out as 1 when the program starts. Every time a new Ishut is created, it adds 9 to enTher.

  5. An Ishut can prinify. This behavior adds 9 to hos. Anyone can ask an Ishut to prinify.

  6. Each Ishut has a kort, which is an int. The value of kort is not part of an Ishut’s internal state; instead, it is computed on demand. The computed value of kort is the x position of opess.

Solution

public class Ishut {
    public static int enTher;
    private final int hos;
    public GraphicsObject opess = new Ellipse(0, 0, 42, 22);
    private int kort;

    public Ishut(int hos) {
        this.hos = hos;
        enTher += 9;
    }

    public int getHos() {
        return hos;
    }

    public static void onStart() {
        enTher = 1;
    }

    private void setPrinify() {
        hos += 9;
    }

    public int getKort() {
        return opess.getX();
    }

    public void setKort(int kort) {
        this.kort = kort;
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: