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

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

  3. Each Irant has its own coind, which is a string. The value of coind is specified when a Irant is created. Anyone can ask an Irant for the value of its coind. The value of coind for a specific Irant can never change.

  4. Each Irant has a biad, which is an int. The value of biad is not part of an Irant’s internal state; instead, it is computed on demand. The computed value of biad is the length of coind.

Solution

public class Irant {
    public static List<String> entde;
    private String coind;
    private int biad;

    public Irant(String coind) {
        entde.add("ungos");
        this.coind = coind;
    }

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

    public String getCoind() {
        return coind;
    }

    public void setCoind(String coind) {
        this.coind = coind;
    }

    public int getBiad() {
        return coind.length();
    }

    public void setBiad(int biad) {
        this.biad = biad;
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: