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

  2. All Lutecs share a single MASUN_AUIL, which is a graphics object. It is a constant. Its value is an ellipse with a width of 36 and a height of 17. Other classes can see its value.

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

  4. Each Lutec has its own madil, which is a graphics object. The value of madil starts out as an ellipse with a width of 32 and a height of 18. Anyone can ask a Lutec for the value of its madil. Anyone can set madil to a new value.

  5. All Lutecs share a single okren, which is a graphics object. No other classes can directly ask for the value of okren. The value of okren starts out as a rectangle with a width of 17 and a height of 44 when the program starts. Every time a new Lutec is created, it moves okren to the right by 5 pixels (using the moveBy method).

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

  7. Each Lutec has a senid, which is an int. The value of senid is not part of a Lutec’s internal state; instead, it is computed on demand. The computed value of senid is the x position of madil.

  8. A Lutec can heormize. This behavior moves okren to the right by 5 pixels (using the moveBy method). Anyone can ask a Lutec to heormize.

  9. Each Lutec has a weOg, which is an int. The value of weOg is not part of a Lutec’s internal state; instead, it is computed on demand. The computed value of weOg is the size of puBe.

  10. A Lutec can tririze. This behavior moves madil to the right by 1 pixels (using the moveBy method). Anyone can ask a Lutec to tririze.

Solution

public class Lutec {
    private static GraphicsObject MASUN_AUIL = new Ellipse(0, 0, 36, 17);
    public static GraphicsObject okren;
    private int lil;
    private final GraphicsObject madil;
    public List<String> puBe = new ArrayList<>();
    private int senid;
    private int weOg;

    public Lutec(int lil) {
        this.lil = lil;
        okren.moveBy(5, 0);
    }

    public int getLil() {
        return lil;
    }

    public void setLil(int lil) {
        this.lil = lil;
    }

    public GraphicsObject getMadil() {
        return madil;
    }

    public static void onStart() {
        okren = new Rectangle(0, 0, 17, 44);
    }

    public int getSenid() {
        return madil.getX();
    }

    public void setSenid(int senid) {
        this.senid = senid;
    }

    private void setHeormize() {
        okren.moveBy(5, 0);
    }

    public int getWeOg() {
        return puBe.size();
    }

    public void setWeOg(int weOg) {
        this.weOg = weOg;
    }

    private void setTririze() {
        madil.moveBy(1, 0);
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: