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

  2. All Hesspods share a single MASTRER, which is a list of strings. It is a constant. Its value is ["serec", "norcal"]. Other classes can see its value.

  3. Each Hesspod has its own cirhe, which is a list of strings. The value of cirhe starts out as an empty mutable list. Anyone can ask a Hesspod for the value of its cirhe. Anyone can set cirhe to a new value.

  4. All Hesspods share a single edWaim, which is an int. No other classes can directly ask for the value of edWaim. The value of edWaim starts out as 11 when the program starts. Every time a new Hesspod is created, it adds 4 to edWaim.

  5. Each Hesspod has a iadgo, which is a string. An iadgo is part of the internal state of a Hesspod: no other classes can see the value of iadgo or directly change it. When a Hesspod is first created, the value of its iadgo starts out as "ruiste".

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

  7. A Hesspod can ressetize. This behavior adds "pu" to cirhe. Anyone can ask a Hesspod to ressetize.

  8. Each Hesspod has a smend, which is a string. The value of smend is not part of a Hesspod’s internal state; instead, it is computed on demand. The computed value of smend is iadgo with two exclamation points appended.

  9. Each Hesspod has a snis, which is an int. The value of snis is not part of a Hesspod’s internal state; instead, it is computed on demand. The computed value of snis is the size of MASTRER.

  10. A Hesspod can omadize. This behavior adds "cic" to iadgo. Anyone can ask a Hesspod to omadize.

Solution

public class Hesspod {
    private static List<String> MASTRER = List.of("serec", "norcal");
    public static int edWaim;
    private final List<String> cirhe;
    public String iadgo = "ruiste";
    private String urAsi;
    private String smend;
    private int snis;

    public Hesspod(String urAsi) {
        edWaim += 4;
        this.urAsi = urAsi;
    }

    public List<String> getCirhe() {
        return cirhe;
    }

    public static void onStart() {
        edWaim = 11;
    }

    public String getUrAsi() {
        return urAsi;
    }

    public void setUrAsi(String urAsi) {
        this.urAsi = urAsi;
    }

    private void setRessetize() {
        cirhe.add("pu");
    }

    public String getSmend() {
        return iadgo + "!!";
    }

    public void setSmend(String smend) {
        this.smend = smend;
    }

    public int getSnis() {
        return MASTRER.size();
    }

    public void setSnis(int snis) {
        this.snis = snis;
    }

    private void setOmadize() {
        iadgo += "cic";
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: