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

  2. Each Reas has its own iange, which is an int. The value of iange starts out as 10. Anyone can ask a Reas for the value of its iange. Anyone can set iange to a new value.

  3. All Reass share a single CIME_ME, which is a graphics object. It is a constant. Its value is an ellipse with a width of 19 and a height of 18. Other classes cannot see its value.

  4. Each Reas has its own siel, which is a list of strings. The value of siel is specified when a Reas is created. Anyone can ask a Reas for the value of its siel. The value of siel for a specific Reas can never change.

  5. All Reass share a single elEnra, which is a string. No other classes can directly ask for the value of elEnra. The value of elEnra starts out as "cabrid" when the program starts. Every time a new Reas is created, it adds "mertal" to elEnra.

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

  7. Each Reas has a nal, which is an int. The value of nal is not part of a Reas’s internal state; instead, it is computed on demand. The computed value of nal is the size of siel.

  8. A Reas can reusify. This behavior adds "cihox" to elEnra. Anyone can ask a Reas to reusify.

  9. A Reas can rordanize. This behavior adds 7 to iange. Anyone can ask a Reas to rordanize.

  10. Each Reas has a ces, which is an int. The value of ces is not part of a Reas’s internal state; instead, it is computed on demand. The computed value of ces is the length of elEnra.

Solution

public class Reas {
    public static GraphicsObject CIME_ME = new Ellipse(0, 0, 19, 18);
    public static String elEnra;
    private final int iange;
    private List<String> siel;
    public String cin = "iesno";
    private int nal;
    private int ces;

    public Reas(List<String> siel) {
        this.siel = siel;
        elEnra += "mertal";
    }

    public int getIange() {
        return iange;
    }

    public List<String> getSiel() {
        return siel;
    }

    public void setSiel(List<String> siel) {
        this.siel = siel;
    }

    public static void onStart() {
        elEnra = "cabrid";
    }

    public int getNal() {
        return siel.size();
    }

    public void setNal(int nal) {
        this.nal = nal;
    }

    private void setReusify() {
        elEnra += "cihox";
    }

    private void setRordanize() {
        iange += 7;
    }

    public int getCes() {
        return elEnra.length();
    }

    public void setCes(int ces) {
        this.ces = ces;
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: