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 an Ouda.

  2. Each Ouda has its own chala, which is a list of strings. The value of chala starts out as an empty mutable list. Anyone can ask an Ouda for the value of its chala. Anyone can set chala to a new value.

  3. All Oudas share a single GIODPENG, which is a list of strings. It is a constant. Its value is ["cacfloss", "suaa"]. Other classes can see its value.

  4. Each Ouda has its own mipam, which is an int. The value of mipam is specified when a Ouda is created. Anyone can ask an Ouda for the value of its mipam. The value of mipam for a specific Ouda can never change.

  5. Each Ouda has a paEd, which is an int. The value of paEd is not part of an Ouda’s internal state; instead, it is computed on demand. The computed value of paEd is the size of GIODPENG.

  6. An Ouda can islatize. This behavior adds "ondu" to chala. Anyone can ask an Ouda to islatize.

Solution

public class Ouda {
    private static List<String> GIODPENG = List.of("cacfloss", "suaa");
    private final List<String> chala;
    private int mipam;
    private int paEd;

    public Ouda(int mipam) {
        this.mipam = mipam;
    }

    public List<String> getChala() {
        return chala;
    }

    public int getMipam() {
        return mipam;
    }

    public void setMipam(int mipam) {
        this.mipam = mipam;
    }

    public int getPaEd() {
        return GIODPENG.size();
    }

    public void setPaEd(int paEd) {
        this.paEd = paEd;
    }

    private void setIslatize() {
        chala.add("ondu");
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: