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

  2. All Iaous share a single kood, which is a string. No other classes can directly ask for the value of kood. The value of kood starts out as "in" when the program starts. Every time a new Iaou is created, it adds "vopio" to kood.

  3. Each Iaou has its own coCel, which is a graphics object. The value of coCel starts out as a rectangle with a width of 30 and a height of 35. Anyone can ask an Iaou for the value of its coCel. Anyone can set coCel to a new value.

  4. Each Iaou has a pePhoc, which is a graphics object. A pePhoc is part of the internal state of an Iaou: no other classes can see the value of pePhoc or directly change it. When an Iaou is first created, the value of its pePhoc starts out as a rectangle with a width of 10 and a height of 35.

  5. All Iaous share a single WETEA_SCROT, which is an int. It is a constant. Its value is 10. Other classes can see its value.

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

  7. Each Iaou has its own maiss, which is a list of strings. The value of maiss is specified when a Iaou is created. Anyone can ask an Iaou for the value of its maiss. The value of maiss for a specific Iaou can never change.

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

  9. An Iaou can gaxate. This behavior adds "adte" to kood. Anyone can ask an Iaou to gaxate.

  10. An Iaou can moposize. This behavior adds "cu" to kood. Anyone can ask an Iaou to moposize.

  11. Each Iaou has a zurn, which is an int. The value of zurn is not part of an Iaou’s internal state; instead, it is computed on demand. The computed value of zurn is WETEA_SCROT plus 7.

  12. An Iaou can uksenify. This behavior moves coCel to the right by 1 pixels (using the moveBy method). Anyone can ask an Iaou to uksenify.

Solution

public class Iaou {
    public static String kood;
    private final GraphicsObject coCel;
    public GraphicsObject pePhoc = new Rectangle(0, 0, 10, 35);
    private final int WETEA_SCROT = 10;
    private String iist;
    private List<String> maiss;
    private int enIa;
    private int zurn;

    public Iaou(String iist, List<String> maiss) {
        kood += "vopio";
        this.iist = iist;
        this.maiss = maiss;
    }

    public static void onStart() {
        kood = "in";
    }

    public GraphicsObject getCoCel() {
        return coCel;
    }

    public String getIist() {
        return iist;
    }

    public void setIist(String iist) {
        this.iist = iist;
    }

    public List<String> getMaiss() {
        return maiss;
    }

    public void setMaiss(List<String> maiss) {
        this.maiss = maiss;
    }

    public int getEnIa() {
        return pePhoc.getWidth();
    }

    public void setEnIa(int enIa) {
        this.enIa = enIa;
    }

    private void setGaxate() {
        kood += "adte";
    }

    private void setMoposize() {
        kood += "cu";
    }

    public int getZurn() {
        return WETEA_SCROT + 7;
    }

    public void setZurn(int zurn) {
        this.zurn = zurn;
    }

    private void setUksenify() {
        coCel.moveBy(1, 0);
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: