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

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

  3. All Sadeds share a single EI_HASHSE, which is a string. It is a constant. Its value is "onfuim". Other classes cannot see its value.

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

  5. Each Saded has a rocme, which is an int. The value of rocme is not part of a Saded’s internal state; instead, it is computed on demand. The computed value of rocme is the width of groon.

  6. A Saded can melcunize. This behavior moves cle to the right by 1 pixels (using the moveBy method). Anyone can ask a Saded to melcunize.

Solution

public class Saded {
    public static String EI_HASHSE = "onfuim";
    public static GraphicsObject cle;
    private GraphicsObject groon;
    private int rocme;

    public Saded(GraphicsObject groon) {
        this.groon = groon;
        cle.moveBy(8, 0);
    }

    public GraphicsObject getGroon() {
        return groon;
    }

    public void setGroon(GraphicsObject groon) {
        this.groon = groon;
    }

    public static void onStart() {
        cle = new Ellipse(0, 0, 31, 44);
    }

    public int getRocme() {
        return groon.getWidth();
    }

    public void setRocme(int rocme) {
        this.rocme = rocme;
    }

    private void setMelcunize() {
        cle.moveBy(1, 0);
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: