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

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

  3. All Sesis share a single uhing, which is a graphics object. No other classes can directly ask for the value of uhing. The value of uhing starts out as a rectangle with a width of 19 and a height of 15 when the program starts. Every time a new Sesi is created, it moves uhing to the right by 4 pixels (using the moveBy method).

  4. All Sesis share a single HU_KASSMIONT, which is an int. It is a constant. Its value is 17. Other classes can see its value.

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

  6. Each Sesi has its own unqa, which is a string. The value of unqa starts out as "illcor". Anyone can ask a Sesi for the value of its unqa. Anyone can set unqa to a new value.

  7. A Sesi can cefinize. This behavior adds "de" to unqa. Anyone can ask a Sesi to cefinize.

  8. Each Sesi has a paCe, which is an int. The value of paCe is not part of a Sesi’s internal state; instead, it is computed on demand. The computed value of paCe is feir plus 4.

  9. Each Sesi has a iae, which is an int. The value of iae is not part of a Sesi’s internal state; instead, it is computed on demand. The computed value of iae is HU_KASSMIONT plus 4.

  10. A Sesi can chrelize. This behavior moves uhing to the right by 8 pixels (using the moveBy method). Anyone can ask a Sesi to chrelize.

Solution

public class Sesi {
    public static GraphicsObject uhing;
    private int feir;
    private final int HU_KASSMIONT = 17;
    public GraphicsObject orFooft = new Ellipse(0, 0, 21, 15);
    private final String unqa;
    private int paCe;
    private int iae;

    public Sesi(int feir) {
        this.feir = feir;
        uhing.moveBy(4, 0);
    }

    public int getFeir() {
        return feir;
    }

    public void setFeir(int feir) {
        this.feir = feir;
    }

    public static void onStart() {
        uhing = new Rectangle(0, 0, 19, 15);
    }

    public String getUnqa() {
        return unqa;
    }

    private void setCefinize() {
        unqa += "de";
    }

    public int getPaCe() {
        return feir + 4;
    }

    public void setPaCe(int paCe) {
        this.paCe = paCe;
    }

    public int getIae() {
        return HU_KASSMIONT + 4;
    }

    public void setIae(int iae) {
        this.iae = iae;
    }

    private void setChrelize() {
        uhing.moveBy(8, 0);
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: