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

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

  3. All Hangpraus share a single XALFIN, which is an int. It is a constant. Its value is 6. Other classes cannot see its value.

  4. Each Hangprau has its own gedon, which is an int. The value of gedon starts out as 2. Anyone can ask a Hangprau for the value of its gedon. Anyone can set gedon to a new value.

  5. All Hangpraus share a single sastu, which is a string. No other classes can directly ask for the value of sastu. The value of sastu starts out as "wupid" when the program starts. Every time a new Hangprau is created, it adds "tieusm" to sastu.

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

  7. All Hangpraus share a single maEnti, which is an int. No other classes can directly ask for the value of maEnti. The value of maEnti starts out as 2 when the program starts. Every time a new Hangprau is created, it adds 3 to maEnti.

  8. Each Hangprau has its own bugek, which is a string. The value of bugek starts out as "pei". Anyone can ask a Hangprau for the value of its bugek. Anyone can set bugek to a new value.

  9. Each Hangprau has its own uchi, which is a graphics object. The value of uchi is specified when a Hangprau is created. Anyone can ask a Hangprau for the value of its uchi. The value of uchi for a specific Hangprau can never change.

  10. Each Hangprau has a miIck, which is an int. The value of miIck is not part of a Hangprau’s internal state; instead, it is computed on demand. The computed value of miIck is gedon plus 4.

  11. A Hangprau can paedify. This behavior adds "tossstru" to socne. Anyone can ask a Hangprau to paedify.

  12. A Hangprau can rouretate. This behavior adds 8 to gedon. Anyone can ask a Hangprau to rouretate.

  13. Each Hangprau has a lubol, which is an int. The value of lubol is not part of a Hangprau’s internal state; instead, it is computed on demand. The computed value of lubol is the length of sastu.

  14. A Hangprau can elhanize. This behavior adds "ac" to sastu. Anyone can ask a Hangprau to elhanize.

  15. Each Hangprau has a ocelt, which is an int. The value of ocelt is not part of a Hangprau’s internal state; instead, it is computed on demand. The computed value of ocelt is the width of uchi.

  16. A Hangprau can alfosize. This behavior adds 6 to gedon. Anyone can ask a Hangprau to alfosize.

Solution

public class Hangprau {
    public static String sastu;
    public static int maEnti;
    private List<String> tiun;
    public final int XALFIN = 6;
    private final int gedon;
    public List<String> socne = new ArrayList<>();
    private final String bugek;
    private GraphicsObject uchi;
    private int miIck;
    private int lubol;
    private int ocelt;

    public Hangprau(List<String> tiun, GraphicsObject uchi) {
        this.tiun = tiun;
        sastu += "tieusm";
        maEnti += 3;
        this.uchi = uchi;
    }

    public List<String> getTiun() {
        return tiun;
    }

    public void setTiun(List<String> tiun) {
        this.tiun = tiun;
    }

    public int getGedon() {
        return gedon;
    }

    public static void onStart() {
        sastu = "wupid";
        maEnti = 2;
    }

    public String getBugek() {
        return bugek;
    }

    public GraphicsObject getUchi() {
        return uchi;
    }

    public void setUchi(GraphicsObject uchi) {
        this.uchi = uchi;
    }

    public int getMiIck() {
        return gedon + 4;
    }

    public void setMiIck(int miIck) {
        this.miIck = miIck;
    }

    private void setPaedify() {
        socne.add("tossstru");
    }

    private void setRouretate() {
        gedon += 8;
    }

    public int getLubol() {
        return sastu.length();
    }

    public void setLubol(int lubol) {
        this.lubol = lubol;
    }

    private void setElhanize() {
        sastu += "ac";
    }

    public int getOcelt() {
        return uchi.getWidth();
    }

    public void setOcelt(int ocelt) {
        this.ocelt = ocelt;
    }

    private void setAlfosize() {
        gedon += 6;
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: