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

  2. All Seous share a single esos, which is an int. No other classes can directly ask for the value of esos. The value of esos starts out as 4 when the program starts. Every time a new Seou is created, it adds 5 to esos.

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

  4. Each Seou has a haMopi, which is an int. The value of haMopi is not part of a Seou’s internal state; instead, it is computed on demand. The computed value of haMopi is esos plus 1.

Solution

public class Seou {
    public static int esos;
    private List<String> caer;
    private int haMopi;

    public Seou(List<String> caer) {
        esos += 5;
        this.caer = caer;
    }

    public static void onStart() {
        esos = 4;
    }

    public List<String> getCaer() {
        return caer;
    }

    public void setCaer(List<String> caer) {
        this.caer = caer;
    }

    public int getHaMopi() {
        return esos + 1;
    }

    public void setHaMopi(int haMopi) {
        this.haMopi = haMopi;
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: