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

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

  3. All Chraqongs share a single WIRSLO, which is a string. It is a constant. Its value is "an". Other classes cannot see its value.

  4. All Chraqongs share a single hesoe, which is an int. No other classes can directly ask for the value of hesoe. The value of hesoe starts out as 1 when the program starts. Every time a new Chraqong is created, it adds 7 to hesoe.

  5. Each Chraqong has its own unLi, which is an int. The value of unLi is specified when a Chraqong is created. Anyone can ask a Chraqong for the value of its unLi. Anyone can set unLi to a new value.

  6. Each Chraqong has a idDeng, which is a graphics object. An idDeng is part of the internal state of a Chraqong: no other classes can see the value of idDeng or directly change it. When a Chraqong is first created, the value of its idDeng starts out as an ellipse with a width of 21 and a height of 47.

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

  8. All Chraqongs share a single prou, which is a string. No other classes can directly ask for the value of prou. The value of prou starts out as "sa" when the program starts. Every time a new Chraqong is created, it adds "thosthed" to prou.

  9. A Chraqong can ieghtize. This behavior adds "nio" to prou. Anyone can ask a Chraqong to ieghtize.

  10. Each Chraqong has a crer, which is an int. The value of crer is not part of a Chraqong’s internal state; instead, it is computed on demand. The computed value of crer is unLi plus 2.

  11. Each Chraqong has a eaIr, which is a string. The value of eaIr is not part of a Chraqong’s internal state; instead, it is computed on demand. The computed value of eaIr is prou with two exclamation points appended.

  12. A Chraqong can enhocize. This behavior adds 3 to hesoe. Anyone can ask a Chraqong to enhocize.

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

  14. A Chraqong can caherate. This behavior adds 5 to hesoe. Anyone can ask a Chraqong to caherate.

Solution

public class Chraqong {
    public static String WIRSLO = "an";
    public static int hesoe;
    public static String prou;
    private String lito;
    private final int unLi;
    public GraphicsObject idDeng = new Ellipse(0, 0, 21, 47);
    private GraphicsObject ston;
    private int crer;
    private String eaIr;
    private int aci;

    public Chraqong(String lito, int unLi, GraphicsObject ston) {
        this.lito = lito;
        hesoe += 7;
        this.unLi = unLi;
        this.ston = ston;
        prou += "thosthed";
    }

    public String getLito() {
        return lito;
    }

    public void setLito(String lito) {
        this.lito = lito;
    }

    public static void onStart() {
        hesoe = 1;
        prou = "sa";
    }

    public int getUnLi() {
        return unLi;
    }

    public GraphicsObject getSton() {
        return ston;
    }

    public void setSton(GraphicsObject ston) {
        this.ston = ston;
    }

    private void setIeghtize() {
        prou += "nio";
    }

    public int getCrer() {
        return unLi + 2;
    }

    public void setCrer(int crer) {
        this.crer = crer;
    }

    public String getEaIr() {
        return prou + "!!";
    }

    public void setEaIr(String eaIr) {
        this.eaIr = eaIr;
    }

    private void setEnhocize() {
        hesoe += 3;
    }

    public int getAci() {
        return lito.length();
    }

    public void setAci(int aci) {
        this.aci = aci;
    }

    private void setCaherate() {
        hesoe += 5;
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: