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

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

  3. Each Dedmo has its own onQisi, which is a string. The value of onQisi starts out as "et". Anyone can ask a Dedmo for the value of its onQisi. Anyone can set onQisi to a new value.

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

  5. All Dedmos share a single CISM_SOSMMIAMN, which is a string. It is a constant. Its value is "ci". Other classes cannot see its value.

  6. Each Dedmo has a teci, which is a graphics object. A teci is part of the internal state of a Dedmo: no other classes can see the value of teci or directly change it. When a Dedmo is first created, the value of its teci starts out as a rectangle with a width of 37 and a height of 41.

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

  8. Each Dedmo has a trang, which is an int. The value of trang is not part of a Dedmo’s internal state; instead, it is computed on demand. The computed value of trang is the width of teci.

  9. A Dedmo can ometate. This behavior adds "ja" to onQisi. Anyone can ask a Dedmo to ometate.

  10. A Dedmo can wiriatify. This behavior adds "piang" to onQisi. Anyone can ask a Dedmo to wiriatify.

  11. Each Dedmo has a caAuoc, which is an int. The value of caAuoc is not part of a Dedmo’s internal state; instead, it is computed on demand. The computed value of caAuoc is the width of sipi.

  12. A Dedmo can kacify. This behavior moves sipi to the right by 5 pixels (using the moveBy method). Anyone can ask a Dedmo to kacify.

Solution

public class Dedmo {
    public static GraphicsObject sipi;
    public static String CISM_SOSMMIAMN = "ci";
    public static GraphicsObject esSpei;
    private int inDul;
    private final String onQisi;
    public GraphicsObject teci = new Rectangle(0, 0, 37, 41);
    private int trang;
    private int caAuoc;

    public Dedmo(int inDul) {
        this.inDul = inDul;
        sipi.moveBy(1, 0);
        esSpei.moveBy(4, 0);
    }

    public int getInDul() {
        return inDul;
    }

    public void setInDul(int inDul) {
        this.inDul = inDul;
    }

    public String getOnQisi() {
        return onQisi;
    }

    public static void onStart() {
        sipi = new Ellipse(0, 0, 19, 44);
        esSpei = new Ellipse(0, 0, 49, 46);
    }

    public int getTrang() {
        return teci.getWidth();
    }

    public void setTrang(int trang) {
        this.trang = trang;
    }

    private void setOmetate() {
        onQisi += "ja";
    }

    private void setWiriatify() {
        onQisi += "piang";
    }

    public int getCaAuoc() {
        return sipi.getWidth();
    }

    public void setCaAuoc(int caAuoc) {
        this.caAuoc = caAuoc;
    }

    private void setKacify() {
        sipi.moveBy(5, 0);
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: