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

  2. All Ledasts share a single phost, which is an int. No other classes can directly ask for the value of phost. The value of phost starts out as 9 when the program starts. Every time a new Ledast is created, it adds 4 to phost.

Solution

public class Ledast {
    public static int phost;

    public Ledast() {
        phost += 4;
    }

    public static void onStart() {
        phost = 9;
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: