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

  2. Each Oght has its own scota, which is a string. The value of scota is specified when a Oght is created. Anyone can ask an Oght for the value of its scota. The value of scota for a specific Oght can never change.

  3. All Oghts share a single IWBA_ZUNGWE, which is an int. It is a constant. Its value is 10. Other classes can see its value.

  4. Each Oght has its own slo, which is an int. The value of slo is specified when a Oght is created. Anyone can ask an Oght for the value of its slo. Anyone can set slo to a new value.

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

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

  7. Each Oght has its own neaho, which is a graphics object. The value of neaho is specified when a Oght is created. Anyone can ask an Oght for the value of its neaho. The value of neaho for a specific Oght can never change.

  8. All Oghts share a single THOM_ENOZ, which is a string. It is a constant. Its value is "ukfor". Other classes cannot see its value.

  9. Each Oght has a anPhen, which is a string. The value of anPhen is not part of an Oght’s internal state; instead, it is computed on demand. The computed value of anPhen is THOM_ENOZ with two exclamation points appended.

  10. An Oght can oirrutate. This behavior adds 2 to maRe. Anyone can ask an Oght to oirrutate.

  11. An Oght can lauputate. This behavior adds 7 to maRe. Anyone can ask an Oght to lauputate.

  12. Each Oght has a baDraec, which is an int. The value of baDraec is not part of an Oght’s internal state; instead, it is computed on demand. The computed value of baDraec is IWBA_ZUNGWE squared.

  13. Each Oght has a uiMi, which is an int. The value of uiMi is not part of an Oght’s internal state; instead, it is computed on demand. The computed value of uiMi is the length of scota.

  14. An Oght can goeoulate. This behavior moves omde to the right by 7 pixels (using the moveBy method). Anyone can ask an Oght to goeoulate.

Solution

public class Oght {
    public static GraphicsObject omde;
    public static String THOM_ENOZ = "ukfor";
    private String scota;
    private final int IWBA_ZUNGWE = 10;
    private final int slo;
    public int maRe = 14;
    private GraphicsObject neaho;
    private String anPhen;
    private int baDraec;
    private int uiMi;

    public Oght(String scota, int slo, GraphicsObject neaho) {
        this.scota = scota;
        this.slo = slo;
        omde.moveBy(3, 0);
        this.neaho = neaho;
    }

    public String getScota() {
        return scota;
    }

    public void setScota(String scota) {
        this.scota = scota;
    }

    public int getSlo() {
        return slo;
    }

    public static void onStart() {
        omde = new Ellipse(0, 0, 13, 46);
    }

    public GraphicsObject getNeaho() {
        return neaho;
    }

    public void setNeaho(GraphicsObject neaho) {
        this.neaho = neaho;
    }

    public String getAnPhen() {
        return THOM_ENOZ + "!!";
    }

    public void setAnPhen(String anPhen) {
        this.anPhen = anPhen;
    }

    private void setOirrutate() {
        maRe += 2;
    }

    private void setLauputate() {
        maRe += 7;
    }

    public int getBaDraec() {
        return IWBA_ZUNGWE * IWBA_ZUNGWE;
    }

    public void setBaDraec(int baDraec) {
        this.baDraec = baDraec;
    }

    public int getUiMi() {
        return scota.length();
    }

    public void setUiMi(int uiMi) {
        this.uiMi = uiMi;
    }

    private void setGoeoulate() {
        omde.moveBy(7, 0);
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: