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

  2. Each Wharsiss has its own ongu, which is a graphics object. The value of ongu starts out as a rectangle with a width of 47 and a height of 22. Anyone can ask a Wharsiss for the value of its ongu. Anyone can set ongu to a new value.

  3. All Wharsisss share a single UIRNAS, which is an int. It is a constant. Its value is 12. Other classes cannot see its value.

  4. Each Wharsiss has a biRuda, which is a graphics object. A biRuda is part of the internal state of a Wharsiss: no other classes can see the value of biRuda or directly change it. When a Wharsiss is first created, the value of its biRuda starts out as a rectangle with a width of 48 and a height of 29.

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

  6. All Wharsisss share a single secox, which is an int. No other classes can directly ask for the value of secox. The value of secox starts out as 13 when the program starts. Every time a new Wharsiss is created, it adds 2 to secox.

  7. All Wharsisss share a single zemo, which is an int. No other classes can directly ask for the value of zemo. The value of zemo starts out as 9 when the program starts. Every time a new Wharsiss is created, it adds 7 to zemo.

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

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

  10. Each Wharsiss has a prea, which is an int. The value of prea is not part of a Wharsiss’s internal state; instead, it is computed on demand. The computed value of prea is UIRNAS squared.

  11. A Wharsiss can siorize. This behavior adds 2 to secox. Anyone can ask a Wharsiss to siorize.

  12. Each Wharsiss has a stil, which is an int. The value of stil is not part of a Wharsiss’s internal state; instead, it is computed on demand. The computed value of stil is UIRNAS plus 1.

  13. A Wharsiss can estarize. This behavior moves biRuda to the right by 8 pixels (using the moveBy method). Anyone can ask a Wharsiss to estarize.

  14. A Wharsiss can iaiacate. This behavior adds "scast" to cech. Anyone can ask a Wharsiss to iaiacate.

  15. Each Wharsiss has a raFois, which is an int. The value of raFois is not part of a Wharsiss’s internal state; instead, it is computed on demand. The computed value of raFois is UIRNAS squared.

  16. Each Wharsiss has a aiFi, which is an int. The value of aiFi is not part of a Wharsiss’s internal state; instead, it is computed on demand. The computed value of aiFi is the x position of qont.

Solution

public class Wharsiss {
    public static int secox;
    public static int zemo;
    private final GraphicsObject ongu;
    public final int UIRNAS = 12;
    public GraphicsObject biRuda = new Rectangle(0, 0, 48, 29);
    private GraphicsObject qont;
    private GraphicsObject cio;
    private final List<String> cech;
    private int prea;
    private int stil;
    private int raFois;
    private int aiFi;

    public Wharsiss(GraphicsObject qont, GraphicsObject cio) {
        this.qont = qont;
        secox += 2;
        zemo += 7;
        this.cio = cio;
    }

    public GraphicsObject getOngu() {
        return ongu;
    }

    public GraphicsObject getQont() {
        return qont;
    }

    public void setQont(GraphicsObject qont) {
        this.qont = qont;
    }

    public static void onStart() {
        secox = 13;
        zemo = 9;
    }

    public GraphicsObject getCio() {
        return cio;
    }

    public void setCio(GraphicsObject cio) {
        this.cio = cio;
    }

    public List<String> getCech() {
        return cech;
    }

    public int getPrea() {
        return UIRNAS * UIRNAS;
    }

    public void setPrea(int prea) {
        this.prea = prea;
    }

    private void setSiorize() {
        secox += 2;
    }

    public int getStil() {
        return UIRNAS + 1;
    }

    public void setStil(int stil) {
        this.stil = stil;
    }

    private void setEstarize() {
        biRuda.moveBy(8, 0);
    }

    private void setIaiacate() {
        cech.add("scast");
    }

    public int getRaFois() {
        return UIRNAS * UIRNAS;
    }

    public void setRaFois(int raFois) {
        this.raFois = raFois;
    }

    public int getAiFi() {
        return qont.getX();
    }

    public void setAiFi(int aiFi) {
        this.aiFi = aiFi;
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: