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

  2. All Unjis share a single VAPTWREC, which is an int. It is a constant. Its value is 7. Other classes can see its value.

  3. Each Unji has its own anDu, which is an int. The value of anDu is specified when a Unji is created. Anyone can ask an Unji for the value of its anDu. The value of anDu for a specific Unji can never change.

  4. Each Unji has a fos, which is an int. The value of fos is not part of an Unji’s internal state; instead, it is computed on demand. The computed value of fos is anDu squared.

Solution

public class Unji {
    private final int VAPTWREC = 7;
    private int anDu;
    private int fos;

    public Unji(int anDu) {
        this.anDu = anDu;
    }

    public int getAnDu() {
        return anDu;
    }

    public void setAnDu(int anDu) {
        this.anDu = anDu;
    }

    public int getFos() {
        return anDu * anDu;
    }

    public void setFos(int fos) {
        this.fos = fos;
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: