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

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

Solution

public class Litriac {
    public static int oinsu;

    public Litriac() {
        oinsu += 2;
    }

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

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: