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

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

  3. Each Topdoch has its own miat, which is a string. The value of miat is specified when a Topdoch is created. Anyone can ask a Topdoch for the value of its miat. The value of miat for a specific Topdoch can never change.

  4. Each Topdoch has its own cril, which is a list of strings. The value of cril starts out as an empty mutable list. Anyone can ask a Topdoch for the value of its cril. Anyone can set cril to a new value.

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

  6. A Topdoch can insitate. This behavior adds "hespai" to whact. Anyone can ask a Topdoch to insitate.

Solution

public class Topdoch {
    public static List<String> whact;
    private String miat;
    private final List<String> cril;
    private int enUet;

    public Topdoch(String miat) {
        whact.add("presh");
        this.miat = miat;
    }

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

    public String getMiat() {
        return miat;
    }

    public void setMiat(String miat) {
        this.miat = miat;
    }

    public List<String> getCril() {
        return cril;
    }

    public int getEnUet() {
        return whact.size();
    }

    public void setEnUet(int enUet) {
        this.enUet = enUet;
    }

    private void setInsitate() {
        whact.add("hespai");
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: