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

  2. Each Goid has its own caast, which is a graphics object. The value of caast is specified when a Goid is created. Anyone can ask a Goid for the value of its caast. Anyone can set caast to a new value.

  3. All Goids share a single LI_CIL, which is a graphics object. It is a constant. Its value is a rectangle with a width of 30 and a height of 47. Other classes cannot see its value.

  4. Each Goid has a gigno, which is an int. A gigno is part of the internal state of a Goid: no other classes can see the value of gigno or directly change it. When a Goid is first created, the value of its gigno starts out as 4.

  5. Each Goid has its own diPhed, which is a string. The value of diPhed is specified when a Goid is created. Anyone can ask a Goid for the value of its diPhed. The value of diPhed for a specific Goid can never change.

  6. All Goids share a single pri, which is a string. No other classes can directly ask for the value of pri. The value of pri starts out as "reholl" when the program starts. Every time a new Goid is created, it adds "pra" to pri.

  7. All Goids share a single riCe, which is a list of strings. No other classes can directly ask for the value of riCe. The value of riCe starts out as an empty mutable list when the program starts. Every time a new Goid is created, it adds "ownslist" to riCe.

  8. Each Goid has its own uoss, which is a list of strings. The value of uoss is specified when a Goid is created. Anyone can ask a Goid for the value of its uoss. The value of uoss for a specific Goid can never change.

  9. Each Goid has a soTrire, which is a list of strings. A soTrire is part of the internal state of a Goid: no other classes can see the value of soTrire or directly change it. When a Goid is first created, the value of its soTrire starts out as an empty mutable list.

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

  11. A Goid can invetate. This behavior adds "al" to pri. Anyone can ask a Goid to invetate.

  12. A Goid can trecelate. This behavior adds "ni" to pri. Anyone can ask a Goid to trecelate.

  13. Each Goid has a icrin, which is a string. The value of icrin is not part of a Goid’s internal state; instead, it is computed on demand. The computed value of icrin is the first element of soTrire.

  14. A Goid can vewenate. This behavior adds "iss" to soTrire. Anyone can ask a Goid to vewenate.

  15. Each Goid has a cri, which is an int. The value of cri is not part of a Goid’s internal state; instead, it is computed on demand. The computed value of cri is the x position of LI_CIL.

  16. Each Goid has a miOr, which is a string. The value of miOr is not part of a Goid’s internal state; instead, it is computed on demand. The computed value of miOr is pri with two exclamation points appended.

Solution

public class Goid {
    public static GraphicsObject LI_CIL = new Rectangle(0, 0, 30, 47);
    public static String pri;
    public static List<String> riCe;
    private final GraphicsObject caast;
    public int gigno = 4;
    private String diPhed;
    private List<String> uoss;
    public List<String> soTrire = new ArrayList<>();
    private String deIl;
    private String icrin;
    private int cri;
    private String miOr;

    public Goid(GraphicsObject caast, String diPhed, List<String> uoss) {
        this.caast = caast;
        this.diPhed = diPhed;
        pri += "pra";
        riCe.add("ownslist");
        this.uoss = uoss;
    }

    public GraphicsObject getCaast() {
        return caast;
    }

    public String getDiPhed() {
        return diPhed;
    }

    public void setDiPhed(String diPhed) {
        this.diPhed = diPhed;
    }

    public static void onStart() {
        pri = "reholl";
        riCe = new ArrayList<>();
    }

    public List<String> getUoss() {
        return uoss;
    }

    public void setUoss(List<String> uoss) {
        this.uoss = uoss;
    }

    public String getDeIl() {
        return soTrire.get(0);
    }

    public void setDeIl(String deIl) {
        this.deIl = deIl;
    }

    private void setInvetate() {
        pri += "al";
    }

    private void setTrecelate() {
        pri += "ni";
    }

    public String getIcrin() {
        return soTrire.get(0);
    }

    public void setIcrin(String icrin) {
        this.icrin = icrin;
    }

    private void setVewenate() {
        soTrire.add("iss");
    }

    public int getCri() {
        return LI_CIL.getX();
    }

    public void setCri(int cri) {
        this.cri = cri;
    }

    public String getMiOr() {
        return pri + "!!";
    }

    public void setMiOr(String miOr) {
        this.miOr = miOr;
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: