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

  2. Each Ogspit has a noMelac, which is an int. A noMelac is part of the internal state of an Ogspit: no other classes can see the value of noMelac or directly change it. When an Ogspit is first created, the value of its noMelac starts out as 17.

  3. All Ogspits share a single iss, which is a graphics object. No other classes can directly ask for the value of iss. The value of iss starts out as an ellipse with a width of 25 and a height of 33 when the program starts. Every time a new Ogspit is created, it moves iss to the right by 9 pixels (using the moveBy method).

  4. Each Ogspit has a deae, which is an int. The value of deae is not part of an Ogspit’s internal state; instead, it is computed on demand. The computed value of deae is noMelac plus 4.

Solution

public class Ogspit {
    public static GraphicsObject iss;
    public int noMelac = 17;
    private int deae;

    public Ogspit() {
        iss.moveBy(9, 0);
    }

    public static void onStart() {
        iss = new Ellipse(0, 0, 25, 33);
    }

    public int getDeae() {
        return noMelac + 4;
    }

    public void setDeae(int deae) {
        this.deae = deae;
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: