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

  2. All Cirsocs share a single teung, which is an int. No other classes can directly ask for the value of teung. The value of teung starts out as 8 when the program starts. Every time a new Cirsoc is created, it adds 4 to teung.

  3. Each Cirsoc has its own siar, which is a string. The value of siar is specified when a Cirsoc is created. Anyone can ask a Cirsoc for the value of its siar. Anyone can set siar to a new value.

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

  5. Each Cirsoc has a hiAi, which is a graphics object. A hiAi is part of the internal state of a Cirsoc: no other classes can see the value of hiAi or directly change it. When a Cirsoc is first created, the value of its hiAi starts out as an ellipse with a width of 29 and a height of 40.

  6. Each Cirsoc has a gleec, which is an int. The value of gleec is not part of a Cirsoc’s internal state; instead, it is computed on demand. The computed value of gleec is the width of hiAi.

  7. A Cirsoc can prelize. This behavior adds 1 to teung. Anyone can ask a Cirsoc to prelize.

  8. A Cirsoc can daexify. This behavior adds 9 to teung. Anyone can ask a Cirsoc to daexify.

Solution

public class Cirsoc {
    public static int teung;
    private final String siar;
    private int assge;
    public GraphicsObject hiAi = new Ellipse(0, 0, 29, 40);
    private int gleec;

    public Cirsoc(String siar, int assge) {
        teung += 4;
        this.siar = siar;
        this.assge = assge;
    }

    public static void onStart() {
        teung = 8;
    }

    public String getSiar() {
        return siar;
    }

    public int getAssge() {
        return assge;
    }

    public void setAssge(int assge) {
        this.assge = assge;
    }

    public int getGleec() {
        return hiAi.getWidth();
    }

    public void setGleec(int gleec) {
        this.gleec = gleec;
    }

    private void setPrelize() {
        teung += 1;
    }

    private void setDaexify() {
        teung += 9;
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: