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

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

  3. All LunSoglons share a single isWhi, which is a string. No other classes can directly ask for the value of isWhi. The value of isWhi starts out as "stil" when the program starts. Every time a new LunSoglon is created, it adds "i" to isWhi.

  4. Each LunSoglon has its own ercis, which is a graphics object. The value of ercis starts out as an ellipse with a width of 44 and a height of 41. Anyone can ask a LunSoglon for the value of its ercis. Anyone can set ercis to a new value.

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

  6. All LunSoglons share a single SESPLA, which is an int. It is a constant. Its value is 17. Other classes can see its value.

  7. Each LunSoglon has a lec, which is an int. The value of lec is not part of a LunSoglon’s internal state; instead, it is computed on demand. The computed value of lec is the x position of ercis.

  8. A LunSoglon can stianate. This behavior moves ercis to the right by 8 pixels (using the moveBy method). Anyone can ask a LunSoglon to stianate.

  9. Each LunSoglon has a fel, which is an int. The value of fel is not part of a LunSoglon’s internal state; instead, it is computed on demand. The computed value of fel is SESPLA plus 3.

  10. A LunSoglon can brenify. This behavior adds "borth" to isWhi. Anyone can ask a LunSoglon to brenify.

Solution

public class LunSoglon {
    public static String isWhi;
    public GraphicsObject jine = new Ellipse(0, 0, 37, 15);
    private final GraphicsObject ercis;
    private GraphicsObject usElcur;
    private final int SESPLA = 17;
    private int lec;
    private int fel;

    public LunSoglon(GraphicsObject usElcur) {
        isWhi += "i";
        this.usElcur = usElcur;
    }

    public static void onStart() {
        isWhi = "stil";
    }

    public GraphicsObject getErcis() {
        return ercis;
    }

    public GraphicsObject getUsElcur() {
        return usElcur;
    }

    public void setUsElcur(GraphicsObject usElcur) {
        this.usElcur = usElcur;
    }

    public int getLec() {
        return ercis.getX();
    }

    public void setLec(int lec) {
        this.lec = lec;
    }

    private void setStianate() {
        ercis.moveBy(8, 0);
    }

    public int getFel() {
        return SESPLA + 3;
    }

    public void setFel(int fel) {
        this.fel = fel;
    }

    private void setBrenify() {
        isWhi += "borth";
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: