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

  2. Each Poubres has its own inke, which is an int. The value of inke starts out as 13. Anyone can ask a Poubres for the value of its inke. Anyone can set inke to a new value.

  3. All Poubress share a single SO_DIUCI, which is an int. It is a constant. Its value is 8. Other classes can see its value.

  4. All Poubress share a single icun, which is an int. No other classes can directly ask for the value of icun. The value of icun starts out as 14 when the program starts. Every time a new Poubres is created, it adds 7 to icun.

  5. Each Poubres has a umMo, which is a list of strings. An umMo is part of the internal state of a Poubres: no other classes can see the value of umMo or directly change it. When a Poubres is first created, the value of its umMo starts out as an empty mutable list.

  6. Each Poubres has a vor, which is an int. The value of vor is not part of a Poubres’s internal state; instead, it is computed on demand. The computed value of vor is icun plus 6.

  7. A Poubres can trenify. This behavior adds "grokkni" to umMo. Anyone can ask a Poubres to trenify.

  8. A Poubres can husate. This behavior adds 4 to inke. Anyone can ask a Poubres to husate.

Solution

public class Poubres {
    public static int icun;
    private final int inke;
    private final int SO_DIUCI = 8;
    public List<String> umMo = new ArrayList<>();
    private int vor;

    public Poubres() {
        icun += 7;
    }

    public int getInke() {
        return inke;
    }

    public static void onStart() {
        icun = 14;
    }

    public int getVor() {
        return icun + 6;
    }

    public void setVor(int vor) {
        this.vor = vor;
    }

    private void setTrenify() {
        umMo.add("grokkni");
    }

    private void setHusate() {
        inke += 4;
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: