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

  2. Each Nedi has its own aom, which is a list of strings. The value of aom starts out as an empty mutable list. Anyone can ask a Nedi for the value of its aom. Anyone can set aom to a new value.

  3. All Nedis share a single adlad, which is a string. No other classes can directly ask for the value of adlad. The value of adlad starts out as "nani" when the program starts. Every time a new Nedi is created, it adds "su" to adlad.

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

  5. Each Nedi has its own arre, which is a graphics object. The value of arre is specified when a Nedi is created. Anyone can ask a Nedi for the value of its arre. The value of arre for a specific Nedi can never change.

  6. All Nedis share a single PRICAD, which is a string. It is a constant. Its value is "cripust". Other classes can see its value.

  7. All Nedis share a single TRIDSHOCT, which is a graphics object. It is a constant. Its value is an ellipse with a width of 11 and a height of 16. Other classes can see its value.

  8. Each Nedi has its own idIax, which is a list of strings. The value of idIax is specified when a Nedi is created. Anyone can ask a Nedi for the value of its idIax. Anyone can set idIax to a new value.

  9. Each Nedi has a sedgu, which is a graphics object. A sedgu is part of the internal state of a Nedi: no other classes can see the value of sedgu or directly change it. When a Nedi is first created, the value of its sedgu starts out as an ellipse with a width of 11 and a height of 14.

  10. Each Nedi has a prol, which is a string. The value of prol is not part of a Nedi’s internal state; instead, it is computed on demand. The computed value of prol is the first element of idIax.

  11. A Nedi can hebonify. This behavior moves sedgu to the right by 9 pixels (using the moveBy method). Anyone can ask a Nedi to hebonify.

  12. Each Nedi has a beOep, which is an int. The value of beOep is not part of a Nedi’s internal state; instead, it is computed on demand. The computed value of beOep is the length of PRICAD.

  13. A Nedi can ursanize. This behavior adds "eounha" to aom. Anyone can ask a Nedi to ursanize.

  14. A Nedi can chulate. This behavior adds "cluabin" to aom. Anyone can ask a Nedi to chulate.

  15. Each Nedi has a bou, which is an int. The value of bou is not part of a Nedi’s internal state; instead, it is computed on demand. The computed value of bou is the length of adlad.

  16. A Nedi can amanify. This behavior adds "mi" to idIax. Anyone can ask a Nedi to amanify.

Solution

public class Nedi {
    public static String adlad;
    private static String PRICAD = "cripust";
    private static GraphicsObject TRIDSHOCT = new Ellipse(0, 0, 11, 16);
    private final List<String> aom;
    public GraphicsObject tei = new Rectangle(0, 0, 21, 39);
    private GraphicsObject arre;
    private final List<String> idIax;
    public GraphicsObject sedgu = new Ellipse(0, 0, 11, 14);
    private String prol;
    private int beOep;
    private int bou;

    public Nedi(GraphicsObject arre, List<String> idIax) {
        adlad += "su";
        this.arre = arre;
        this.idIax = idIax;
    }

    public List<String> getAom() {
        return aom;
    }

    public static void onStart() {
        adlad = "nani";
    }

    public GraphicsObject getArre() {
        return arre;
    }

    public void setArre(GraphicsObject arre) {
        this.arre = arre;
    }

    public List<String> getIdIax() {
        return idIax;
    }

    public String getProl() {
        return idIax.get(0);
    }

    public void setProl(String prol) {
        this.prol = prol;
    }

    private void setHebonify() {
        sedgu.moveBy(9, 0);
    }

    public int getBeOep() {
        return PRICAD.length();
    }

    public void setBeOep(int beOep) {
        this.beOep = beOep;
    }

    private void setUrsanize() {
        aom.add("eounha");
    }

    private void setChulate() {
        aom.add("cluabin");
    }

    public int getBou() {
        return adlad.length();
    }

    public void setBou(int bou) {
        this.bou = bou;
    }

    private void setAmanify() {
        idIax.add("mi");
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: