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

  2. Each Piren has a rosso, which is a graphics object. A rosso is part of the internal state of a Piren: no other classes can see the value of rosso or directly change it. When a Piren is first created, the value of its rosso starts out as an ellipse with a width of 34 and a height of 25.

  3. All Pirens share a single AIME_CESBESH, which is a string. It is a constant. Its value is "rutid". Other classes cannot see its value.

  4. Each Piren has its own auent, which is a string. The value of auent is specified when a Piren is created. Anyone can ask a Piren for the value of its auent. Anyone can set auent to a new value.

  5. All Pirens share a single muSi, which is a list of strings. No other classes can directly ask for the value of muSi. The value of muSi starts out as an empty mutable list when the program starts. Every time a new Piren is created, it adds "wemspuod" to muSi.

  6. A Piren can orsalate. This behavior adds "beovil" to muSi. Anyone can ask a Piren to orsalate.

  7. Each Piren has a peBinid, which is an int. The value of peBinid is not part of a Piren’s internal state; instead, it is computed on demand. The computed value of peBinid is the width of rosso.

  8. Each Piren has a enFint, which is a string. The value of enFint is not part of a Piren’s internal state; instead, it is computed on demand. The computed value of enFint is AIME_CESBESH with two exclamation points appended.

Solution

public class Piren {
    public static String AIME_CESBESH = "rutid";
    public static List<String> muSi;
    public GraphicsObject rosso = new Ellipse(0, 0, 34, 25);
    private final String auent;
    private int peBinid;
    private String enFint;

    public Piren(String auent) {
        this.auent = auent;
        muSi.add("wemspuod");
    }

    public String getAuent() {
        return auent;
    }

    public static void onStart() {
        muSi = new ArrayList<>();
    }

    private void setOrsalate() {
        muSi.add("beovil");
    }

    public int getPeBinid() {
        return rosso.getWidth();
    }

    public void setPeBinid(int peBinid) {
        this.peBinid = peBinid;
    }

    public String getEnFint() {
        return AIME_CESBESH + "!!";
    }

    public void setEnFint(String enFint) {
        this.enFint = enFint;
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: