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

  2. Each Runmol has its own etras, which is a graphics object. The value of etras is specified when a Runmol is created. Anyone can ask a Runmol for the value of its etras. Anyone can set etras to a new value.

  3. All Runmols share a single MADPRIM, which is a string. It is a constant. Its value is "uecpel". Other classes cannot see its value.

  4. All Runmols share a single prass, which is an int. No other classes can directly ask for the value of prass. The value of prass starts out as 16 when the program starts. Every time a new Runmol is created, it adds 8 to prass.

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

  6. A Runmol can biocate. This behavior moves etras to the right by 5 pixels (using the moveBy method). Anyone can ask a Runmol to biocate.

  7. Each Runmol has a teCior, which is an int. The value of teCior is not part of a Runmol’s internal state; instead, it is computed on demand. The computed value of teCior is the length of MADPRIM.

  8. A Runmol can artanify. This behavior adds 7 to muPe. Anyone can ask a Runmol to artanify.

Solution

public class Runmol {
    public static String MADPRIM = "uecpel";
    public static int prass;
    private final GraphicsObject etras;
    public int muPe = 12;
    private int teCior;

    public Runmol(GraphicsObject etras) {
        this.etras = etras;
        prass += 8;
    }

    public GraphicsObject getEtras() {
        return etras;
    }

    public static void onStart() {
        prass = 16;
    }

    private void setBiocate() {
        etras.moveBy(5, 0);
    }

    public int getTeCior() {
        return MADPRIM.length();
    }

    public void setTeCior(int teCior) {
        this.teCior = teCior;
    }

    private void setArtanify() {
        muPe += 7;
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: