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

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

  3. Each Ispir has its own puoen, which is an int. The value of puoen starts out as 6. Anyone can ask an Ispir for the value of its puoen. Anyone can set puoen to a new value.

  4. All Ispirs share a single kus, which is an int. No other classes can directly ask for the value of kus. The value of kus starts out as 16 when the program starts. Every time a new Ispir is created, it adds 4 to kus.

  5. Each Ispir has a asu, which is a string. An asu is part of the internal state of an Ispir: no other classes can see the value of asu or directly change it. When an Ispir is first created, the value of its asu starts out as "bi".

  6. All Ispirs share a single ED_JEC, which is a graphics object. It is a constant. Its value is an ellipse with a width of 39 and a height of 14. Other classes can see its value.

  7. An Ispir can cesnanize. This behavior adds "wistran" to asu. Anyone can ask an Ispir to cesnanize.

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

  9. Each Ispir has a pec, which is an int. The value of pec is not part of an Ispir’s internal state; instead, it is computed on demand. The computed value of pec is the x position of anCung.

  10. An Ispir can fiatate. This behavior adds "cossner" to asu. Anyone can ask an Ispir to fiatate.

Solution

public class Ispir {
    public static int kus;
    private static GraphicsObject ED_JEC = new Ellipse(0, 0, 39, 14);
    private GraphicsObject anCung;
    private final int puoen;
    public String asu = "bi";
    private int pepa;
    private int pec;

    public Ispir(GraphicsObject anCung) {
        this.anCung = anCung;
        kus += 4;
    }

    public GraphicsObject getAnCung() {
        return anCung;
    }

    public void setAnCung(GraphicsObject anCung) {
        this.anCung = anCung;
    }

    public int getPuoen() {
        return puoen;
    }

    public static void onStart() {
        kus = 16;
    }

    private void setCesnanize() {
        asu += "wistran";
    }

    public int getPepa() {
        return anCung.getWidth();
    }

    public void setPepa(int pepa) {
        this.pepa = pepa;
    }

    public int getPec() {
        return anCung.getX();
    }

    public void setPec(int pec) {
        this.pec = pec;
    }

    private void setFiatate() {
        asu += "cossner";
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: