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

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

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

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

Solution

public class Tont {
    public static List<String> apli;
    private List<String> egReoss;
    private int liIl;

    public Tont(List<String> egReoss) {
        apli.add("cian");
        this.egReoss = egReoss;
    }

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

    public List<String> getEgReoss() {
        return egReoss;
    }

    public void setEgReoss(List<String> egReoss) {
        this.egReoss = egReoss;
    }

    public int getLiIl() {
        return egReoss.size();
    }

    public void setLiIl(int liIl) {
        this.liIl = liIl;
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: