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

  2. All TocHiochars share a single rer, which is a string. No other classes can directly ask for the value of rer. The value of rer starts out as "hestsli" when the program starts. Every time a new TocHiochar is created, it adds "slatz" to rer.

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

  4. Each TocHiochar has its own mocso, which is a list of strings. The value of mocso is specified when a TocHiochar is created. Anyone can ask a TocHiochar for the value of its mocso. Anyone can set mocso to a new value.

  5. All TocHiochars share a single GUI_CEAFIN, which is an int. It is a constant. Its value is 17. Other classes cannot see its value.

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

  7. Each TocHiochar has a irin, which is an int. An irin is part of the internal state of a TocHiochar: no other classes can see the value of irin or directly change it. When a TocHiochar is first created, the value of its irin starts out as 4.

  8. Each TocHiochar has a peet, which is an int. The value of peet is not part of a TocHiochar’s internal state; instead, it is computed on demand. The computed value of peet is GUI_CEAFIN plus 6.

  9. A TocHiochar can schenify. This behavior adds "toszim" to rer. Anyone can ask a TocHiochar to schenify.

  10. A TocHiochar can hessate. This behavior adds 8 to irin. Anyone can ask a TocHiochar to hessate.

  11. Each TocHiochar has a hudi, which is an int. The value of hudi is not part of a TocHiochar’s internal state; instead, it is computed on demand. The computed value of hudi is the length of rer.

  12. Each TocHiochar has a apEc, which is an int. The value of apEc is not part of a TocHiochar’s internal state; instead, it is computed on demand. The computed value of apEc is irin plus 4.

Solution

public class TocHiochar {
    public static String rer;
    public List<String> eus = new ArrayList<>();
    private final List<String> mocso;
    public final int GUI_CEAFIN = 17;
    private int soBloss;
    public int irin = 4;
    private int peet;
    private int hudi;
    private int apEc;

    public TocHiochar(List<String> mocso, int soBloss) {
        rer += "slatz";
        this.mocso = mocso;
        this.soBloss = soBloss;
    }

    public static void onStart() {
        rer = "hestsli";
    }

    public List<String> getMocso() {
        return mocso;
    }

    public int getSoBloss() {
        return soBloss;
    }

    public void setSoBloss(int soBloss) {
        this.soBloss = soBloss;
    }

    public int getPeet() {
        return GUI_CEAFIN + 6;
    }

    public void setPeet(int peet) {
        this.peet = peet;
    }

    private void setSchenify() {
        rer += "toszim";
    }

    private void setHessate() {
        irin += 8;
    }

    public int getHudi() {
        return rer.length();
    }

    public void setHudi(int hudi) {
        this.hudi = hudi;
    }

    public int getApEc() {
        return irin + 4;
    }

    public void setApEc(int apEc) {
        this.apEc = apEc;
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: