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

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

  3. Each Etiad has its own zin, which is a string. The value of zin is specified when a Etiad is created. Anyone can ask an Etiad for the value of its zin. Anyone can set zin to a new value.

  4. All Etiads share a single AL_FRES, which is a list of strings. It is a constant. Its value is ["ded", "cilqent", "ilphess"]. Other classes cannot see its value.

  5. All Etiads share a single cepi, which is a list of strings. No other classes can directly ask for the value of cepi. The value of cepi starts out as an empty mutable list when the program starts. Every time a new Etiad is created, it adds "pi" to cepi.

  6. Each Etiad has a trafa, which is a graphics object. A trafa is part of the internal state of an Etiad: no other classes can see the value of trafa or directly change it. When an Etiad is first created, the value of its trafa starts out as an ellipse with a width of 35 and a height of 49.

  7. Each Etiad has its own dast, which is a graphics object. The value of dast is specified when a Etiad is created. Anyone can ask an Etiad for the value of its dast. The value of dast for a specific Etiad can never change.

  8. Each Etiad has a paSi, which is an int. The value of paSi is not part of an Etiad’s internal state; instead, it is computed on demand. The computed value of paSi is the x position of dast.

  9. An Etiad can folanate. This behavior adds "on" to zin. Anyone can ask an Etiad to folanate.

  10. An Etiad can ilestify. This behavior moves trafa to the right by 5 pixels (using the moveBy method). Anyone can ask an Etiad to ilestify.

  11. Each Etiad has a erra, which is a string. The value of erra is not part of an Etiad’s internal state; instead, it is computed on demand. The computed value of erra is the first element of foUea.

  12. Each Etiad has a eeba, which is a string. The value of eeba is not part of an Etiad’s internal state; instead, it is computed on demand. The computed value of eeba is zin with two exclamation points appended.

Solution

public class Etiad {
    public static List<String> AL_FRES = List.of("ded", "cilqent", "ilphess");
    public static List<String> cepi;
    private List<String> foUea;
    private final String zin;
    public GraphicsObject trafa = new Ellipse(0, 0, 35, 49);
    private GraphicsObject dast;
    private int paSi;
    private String erra;
    private String eeba;

    public Etiad(List<String> foUea, String zin, GraphicsObject dast) {
        this.foUea = foUea;
        this.zin = zin;
        cepi.add("pi");
        this.dast = dast;
    }

    public List<String> getFoUea() {
        return foUea;
    }

    public void setFoUea(List<String> foUea) {
        this.foUea = foUea;
    }

    public String getZin() {
        return zin;
    }

    public static void onStart() {
        cepi = new ArrayList<>();
    }

    public GraphicsObject getDast() {
        return dast;
    }

    public void setDast(GraphicsObject dast) {
        this.dast = dast;
    }

    public int getPaSi() {
        return dast.getX();
    }

    public void setPaSi(int paSi) {
        this.paSi = paSi;
    }

    private void setFolanate() {
        zin += "on";
    }

    private void setIlestify() {
        trafa.moveBy(5, 0);
    }

    public String getErra() {
        return foUea.get(0);
    }

    public void setErra(String erra) {
        this.erra = erra;
    }

    public String getEeba() {
        return zin + "!!";
    }

    public void setEeba(String eeba) {
        this.eeba = eeba;
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: