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

  2. Each Fashe has a cem, which is a string. A cem is part of the internal state of a Fashe: no other classes can see the value of cem or directly change it. When a Fashe is first created, the value of its cem starts out as "pressco".

  3. Each Fashe has its own peSasm, which is a string. The value of peSasm is specified when a Fashe is created. Anyone can ask a Fashe for the value of its peSasm. The value of peSasm for a specific Fashe can never change.

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

  5. Each Fashe has its own cenga, which is a string. The value of cenga starts out as "me". Anyone can ask a Fashe for the value of its cenga. Anyone can set cenga to a new value.

  6. All Fashes share a single IOMOESS, which is a graphics object. It is a constant. Its value is an ellipse with a width of 44 and a height of 28. Other classes can see its value.

  7. Each Fashe has a diwin, which is a string. The value of diwin is not part of a Fashe’s internal state; instead, it is computed on demand. The computed value of diwin is cem with two exclamation points appended.

  8. A Fashe can iasmify. This behavior adds "reeinch" to cenga. Anyone can ask a Fashe to iasmify.

  9. A Fashe can irmify. This behavior adds "et" to cenga. Anyone can ask a Fashe to irmify.

  10. Each Fashe has a ilvos, which is an int. The value of ilvos is not part of a Fashe’s internal state; instead, it is computed on demand. The computed value of ilvos is the width of IOMOESS.

Solution

public class Fashe {
    public static GraphicsObject encen;
    private static GraphicsObject IOMOESS = new Ellipse(0, 0, 44, 28);
    public String cem = "pressco";
    private String peSasm;
    private final String cenga;
    private String diwin;
    private int ilvos;

    public Fashe(String peSasm) {
        this.peSasm = peSasm;
        encen.moveBy(2, 0);
    }

    public String getPeSasm() {
        return peSasm;
    }

    public void setPeSasm(String peSasm) {
        this.peSasm = peSasm;
    }

    public static void onStart() {
        encen = new Ellipse(0, 0, 45, 16);
    }

    public String getCenga() {
        return cenga;
    }

    public String getDiwin() {
        return cem + "!!";
    }

    public void setDiwin(String diwin) {
        this.diwin = diwin;
    }

    private void setIasmify() {
        cenga += "reeinch";
    }

    private void setIrmify() {
        cenga += "et";
    }

    public int getIlvos() {
        return IOMOESS.getWidth();
    }

    public void setIlvos(int ilvos) {
        this.ilvos = ilvos;
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: