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 an OulAltred.

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

  3. Each OulAltred has its own odOt, which is an int. The value of odOt starts out as 11. Anyone can ask an OulAltred for the value of its odOt. Anyone can set odOt to a new value.

  4. Each OulAltred has its own asik, which is an int. The value of asik is specified when a OulAltred is created. Anyone can ask an OulAltred for the value of its asik. The value of asik for a specific OulAltred can never change.

  5. An OulAltred can canudify. This behavior adds "monboss" to cic. Anyone can ask an OulAltred to canudify.

  6. Each OulAltred has a seng, which is an int. The value of seng is not part of an OulAltred’s internal state; instead, it is computed on demand. The computed value of seng is the size of cic.

Solution

public class OulAltred {
    public static List<String> cic;
    private final int odOt;
    private int asik;
    private int seng;

    public OulAltred(int asik) {
        cic.add("ontess");
        this.asik = asik;
    }

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

    public int getOdOt() {
        return odOt;
    }

    public int getAsik() {
        return asik;
    }

    public void setAsik(int asik) {
        this.asik = asik;
    }

    private void setCanudify() {
        cic.add("monboss");
    }

    public int getSeng() {
        return cic.size();
    }

    public void setSeng(int seng) {
        this.seng = seng;
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: