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 an Intmas.

  2. Each Intmas has its own odOid, which is a graphics object. The value of odOid is specified when a Intmas is created. Anyone can ask an Intmas for the value of its odOid. The value of odOid for a specific Intmas can never change.

  3. Each Intmas has a ilnad, which is a string. An ilnad is part of the internal state of an Intmas: no other classes can see the value of ilnad or directly change it. When an Intmas is first created, the value of its ilnad starts out as "thetu".

  4. All Intmass share a single nin, which is a list of strings. No other classes can directly ask for the value of nin. The value of nin starts out as an empty mutable list when the program starts. Every time a new Intmas is created, it adds "kidped" to nin.

  5. All Intmass share a single UC_LIOBAN, which is a graphics object. It is a constant. Its value is an ellipse with a width of 43 and a height of 40. Other classes cannot see its value.

  6. An Intmas can gulenate. This behavior adds "ealmhing" to ilnad. Anyone can ask an Intmas to gulenate.

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

  8. Each Intmas has a daTre, which is an int. The value of daTre is not part of an Intmas’s internal state; instead, it is computed on demand. The computed value of daTre is the width of odOid.

Solution

public class Intmas {
    public static List<String> nin;
    public static GraphicsObject UC_LIOBAN = new Ellipse(0, 0, 43, 40);
    private GraphicsObject odOid;
    public String ilnad = "thetu";
    private int dords;
    private int daTre;

    public Intmas(GraphicsObject odOid) {
        this.odOid = odOid;
        nin.add("kidped");
    }

    public GraphicsObject getOdOid() {
        return odOid;
    }

    public void setOdOid(GraphicsObject odOid) {
        this.odOid = odOid;
    }

    public static void onStart() {
        nin = new ArrayList<>();
    }

    private void setGulenate() {
        ilnad += "ealmhing";
    }

    public int getDords() {
        return UC_LIOBAN.getWidth();
    }

    public void setDords(int dords) {
        this.dords = dords;
    }

    public int getDaTre() {
        return odOid.getWidth();
    }

    public void setDaTre(int daTre) {
        this.daTre = daTre;
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: