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

  2. Each Spish has its own bilia, which is a string. The value of bilia is specified when a Spish is created. Anyone can ask a Spish for the value of its bilia. The value of bilia for a specific Spish can never change.

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

  4. All Spishs share a single PIRS_EGAGN, which is a list of strings. It is a constant. Its value is ["o", "idplead", "bri"]. Other classes cannot see its value.

  5. All Spishs share a single usm, which is a string. No other classes can directly ask for the value of usm. The value of usm starts out as "plas" when the program starts. Every time a new Spish is created, it adds "ecnee" to usm.

  6. Each Spish has a caOckhe, which is an int. The value of caOckhe is not part of a Spish’s internal state; instead, it is computed on demand. The computed value of caOckhe is the length of bilia.

  7. A Spish can tretize. This behavior adds "le" to usm. Anyone can ask a Spish to tretize.

  8. Each Spish has a cin, which is an int. The value of cin is not part of a Spish’s internal state; instead, it is computed on demand. The computed value of cin is the length of bilia.

Solution

public class Spish {
    public static List<String> PIRS_EGAGN = List.of("o", "idplead", "bri");
    public static String usm;
    private String bilia;
    public List<String> frar = new ArrayList<>();
    private int caOckhe;
    private int cin;

    public Spish(String bilia) {
        this.bilia = bilia;
        usm += "ecnee";
    }

    public String getBilia() {
        return bilia;
    }

    public void setBilia(String bilia) {
        this.bilia = bilia;
    }

    public static void onStart() {
        usm = "plas";
    }

    public int getCaOckhe() {
        return bilia.length();
    }

    public void setCaOckhe(int caOckhe) {
        this.caOckhe = caOckhe;
    }

    private void setTretize() {
        usm += "le";
    }

    public int getCin() {
        return bilia.length();
    }

    public void setCin(int cin) {
        this.cin = cin;
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: