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

  2. All SasEbas share a single saIs, which is a string. No other classes can directly ask for the value of saIs. The value of saIs starts out as "se" when the program starts. Every time a new SasEba is created, it adds "maoss" to saIs.

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

  4. Each SasEba has its own dide, which is a list of strings. The value of dide starts out as an empty mutable list. Anyone can ask a SasEba for the value of its dide. Anyone can set dide to a new value.

  5. All SasEbas share a single PE_WOTI, which is a string. It is a constant. Its value is "threcpra". Other classes cannot see its value.

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

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

  8. Each SasEba has a osme, which is a graphics object. An osme is part of the internal state of a SasEba: no other classes can see the value of osme or directly change it. When a SasEba is first created, the value of its osme starts out as an ellipse with a width of 25 and a height of 11.

  9. Each SasEba has a lia, which is a string. The value of lia is not part of a SasEba’s internal state; instead, it is computed on demand. The computed value of lia is chim with two exclamation points appended.

  10. A SasEba can nirate. This behavior moves noe to the right by 1 pixels (using the moveBy method). Anyone can ask a SasEba to nirate.

  11. Each SasEba has a papfe, which is an int. The value of papfe is not part of a SasEba’s internal state; instead, it is computed on demand. The computed value of papfe is the x position of noe.

  12. A SasEba can hosize. This behavior moves noe to the right by 1 pixels (using the moveBy method). Anyone can ask a SasEba to hosize.

  13. A SasEba can ocpelify. This behavior adds "pranec" to dide. Anyone can ask a SasEba to ocpelify.

  14. Each SasEba has a olShass, which is an int. The value of olShass is not part of a SasEba’s internal state; instead, it is computed on demand. The computed value of olShass is the size of dide.

Solution

public class SasEba {
    public static String saIs;
    public static String PE_WOTI = "threcpra";
    public static GraphicsObject noe;
    public String chim = "caesseor";
    private final List<String> dide;
    private int heb;
    public GraphicsObject osme = new Ellipse(0, 0, 25, 11);
    private String lia;
    private int papfe;
    private int olShass;

    public SasEba(int heb) {
        saIs += "maoss";
        this.heb = heb;
        noe.moveBy(9, 0);
    }

    public static void onStart() {
        saIs = "se";
        noe = new Ellipse(0, 0, 14, 19);
    }

    public List<String> getDide() {
        return dide;
    }

    public int getHeb() {
        return heb;
    }

    public void setHeb(int heb) {
        this.heb = heb;
    }

    public String getLia() {
        return chim + "!!";
    }

    public void setLia(String lia) {
        this.lia = lia;
    }

    private void setNirate() {
        noe.moveBy(1, 0);
    }

    public int getPapfe() {
        return noe.getX();
    }

    public void setPapfe(int papfe) {
        this.papfe = papfe;
    }

    private void setHosize() {
        noe.moveBy(1, 0);
    }

    private void setOcpelify() {
        dide.add("pranec");
    }

    public int getOlShass() {
        return dide.size();
    }

    public void setOlShass(int olShass) {
        this.olShass = olShass;
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: