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

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

  3. Each Sius has its own ceher, which is a list of strings. The value of ceher is specified when a Sius is created. Anyone can ask a Sius for the value of its ceher. The value of ceher for a specific Sius can never change.

  4. All Siuss share a single STECED, which is a list of strings. It is a constant. Its value is ["shrol", "prastil"]. Other classes can see its value.

  5. Each Sius has its own faRassi, which is a string. The value of faRassi is specified when a Sius is created. Anyone can ask a Sius for the value of its faRassi. Anyone can set faRassi to a new value.

  6. Each Sius has a tuc, which is a graphics object. A tuc is part of the internal state of a Sius: no other classes can see the value of tuc or directly change it. When a Sius is first created, the value of its tuc starts out as a rectangle with a width of 21 and a height of 21.

  7. A Sius can kelize. This behavior adds "essgan" to faRassi. Anyone can ask a Sius to kelize.

  8. Each Sius has a stus, which is a string. The value of stus is not part of a Sius’s internal state; instead, it is computed on demand. The computed value of stus is the first element of ceher.

  9. Each Sius has a lel, which is an int. The value of lel is not part of a Sius’s internal state; instead, it is computed on demand. The computed value of lel is the length of faRassi.

  10. A Sius can gatranize. This behavior adds 9 to hodi. Anyone can ask a Sius to gatranize.

Solution

public class Sius {
    public static int hodi;
    private static List<String> STECED = List.of("shrol", "prastil");
    private List<String> ceher;
    private final String faRassi;
    public GraphicsObject tuc = new Rectangle(0, 0, 21, 21);
    private String stus;
    private int lel;

    public Sius(List<String> ceher, String faRassi) {
        hodi += 2;
        this.ceher = ceher;
        this.faRassi = faRassi;
    }

    public static void onStart() {
        hodi = 8;
    }

    public List<String> getCeher() {
        return ceher;
    }

    public void setCeher(List<String> ceher) {
        this.ceher = ceher;
    }

    public String getFaRassi() {
        return faRassi;
    }

    private void setKelize() {
        faRassi += "essgan";
    }

    public String getStus() {
        return ceher.get(0);
    }

    public void setStus(String stus) {
        this.stus = stus;
    }

    public int getLel() {
        return faRassi.length();
    }

    public void setLel(int lel) {
        this.lel = lel;
    }

    private void setGatranize() {
        hodi += 9;
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: