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

  2. Each Usbi has its own asm, which is a string. The value of asm starts out as "ptengbud". Anyone can ask an Usbi for the value of its asm. Anyone can set asm to a new value.

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

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

Solution

public class Usbi {
    private final String asm;
    public int ioOod = 10;
    private int puell;

    public Usbi() {
    }

    public String getAsm() {
        return asm;
    }

    public int getPuell() {
        return ioOod * ioOod;
    }

    public void setPuell(int puell) {
        this.puell = puell;
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: