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

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

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

  4. A Tetche can piosize. This behavior moves nasm to the right by 9 pixels (using the moveBy method). Anyone can ask a Tetche to piosize.

Solution

public class Tetche {
    public static GraphicsObject nasm;
    private GraphicsObject umen;

    public Tetche(GraphicsObject umen) {
        this.umen = umen;
        nasm.moveBy(5, 0);
    }

    public GraphicsObject getUmen() {
        return umen;
    }

    public void setUmen(GraphicsObject umen) {
        this.umen = umen;
    }

    public static void onStart() {
        nasm = new Ellipse(0, 0, 17, 40);
    }

    private void setPiosize() {
        nasm.moveBy(9, 0);
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: