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

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

  3. All Glabis share a single penu, which is a string. No other classes can directly ask for the value of penu. The value of penu starts out as "sidre" when the program starts. Every time a new Glabi is created, it adds "el" to penu.

  4. Each Glabi has its own diur, which is a graphics object. The value of diur starts out as an ellipse with a width of 28 and a height of 44. Anyone can ask a Glabi for the value of its diur. Anyone can set diur to a new value.

  5. Each Glabi has a erSisar, which is an int. The value of erSisar is not part of a Glabi’s internal state; instead, it is computed on demand. The computed value of erSisar is the size of wiEs.

  6. A Glabi can aldanize. This behavior adds "ri" to wiEs. Anyone can ask a Glabi to aldanize.

Solution

public class Glabi {
    public static String penu;
    public List<String> wiEs = new ArrayList<>();
    private final GraphicsObject diur;
    private int erSisar;

    public Glabi() {
        penu += "el";
    }

    public static void onStart() {
        penu = "sidre";
    }

    public GraphicsObject getDiur() {
        return diur;
    }

    public int getErSisar() {
        return wiEs.size();
    }

    public void setErSisar(int erSisar) {
        this.erSisar = erSisar;
    }

    private void setAldanize() {
        wiEs.add("ri");
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: