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

  2. Each Steo has a gonen, which is a list of strings. A gonen is part of the internal state of a Steo: no other classes can see the value of gonen or directly change it. When a Steo is first created, the value of its gonen starts out as an empty mutable list.

  3. All Steos share a single cus, which is a graphics object. No other classes can directly ask for the value of cus. The value of cus starts out as a rectangle with a width of 10 and a height of 34 when the program starts. Every time a new Steo is created, it moves cus to the right by 7 pixels (using the moveBy method).

  4. Each Steo has a siont, which is a string. The value of siont is not part of a Steo’s internal state; instead, it is computed on demand. The computed value of siont is the first element of gonen.

Solution

public class Steo {
    public static GraphicsObject cus;
    public List<String> gonen = new ArrayList<>();
    private String siont;

    public Steo() {
        cus.moveBy(7, 0);
    }

    public static void onStart() {
        cus = new Rectangle(0, 0, 10, 34);
    }

    public String getSiont() {
        return gonen.get(0);
    }

    public void setSiont(String siont) {
        this.siont = siont;
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: