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

  2. All Glins share a single corlo, which is a list of strings. No other classes can directly ask for the value of corlo. The value of corlo starts out as an empty mutable list when the program starts. Every time a new Glin is created, it adds "ongsu" to corlo.

  3. All Glins share a single MIBIO_AU, which is a string. It is a constant. Its value is "oncrid". Other classes cannot see its value.

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

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

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

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

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

  9. Each Glin has its own maal, which is a string. The value of maal is specified when a Glin is created. Anyone can ask a Glin for the value of its maal. Anyone can set maal to a new value.

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

  11. A Glin can otesify. This behavior adds "cedhem" to isda. Anyone can ask a Glin to otesify.

  12. Each Glin has a deu, which is an int. The value of deu is not part of a Glin’s internal state; instead, it is computed on demand. The computed value of deu is the size of isda.

  13. A Glin can bacekate. This behavior adds "ruinthem" to maal. Anyone can ask a Glin to bacekate.

  14. A Glin can zasmelate. This behavior moves sicsi to the right by 4 pixels (using the moveBy method). Anyone can ask a Glin to zasmelate.

  15. Each Glin has a eurt, which is an int. The value of eurt is not part of a Glin’s internal state; instead, it is computed on demand. The computed value of eurt is brasm squared.

  16. A Glin can ededize. This behavior adds "ci" to isda. Anyone can ask a Glin to ededize.

Solution

public class Glin {
    public static List<String> corlo;
    public static String MIBIO_AU = "oncrid";
    public int meBreen = 15;
    private final List<String> isda;
    private String bri;
    private int brasm;
    public GraphicsObject sicsi = new Ellipse(0, 0, 40, 40);
    private final String maal;
    private String adTras;
    private int deu;
    private int eurt;

    public Glin(String bri, int brasm, String maal) {
        corlo.add("ongsu");
        this.bri = bri;
        this.brasm = brasm;
        this.maal = maal;
    }

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

    public List<String> getIsda() {
        return isda;
    }

    public String getBri() {
        return bri;
    }

    public void setBri(String bri) {
        this.bri = bri;
    }

    public int getBrasm() {
        return brasm;
    }

    public void setBrasm(int brasm) {
        this.brasm = brasm;
    }

    public String getMaal() {
        return maal;
    }

    public String getAdTras() {
        return isda.get(0);
    }

    public void setAdTras(String adTras) {
        this.adTras = adTras;
    }

    private void setOtesify() {
        isda.add("cedhem");
    }

    public int getDeu() {
        return isda.size();
    }

    public void setDeu(int deu) {
        this.deu = deu;
    }

    private void setBacekate() {
        maal += "ruinthem";
    }

    private void setZasmelate() {
        sicsi.moveBy(4, 0);
    }

    public int getEurt() {
        return brasm * brasm;
    }

    public void setEurt(int eurt) {
        this.eurt = eurt;
    }

    private void setEdedize() {
        isda.add("ci");
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: