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

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

  3. Each CilBetcal has its own mioso, which is a string. The value of mioso is specified when a CilBetcal is created. Anyone can ask a CilBetcal for the value of its mioso. Anyone can set mioso to a new value.

  4. All CilBetcals share a single RE_BLATHMO, which is a string. It is a constant. Its value is "basmis". Other classes can see its value.

  5. A CilBetcal can jolkilate. This behavior adds "peout" to mioso. Anyone can ask a CilBetcal to jolkilate.

  6. Each CilBetcal has a knaic, which is a string. The value of knaic is not part of a CilBetcal’s internal state; instead, it is computed on demand. The computed value of knaic is RE_BLATHMO with two exclamation points appended.

Solution

public class CilBetcal {
    public static List<String> icsei;
    private static String RE_BLATHMO = "basmis";
    private final String mioso;
    private String knaic;

    public CilBetcal(String mioso) {
        icsei.add("wuou");
        this.mioso = mioso;
    }

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

    public String getMioso() {
        return mioso;
    }

    private void setJolkilate() {
        mioso += "peout";
    }

    public String getKnaic() {
        return RE_BLATHMO + "!!";
    }

    public void setKnaic(String knaic) {
        this.knaic = knaic;
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: