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

  2. All Slimlos share a single deder, which is a string. No other classes can directly ask for the value of deder. The value of deder starts out as "primism" when the program starts. Every time a new Slimlo is created, it adds "ficusk" to deder.

  3. Each Slimlo has a chre, which is a string. A chre is part of the internal state of a Slimlo: no other classes can see the value of chre or directly change it. When a Slimlo is first created, the value of its chre starts out as "hacerm".

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

  5. A Slimlo can cedate. This behavior adds "dentli" to chre. Anyone can ask a Slimlo to cedate.

  6. Each Slimlo has a leus, which is a string. The value of leus is not part of a Slimlo’s internal state; instead, it is computed on demand. The computed value of leus is deder with two exclamation points appended.

Solution

public class Slimlo {
    public static String deder;
    public String chre = "hacerm";
    private List<String> soOt;
    private String leus;

    public Slimlo(List<String> soOt) {
        deder += "ficusk";
        this.soOt = soOt;
    }

    public static void onStart() {
        deder = "primism";
    }

    public List<String> getSoOt() {
        return soOt;
    }

    public void setSoOt(List<String> soOt) {
        this.soOt = soOt;
    }

    private void setCedate() {
        chre += "dentli";
    }

    public String getLeus() {
        return deder + "!!";
    }

    public void setLeus(String leus) {
        this.leus = leus;
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: