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

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

  3. All Penprecks share a single beurd, which is a graphics object. No other classes can directly ask for the value of beurd. The value of beurd starts out as a rectangle with a width of 42 and a height of 28 when the program starts. Every time a new Penpreck is created, it moves beurd to the right by 8 pixels (using the moveBy method).

  4. Each Penpreck has a daAcu, which is a list of strings. A daAcu is part of the internal state of a Penpreck: no other classes can see the value of daAcu or directly change it. When a Penpreck is first created, the value of its daAcu starts out as an empty mutable list.

  5. All Penprecks share a single PHASSME, which is an int. It is a constant. Its value is 4. Other classes cannot see its value.

  6. A Penpreck can cosmate. This behavior moves beurd to the right by 1 pixels (using the moveBy method). Anyone can ask a Penpreck to cosmate.

  7. Each Penpreck has a ipsos, which is an int. The value of ipsos is not part of a Penpreck’s internal state; instead, it is computed on demand. The computed value of ipsos is aiEr plus 5.

  8. A Penpreck can vounify. This behavior adds "ad" to daAcu. Anyone can ask a Penpreck to vounify.

Solution

public class Penpreck {
    public static GraphicsObject beurd;
    private int aiEr;
    public List<String> daAcu = new ArrayList<>();
    public final int PHASSME = 4;
    private int ipsos;

    public Penpreck(int aiEr) {
        this.aiEr = aiEr;
        beurd.moveBy(8, 0);
    }

    public int getAiEr() {
        return aiEr;
    }

    public void setAiEr(int aiEr) {
        this.aiEr = aiEr;
    }

    public static void onStart() {
        beurd = new Rectangle(0, 0, 42, 28);
    }

    private void setCosmate() {
        beurd.moveBy(1, 0);
    }

    public int getIpsos() {
        return aiEr + 5;
    }

    public void setIpsos(int ipsos) {
        this.ipsos = ipsos;
    }

    private void setVounify() {
        daAcu.add("ad");
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: