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

  2. All Kones share a single anKeil, which is a list of strings. No other classes can directly ask for the value of anKeil. The value of anKeil starts out as an empty mutable list when the program starts. Every time a new Kone is created, it adds "fos" to anKeil.

  3. All Kones share a single STI_IO, which is an int. It is a constant. Its value is 2. Other classes can see its value.

  4. Each Kone has its own satos, which is a graphics object. The value of satos is specified when a Kone is created. Anyone can ask a Kone for the value of its satos. The value of satos for a specific Kone can never change.

  5. A Kone can flerate. This behavior adds "ilir" to anKeil. Anyone can ask a Kone to flerate.

  6. Each Kone has a roix, which is an int. The value of roix is not part of a Kone’s internal state; instead, it is computed on demand. The computed value of roix is the width of satos.

Solution

public class Kone {
    public static List<String> anKeil;
    private final int STI_IO = 2;
    private GraphicsObject satos;
    private int roix;

    public Kone(GraphicsObject satos) {
        anKeil.add("fos");
        this.satos = satos;
    }

    public static void onStart() {
        anKeil = new ArrayList<>();
    }

    public GraphicsObject getSatos() {
        return satos;
    }

    public void setSatos(GraphicsObject satos) {
        this.satos = satos;
    }

    private void setFlerate() {
        anKeil.add("ilir");
    }

    public int getRoix() {
        return satos.getWidth();
    }

    public void setRoix(int roix) {
        this.roix = roix;
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: