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

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

  3. All Plinhowns share a single HOSM_ED, which is an int. It is a constant. Its value is 14. Other classes can see its value.

  4. A Plinhown can urchunate. This behavior adds "sto" to antoc. Anyone can ask a Plinhown to urchunate.

Solution

public class Plinhown {
    public static List<String> antoc;
    private final int HOSM_ED = 14;

    public Plinhown() {
        antoc.add("pe");
    }

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

    private void setUrchunate() {
        antoc.add("sto");
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: