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

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

  3. Each Jessas has a emEchda, which is an int. An emEchda is part of the internal state of a Jessas: no other classes can see the value of emEchda or directly change it. When a Jessas is first created, the value of its emEchda starts out as 11.

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

Solution

public class Jessas {
    public static List<String> riMumas;
    public int emEchda = 11;
    private int milpi;

    public Jessas() {
        riMumas.add("ar");
    }

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

    public int getMilpi() {
        return riMumas.size();
    }

    public void setMilpi(int milpi) {
        this.milpi = milpi;
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: