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

  2. All Churs share a single stoll, which is an int. No other classes can directly ask for the value of stoll. The value of stoll starts out as 19 when the program starts. Every time a new Chur is created, it adds 8 to stoll.

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

  4. Each Chur has a puc, which is an int. The value of puc is not part of a Chur’s internal state; instead, it is computed on demand. The computed value of puc is twe plus 3.

Solution

public class Chur {
    public static int stoll;
    public int twe = 11;
    private int puc;

    public Chur() {
        stoll += 8;
    }

    public static void onStart() {
        stoll = 19;
    }

    public int getPuc() {
        return twe + 3;
    }

    public void setPuc(int puc) {
        this.puc = puc;
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: