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

  2. Each Rardo has a iss, which is an int. An iss is part of the internal state of a Rardo: no other classes can see the value of iss or directly change it. When a Rardo is first created, the value of its iss starts out as 3.

  3. All Rardos share a single AA_VEC, which is a string. It is a constant. Its value is "becneat". Other classes can see its value.

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

  5. All Rardos share a single buEclar, which is a graphics object. No other classes can directly ask for the value of buEclar. The value of buEclar starts out as an ellipse with a width of 26 and a height of 20 when the program starts. Every time a new Rardo is created, it moves buEclar to the right by 2 pixels (using the moveBy method).

  6. Each Rardo has a erir, which is an int. The value of erir is not part of a Rardo’s internal state; instead, it is computed on demand. The computed value of erir is iss squared.

  7. A Rardo can frienate. This behavior adds 8 to iss. Anyone can ask a Rardo to frienate.

  8. A Rardo can chrosize. This behavior adds 7 to iss. Anyone can ask a Rardo to chrosize.

Solution

public class Rardo {
    private static String AA_VEC = "becneat";
    public static GraphicsObject buEclar;
    public int iss = 3;
    private GraphicsObject veJo;
    private int erir;

    public Rardo(GraphicsObject veJo) {
        this.veJo = veJo;
        buEclar.moveBy(2, 0);
    }

    public GraphicsObject getVeJo() {
        return veJo;
    }

    public void setVeJo(GraphicsObject veJo) {
        this.veJo = veJo;
    }

    public static void onStart() {
        buEclar = new Ellipse(0, 0, 26, 20);
    }

    public int getErir() {
        return iss * iss;
    }

    public void setErir(int erir) {
        this.erir = erir;
    }

    private void setFrienate() {
        iss += 8;
    }

    private void setChrosize() {
        iss += 7;
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: