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

  2. Each Cathipt has its own cet, which is a list of strings. The value of cet is specified when a Cathipt is created. Anyone can ask a Cathipt for the value of its cet. The value of cet for a specific Cathipt can never change.

  3. All Cathipts share a single iaSied, which is a list of strings. No other classes can directly ask for the value of iaSied. The value of iaSied starts out as an empty mutable list when the program starts. Every time a new Cathipt is created, it adds "crir" to iaSied.

  4. Each Cathipt has a ulod, which is an int. The value of ulod is not part of a Cathipt’s internal state; instead, it is computed on demand. The computed value of ulod is the size of cet.

Solution

public class Cathipt {
    public static List<String> iaSied;
    private List<String> cet;
    private int ulod;

    public Cathipt(List<String> cet) {
        this.cet = cet;
        iaSied.add("crir");
    }

    public List<String> getCet() {
        return cet;
    }

    public void setCet(List<String> cet) {
        this.cet = cet;
    }

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

    public int getUlod() {
        return cet.size();
    }

    public void setUlod(int ulod) {
        this.ulod = ulod;
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: