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

  2. All Keissobs share a single ISTPRE, which is a graphics object. It is a constant. Its value is a rectangle with a width of 46 and a height of 23. Other classes cannot see its value.

  3. Each Keissob has a icmes, which is an int. An icmes is part of the internal state of a Keissob: no other classes can see the value of icmes or directly change it. When a Keissob is first created, the value of its icmes starts out as 12.

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

  5. Each Keissob has its own tia, which is a graphics object. The value of tia is specified when a Keissob is created. Anyone can ask a Keissob for the value of its tia. The value of tia for a specific Keissob can never change.

  6. A Keissob can grinkify. This behavior moves siCror to the right by 6 pixels (using the moveBy method). Anyone can ask a Keissob to grinkify.

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

  8. A Keissob can phuntify. This behavior adds 6 to icmes. Anyone can ask a Keissob to phuntify.

Solution

public class Keissob {
    public static GraphicsObject ISTPRE = new Rectangle(0, 0, 46, 23);
    public int icmes = 12;
    private final GraphicsObject siCror;
    private GraphicsObject tia;
    private int proui;

    public Keissob(GraphicsObject tia) {
        this.tia = tia;
    }

    public GraphicsObject getSiCror() {
        return siCror;
    }

    public GraphicsObject getTia() {
        return tia;
    }

    public void setTia(GraphicsObject tia) {
        this.tia = tia;
    }

    private void setGrinkify() {
        siCror.moveBy(6, 0);
    }

    public int getProui() {
        return ISTPRE.getWidth();
    }

    public void setProui(int proui) {
        this.proui = proui;
    }

    private void setPhuntify() {
        icmes += 6;
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: