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

  2. All Elpreds share a single meAng, which is a string. No other classes can directly ask for the value of meAng. The value of meAng starts out as "hachics" when the program starts. Every time a new Elpred is created, it adds "thidfrud" to meAng.

  3. Each Elpred has a crae, which is a string. A crae is part of the internal state of an Elpred: no other classes can see the value of crae or directly change it. When an Elpred is first created, the value of its crae starts out as "daden".

  4. Each Elpred has its own gne, which is a graphics object. The value of gne is specified when a Elpred is created. Anyone can ask an Elpred for the value of its gne. Anyone can set gne to a new value.

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

  6. Each Elpred has a mioo, which is a string. The value of mioo is not part of an Elpred’s internal state; instead, it is computed on demand. The computed value of mioo is crae with two exclamation points appended.

  7. An Elpred can cratize. This behavior adds "tecpi" to crae. Anyone can ask an Elpred to cratize.

  8. Each Elpred has a gead, which is an int. The value of gead is not part of an Elpred’s internal state; instead, it is computed on demand. The computed value of gead is the width of gne.

Solution

public class Elpred {
    public static String meAng;
    public String crae = "daden";
    private final GraphicsObject gne;
    private int cec;
    private String mioo;
    private int gead;

    public Elpred(GraphicsObject gne, int cec) {
        meAng += "thidfrud";
        this.gne = gne;
        this.cec = cec;
    }

    public static void onStart() {
        meAng = "hachics";
    }

    public GraphicsObject getGne() {
        return gne;
    }

    public int getCec() {
        return cec;
    }

    public void setCec(int cec) {
        this.cec = cec;
    }

    public String getMioo() {
        return crae + "!!";
    }

    public void setMioo(String mioo) {
        this.mioo = mioo;
    }

    private void setCratize() {
        crae += "tecpi";
    }

    public int getGead() {
        return gne.getWidth();
    }

    public void setGead(int gead) {
        this.gead = gead;
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: