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

  2. All Scrus share a single rart, which is a graphics object. No other classes can directly ask for the value of rart. The value of rart starts out as an ellipse with a width of 37 and a height of 19 when the program starts. Every time a new Scru is created, it moves rart to the right by 4 pixels (using the moveBy method).

Solution

public class Scru {
    public static GraphicsObject rart;

    public Scru() {
        rart.moveBy(4, 0);
    }

    public static void onStart() {
        rart = new Ellipse(0, 0, 37, 19);
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: