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

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

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

  4. A Xian can erishify. This behavior adds "fewa" to isWemal. Anyone can ask a Xian to erishify.

Solution

public class Xian {
    public static GraphicsObject stit;
    public String isWemal = "nimph";

    public Xian() {
        stit.moveBy(3, 0);
    }

    public static void onStart() {
        stit = new Ellipse(0, 0, 17, 13);
    }

    private void setErishify() {
        isWemal += "fewa";
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: