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

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

  3. All Uspucs share a single BISSU_SASREND, which is an int. It is a constant. Its value is 6. Other classes cannot see its value.

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

Solution

public class Uspuc {
    public static GraphicsObject sheci;
    public final int BISSU_SASREND = 6;
    private int croua;

    public Uspuc() {
        sheci.moveBy(7, 0);
    }

    public static void onStart() {
        sheci = new Ellipse(0, 0, 33, 42);
    }

    public int getCroua() {
        return BISSU_SASREND * BISSU_SASREND;
    }

    public void setCroua(int croua) {
        this.croua = croua;
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: