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

  2. All Odhas share a single ILDEM_MOISI, which is a list of strings. It is a constant. Its value is ["riontos", "esscic", "e"]. Other classes cannot see its value.

  3. All Odhas share a single sadci, which is an int. No other classes can directly ask for the value of sadci. The value of sadci starts out as 16 when the program starts. Every time a new Odha is created, it adds 6 to sadci.

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

  5. Each Odha has a thag, which is an int. The value of thag is not part of an Odha’s internal state; instead, it is computed on demand. The computed value of thag is sadci squared.

  6. An Odha can panenize. This behavior adds 9 to sadci. Anyone can ask an Odha to panenize.

Solution

public class Odha {
    public static List<String> ILDEM_MOISI = List.of("riontos", "esscic", "e");
    public static int sadci;
    private int sli;
    private int thag;

    public Odha(int sli) {
        sadci += 6;
        this.sli = sli;
    }

    public static void onStart() {
        sadci = 16;
    }

    public int getSli() {
        return sli;
    }

    public void setSli(int sli) {
        this.sli = sli;
    }

    public int getThag() {
        return sadci * sadci;
    }

    public void setThag(int thag) {
        this.thag = thag;
    }

    private void setPanenize() {
        sadci += 9;
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: