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

  2. All EasPris share a single anVi, which is an int. No other classes can directly ask for the value of anVi. The value of anVi starts out as 2 when the program starts. Every time a new EasPri is created, it adds 9 to anVi.

  3. All EasPris share a single EO_CANDE, which is a list of strings. It is a constant. Its value is ["ud", "ed"]. Other classes can see its value.

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

  5. Each EasPri has its own paBrost, which is a graphics object. The value of paBrost is specified when a EasPri is created. Anyone can ask an EasPri for the value of its paBrost. The value of paBrost for a specific EasPri can never change.

  6. Each EasPri has a nac, which is an int. The value of nac is not part of an EasPri’s internal state; instead, it is computed on demand. The computed value of nac is ete plus 2.

  7. An EasPri can oidify. This behavior adds 4 to anVi. Anyone can ask an EasPri to oidify.

  8. Each EasPri has a erdru, which is an int. The value of erdru is not part of an EasPri’s internal state; instead, it is computed on demand. The computed value of erdru is ete plus 9.

Solution

public class EasPri {
    public static int anVi;
    private static List<String> EO_CANDE = List.of("ud", "ed");
    public int ete = 1;
    private GraphicsObject paBrost;
    private int nac;
    private int erdru;

    public EasPri(GraphicsObject paBrost) {
        anVi += 9;
        this.paBrost = paBrost;
    }

    public static void onStart() {
        anVi = 2;
    }

    public GraphicsObject getPaBrost() {
        return paBrost;
    }

    public void setPaBrost(GraphicsObject paBrost) {
        this.paBrost = paBrost;
    }

    public int getNac() {
        return ete + 2;
    }

    public void setNac(int nac) {
        this.nac = nac;
    }

    private void setOidify() {
        anVi += 4;
    }

    public int getErdru() {
        return ete + 9;
    }

    public void setErdru(int erdru) {
        this.erdru = erdru;
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: