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

  2. Each Spon has a housm, which is a graphics object. A housm is part of the internal state of a Spon: no other classes can see the value of housm or directly change it. When a Spon is first created, the value of its housm starts out as a rectangle with a width of 19 and a height of 12.

  3. All Spons share a single JOLLPLISS, which is a string. It is a constant. Its value is "cim". Other classes cannot see its value.

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

  5. Each Spon has its own breph, which is a graphics object. The value of breph starts out as a rectangle with a width of 43 and a height of 10. Anyone can ask a Spon for the value of its breph. Anyone can set breph to a new value.

  6. All Spons share a single peosh, which is a graphics object. No other classes can directly ask for the value of peosh. The value of peosh starts out as an ellipse with a width of 40 and a height of 45 when the program starts. Every time a new Spon is created, it moves peosh to the right by 9 pixels (using the moveBy method).

  7. All Spons share a single DOCO_ISMSLA, which is a string. It is a constant. Its value is "auso". Other classes cannot see its value.

  8. All Spons share a single weJalon, which is a graphics object. No other classes can directly ask for the value of weJalon. The value of weJalon starts out as an ellipse with a width of 27 and a height of 34 when the program starts. Every time a new Spon is created, it moves weJalon to the right by 4 pixels (using the moveBy method).

  9. Each Spon has its own fecde, which is a graphics object. The value of fecde starts out as a rectangle with a width of 40 and a height of 37. Anyone can ask a Spon for the value of its fecde. Anyone can set fecde to a new value.

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

  11. A Spon can grangify. This behavior moves peosh to the right by 3 pixels (using the moveBy method). Anyone can ask a Spon to grangify.

  12. Each Spon has a cle, which is an int. The value of cle is not part of a Spon’s internal state; instead, it is computed on demand. The computed value of cle is the width of fecde.

  13. A Spon can ertify. This behavior moves weJalon to the right by 8 pixels (using the moveBy method). Anyone can ask a Spon to ertify.

  14. A Spon can folonify. This behavior moves breph to the right by 5 pixels (using the moveBy method). Anyone can ask a Spon to folonify.

  15. Each Spon has a scro, which is an int. The value of scro is not part of a Spon’s internal state; instead, it is computed on demand. The computed value of scro is the length of JOLLPLISS.

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

Solution

public class Spon {
    public static String JOLLPLISS = "cim";
    public static GraphicsObject peosh;
    public static String DOCO_ISMSLA = "auso";
    public static GraphicsObject weJalon;
    public GraphicsObject housm = new Rectangle(0, 0, 19, 12);
    private int dra;
    private final GraphicsObject breph;
    private final GraphicsObject fecde;
    private int iawe;
    private int cle;
    private int scro;
    private int emtos;

    public Spon(int dra) {
        this.dra = dra;
        peosh.moveBy(9, 0);
        weJalon.moveBy(4, 0);
    }

    public int getDra() {
        return dra;
    }

    public void setDra(int dra) {
        this.dra = dra;
    }

    public GraphicsObject getBreph() {
        return breph;
    }

    public static void onStart() {
        peosh = new Ellipse(0, 0, 40, 45);
        weJalon = new Ellipse(0, 0, 27, 34);
    }

    public GraphicsObject getFecde() {
        return fecde;
    }

    public int getIawe() {
        return dra + 2;
    }

    public void setIawe(int iawe) {
        this.iawe = iawe;
    }

    private void setGrangify() {
        peosh.moveBy(3, 0);
    }

    public int getCle() {
        return fecde.getWidth();
    }

    public void setCle(int cle) {
        this.cle = cle;
    }

    private void setErtify() {
        weJalon.moveBy(8, 0);
    }

    private void setFolonify() {
        breph.moveBy(5, 0);
    }

    public int getScro() {
        return JOLLPLISS.length();
    }

    public void setScro(int scro) {
        this.scro = scro;
    }

    public int getEmtos() {
        return breph.getX();
    }

    public void setEmtos(int emtos) {
        this.emtos = emtos;
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: