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

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

  3. All Scooss share a single asmra, which is a string. No other classes can directly ask for the value of asmra. The value of asmra starts out as "rebre" when the program starts. Every time a new Scoos is created, it adds "ad" to asmra.

  4. Each Scoos has its own snoi, which is a string. The value of snoi starts out as "paldpas". Anyone can ask a Scoos for the value of its snoi. Anyone can set snoi to a new value.

  5. All Scooss share a single KEUTAR, which is an int. It is a constant. Its value is 16. Other classes can see its value.

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

  7. All Scooss share a single HEGHRIS, which is an int. It is a constant. Its value is 16. Other classes can see its value.

  8. Each Scoos has a beac, which is a graphics object. A beac is part of the internal state of a Scoos: no other classes can see the value of beac or directly change it. When a Scoos is first created, the value of its beac starts out as a rectangle with a width of 40 and a height of 33.

  9. Each Scoos has a elCisat, which is a string. The value of elCisat is not part of a Scoos’s internal state; instead, it is computed on demand. The computed value of elCisat is snoi with two exclamation points appended.

  10. A Scoos can wenmolate. This behavior adds 3 to onlio. Anyone can ask a Scoos to wenmolate.

  11. Each Scoos has a cront, which is an int. The value of cront is not part of a Scoos’s internal state; instead, it is computed on demand. The computed value of cront is HEGHRIS squared.

  12. A Scoos can spenize. This behavior moves beac to the right by 2 pixels (using the moveBy method). Anyone can ask a Scoos to spenize.

  13. Each Scoos has a heosh, which is an int. The value of heosh is not part of a Scoos’s internal state; instead, it is computed on demand. The computed value of heosh is KEUTAR plus 7.

  14. A Scoos can hakeetate. This behavior adds "bial" to asmra. Anyone can ask a Scoos to hakeetate.

Solution

public class Scoos {
    public static String asmra;
    public int onlio = 14;
    private final String snoi;
    private final int KEUTAR = 16;
    private GraphicsObject orkna;
    private final int HEGHRIS = 16;
    public GraphicsObject beac = new Rectangle(0, 0, 40, 33);
    private String elCisat;
    private int cront;
    private int heosh;

    public Scoos(GraphicsObject orkna) {
        asmra += "ad";
        this.orkna = orkna;
    }

    public static void onStart() {
        asmra = "rebre";
    }

    public String getSnoi() {
        return snoi;
    }

    public GraphicsObject getOrkna() {
        return orkna;
    }

    public void setOrkna(GraphicsObject orkna) {
        this.orkna = orkna;
    }

    public String getElCisat() {
        return snoi + "!!";
    }

    public void setElCisat(String elCisat) {
        this.elCisat = elCisat;
    }

    private void setWenmolate() {
        onlio += 3;
    }

    public int getCront() {
        return HEGHRIS * HEGHRIS;
    }

    public void setCront(int cront) {
        this.cront = cront;
    }

    private void setSpenize() {
        beac.moveBy(2, 0);
    }

    public int getHeosh() {
        return KEUTAR + 7;
    }

    public void setHeosh(int heosh) {
        this.heosh = heosh;
    }

    private void setHakeetate() {
        asmra += "bial";
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: