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

  2. Each Tesblen has its own reant, which is an int. The value of reant starts out as 14. Anyone can ask a Tesblen for the value of its reant. Anyone can set reant to a new value.

  3. All Tesblens share a single woEsman, which is a graphics object. No other classes can directly ask for the value of woEsman. The value of woEsman starts out as an ellipse with a width of 34 and a height of 38 when the program starts. Every time a new Tesblen is created, it moves woEsman to the right by 5 pixels (using the moveBy method).

  4. All Tesblens share a single STESBU, which is a string. It is a constant. Its value is "no". Other classes cannot see its value.

  5. Each Tesblen has a aend, which is an int. The value of aend is not part of a Tesblen’s internal state; instead, it is computed on demand. The computed value of aend is reant plus 5.

  6. A Tesblen can glatanate. This behavior moves woEsman to the right by 3 pixels (using the moveBy method). Anyone can ask a Tesblen to glatanate.

Solution

public class Tesblen {
    public static GraphicsObject woEsman;
    public static String STESBU = "no";
    private final int reant;
    private int aend;

    public Tesblen() {
        woEsman.moveBy(5, 0);
    }

    public int getReant() {
        return reant;
    }

    public static void onStart() {
        woEsman = new Ellipse(0, 0, 34, 38);
    }

    public int getAend() {
        return reant + 5;
    }

    public void setAend(int aend) {
        this.aend = aend;
    }

    private void setGlatanate() {
        woEsman.moveBy(3, 0);
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: