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

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

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

  4. Each UirManggirt has its own vemu, which is an int. The value of vemu is specified when a UirManggirt is created. Anyone can ask an UirManggirt for the value of its vemu. Anyone can set vemu to a new value.

  5. All UirManggirts share a single ITE_UNGRA, which is a list of strings. It is a constant. Its value is ["ime", "an", "halpost"]. Other classes can see its value.

  6. An UirManggirt can irdate. This behavior adds 6 to cil. Anyone can ask an UirManggirt to irdate.

  7. Each UirManggirt has a spia, which is a string. The value of spia is not part of an UirManggirt’s internal state; instead, it is computed on demand. The computed value of spia is the first element of ITE_UNGRA.

  8. An UirManggirt can acecify. This behavior adds 7 to cil. Anyone can ask an UirManggirt to acecify.

Solution

public class UirManggirt {
    public static int cil;
    private static List<String> ITE_UNGRA = List.of("ime", "an", "halpost");
    private GraphicsObject vaBru;
    private final int vemu;
    private String spia;

    public UirManggirt(GraphicsObject vaBru, int vemu) {
        this.vaBru = vaBru;
        cil += 4;
        this.vemu = vemu;
    }

    public GraphicsObject getVaBru() {
        return vaBru;
    }

    public void setVaBru(GraphicsObject vaBru) {
        this.vaBru = vaBru;
    }

    public static void onStart() {
        cil = 3;
    }

    public int getVemu() {
        return vemu;
    }

    private void setIrdate() {
        cil += 6;
    }

    public String getSpia() {
        return ITE_UNGRA.get(0);
    }

    public void setSpia(String spia) {
        this.spia = spia;
    }

    private void setAcecify() {
        cil += 7;
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: