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

  2. All Phals share a single OURSI_BII, which is a string. It is a constant. Its value is "a". Other classes can see its value.

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

  4. Each Phal has its own oaIght, which is an int. The value of oaIght starts out as 16. Anyone can ask a Phal for the value of its oaIght. Anyone can set oaIght to a new value.

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

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

  7. All Phals share a single IBI_NER, which is a graphics object. It is a constant. Its value is an ellipse with a width of 41 and a height of 11. Other classes cannot see its value.

  8. A Phal can ialelate. This behavior adds "vopiss" to ceVoism. Anyone can ask a Phal to ialelate.

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

  10. Each Phal has a gaja, which is an int. The value of gaja is not part of a Phal’s internal state; instead, it is computed on demand. The computed value of gaja is oaIght plus 2.

  11. A Phal can oucidify. This behavior moves addur to the right by 9 pixels (using the moveBy method). Anyone can ask a Phal to oucidify.

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

Solution

public class Phal {
    private static String OURSI_BII = "a";
    public static GraphicsObject addur;
    public static GraphicsObject IBI_NER = new Ellipse(0, 0, 41, 11);
    public String ceVoism = "monphlion";
    private final int oaIght;
    private int apTastu;
    private String inCu;
    private int gaja;
    private int drei;

    public Phal(int apTastu) {
        this.apTastu = apTastu;
        addur.moveBy(5, 0);
    }

    public int getOaIght() {
        return oaIght;
    }

    public int getApTastu() {
        return apTastu;
    }

    public void setApTastu(int apTastu) {
        this.apTastu = apTastu;
    }

    public static void onStart() {
        addur = new Ellipse(0, 0, 26, 30);
    }

    private void setIalelate() {
        ceVoism += "vopiss";
    }

    public String getInCu() {
        return OURSI_BII + "!!";
    }

    public void setInCu(String inCu) {
        this.inCu = inCu;
    }

    public int getGaja() {
        return oaIght + 2;
    }

    public void setGaja(int gaja) {
        this.gaja = gaja;
    }

    private void setOucidify() {
        addur.moveBy(9, 0);
    }

    public int getDrei() {
        return oaIght * oaIght;
    }

    public void setDrei(int drei) {
        this.drei = drei;
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: