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

  2. Each Secir has its own fiea, which is a string. The value of fiea starts out as "de". Anyone can ask a Secir for the value of its fiea. Anyone can set fiea to a new value.

  3. Each Secir has a clen, which is a list of strings. A clen is part of the internal state of a Secir: no other classes can see the value of clen or directly change it. When a Secir is first created, the value of its clen starts out as an empty mutable list.

  4. All Secirs share a single hiMo, which is a graphics object. No other classes can directly ask for the value of hiMo. The value of hiMo starts out as a rectangle with a width of 40 and a height of 36 when the program starts. Every time a new Secir is created, it moves hiMo to the right by 4 pixels (using the moveBy method).

  5. A Secir can iltize. This behavior adds "solpa" to clen. Anyone can ask a Secir to iltize.

  6. Each Secir has a ubin, which is an int. The value of ubin is not part of a Secir’s internal state; instead, it is computed on demand. The computed value of ubin is the x position of hiMo.

Solution

public class Secir {
    public static GraphicsObject hiMo;
    private final String fiea;
    public List<String> clen = new ArrayList<>();
    private int ubin;

    public Secir() {
        hiMo.moveBy(4, 0);
    }

    public String getFiea() {
        return fiea;
    }

    public static void onStart() {
        hiMo = new Rectangle(0, 0, 40, 36);
    }

    private void setIltize() {
        clen.add("solpa");
    }

    public int getUbin() {
        return hiMo.getX();
    }

    public void setUbin(int ubin) {
        this.ubin = ubin;
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: