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

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

  3. Each Kint has its own fli, which is a string. The value of fli starts out as "prarthunt". Anyone can ask a Kint for the value of its fli. Anyone can set fli to a new value.

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

  5. All Kints share a single frics, which is an int. No other classes can directly ask for the value of frics. The value of frics starts out as 19 when the program starts. Every time a new Kint is created, it adds 4 to frics.

  6. All Kints share a single PUAL_ANO, which is a graphics object. It is a constant. Its value is an ellipse with a width of 27 and a height of 22. Other classes cannot see its value.

  7. All Kints share a single olEcdi, which is a graphics object. No other classes can directly ask for the value of olEcdi. The value of olEcdi starts out as a rectangle with a width of 43 and a height of 25 when the program starts. Every time a new Kint is created, it moves olEcdi to the right by 7 pixels (using the moveBy method).

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

  9. Each Kint has a rapio, which is a string. The value of rapio is not part of a Kint’s internal state; instead, it is computed on demand. The computed value of rapio is the first element of ninse.

  10. A Kint can dibatate. This behavior adds "banil" to fli. Anyone can ask a Kint to dibatate.

  11. A Kint can hanawize. This behavior adds 1 to frics. Anyone can ask a Kint to hanawize.

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

  13. Each Kint has a esChod, which is an int. The value of esChod is not part of a Kint’s internal state; instead, it is computed on demand. The computed value of esChod is the width of olEcdi.

  14. A Kint can peserate. This behavior adds "glasta" to fli. Anyone can ask a Kint to peserate.

Solution

public class Kint {
    public static int frics;
    public static GraphicsObject PUAL_ANO = new Ellipse(0, 0, 27, 22);
    public static GraphicsObject olEcdi;
    public List<String> tosm = new ArrayList<>();
    private final String fli;
    private List<String> ninse;
    private GraphicsObject raed;
    private String rapio;
    private int pha;
    private int esChod;

    public Kint(List<String> ninse, GraphicsObject raed) {
        this.ninse = ninse;
        frics += 4;
        olEcdi.moveBy(7, 0);
        this.raed = raed;
    }

    public String getFli() {
        return fli;
    }

    public List<String> getNinse() {
        return ninse;
    }

    public void setNinse(List<String> ninse) {
        this.ninse = ninse;
    }

    public static void onStart() {
        frics = 19;
        olEcdi = new Rectangle(0, 0, 43, 25);
    }

    public GraphicsObject getRaed() {
        return raed;
    }

    public void setRaed(GraphicsObject raed) {
        this.raed = raed;
    }

    public String getRapio() {
        return ninse.get(0);
    }

    public void setRapio(String rapio) {
        this.rapio = rapio;
    }

    private void setDibatate() {
        fli += "banil";
    }

    private void setHanawize() {
        frics += 1;
    }

    public int getPha() {
        return ninse.size();
    }

    public void setPha(int pha) {
        this.pha = pha;
    }

    public int getEsChod() {
        return olEcdi.getWidth();
    }

    public void setEsChod(int esChod) {
        this.esChod = esChod;
    }

    private void setPeserate() {
        fli += "glasta";
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: