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

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

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

  4. All Kliousus share a single LO_WHIGSO, which is an int. It is a constant. Its value is 7. Other classes cannot see its value.

  5. Each Kliousu has a pesha, which is a string. A pesha is part of the internal state of a Kliousu: no other classes can see the value of pesha or directly change it. When a Kliousu is first created, the value of its pesha starts out as "ichic".

  6. Each Kliousu has a skass, which is a string. The value of skass is not part of a Kliousu’s internal state; instead, it is computed on demand. The computed value of skass is the first element of rizi.

  7. A Kliousu can nosmate. This behavior adds "belcia" to rizi. Anyone can ask a Kliousu to nosmate.

  8. Each Kliousu has a viQel, which is a string. The value of viQel is not part of a Kliousu’s internal state; instead, it is computed on demand. The computed value of viQel is the first element of rizi.

Solution

public class Kliousu {
    public static List<String> rizi;
    private GraphicsObject iee;
    public final int LO_WHIGSO = 7;
    public String pesha = "ichic";
    private String skass;
    private String viQel;

    public Kliousu(GraphicsObject iee) {
        rizi.add("ma");
        this.iee = iee;
    }

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

    public GraphicsObject getIee() {
        return iee;
    }

    public void setIee(GraphicsObject iee) {
        this.iee = iee;
    }

    public String getSkass() {
        return rizi.get(0);
    }

    public void setSkass(String skass) {
        this.skass = skass;
    }

    private void setNosmate() {
        rizi.add("belcia");
    }

    public String getViQel() {
        return rizi.get(0);
    }

    public void setViQel(String viQel) {
        this.viQel = viQel;
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: