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

  2. Each Tilta has its own soZam, which is a graphics object. The value of soZam is specified when a Tilta is created. Anyone can ask a Tilta for the value of its soZam. Anyone can set soZam to a new value.

  3. All Tiltas share a single HEANHIS, which is an int. It is a constant. Its value is 11. Other classes cannot see its value.

  4. Each Tilta has a tia, which is an int. The value of tia is not part of a Tilta’s internal state; instead, it is computed on demand. The computed value of tia is HEANHIS squared.

Solution

public class Tilta {
    private final GraphicsObject soZam;
    public final int HEANHIS = 11;
    private int tia;

    public Tilta(GraphicsObject soZam) {
        this.soZam = soZam;
    }

    public GraphicsObject getSoZam() {
        return soZam;
    }

    public int getTia() {
        return HEANHIS * HEANHIS;
    }

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

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: