2. This question involves the GameSpinner class, which simulates a game spinner with a specific number of sectors. You will write the complete GameSpinner class, which contains a constructor and two methods.
GameSpinner objects are created by calls to a constructor with one parameter.
The parameter is an int that represents the number of sectors on the spinner. (Precondition: Assume that this value will be greater than or equal to 1.)
currentRun
The GameSpinner class contains a currentRun method, which reports the length of the current run of consecutive identical spin values.
Returns the current length of the run of consecutive identical spins.
If no spins have occurred, returns 0.
If the most recent spin value is different from the spin value immediately preceding it, the current run length is 1.
If the most recent spin value is the same as the spin value immediately preceding it, the current run length is incremented by 1.
Returns an int representing the length of the current run.
spin
The GameSpinner class contains a spin method, which simulates a spin of the spinner.
Generates and returns a random integer between 1 and the number of sectors (inclusive).
Updates the current run length based on whether the generated value matches the previous spin value.
Returns the random integer result of the spin.
Statement | Return Value | Explanation |
|---|---|---|
GameSpinner g = new GameSpinner(4); | A GameSpinner object is created with 4 sectors. | |
g.currentRun(); | 0 | No spins have occurred yet. |
g.spin(); | 3 | A random value between 1 and 4 is generated. Let's assume it is 3. The current run is now a sequence of one 3. |
g.currentRun(); | 1 | The current run length is 1. |
g.spin(); | 3 | Let's assume the random value is 3 again. The run of 3s continues. |
g.currentRun(); | 2 | The current run length is 2 (two consecutive 3s). |
g.spin(); | 4 | Let's assume the random value is 4. The run of 3s is broken. The new run is a sequence of one 4. |
g.currentRun(); | 1 | The current run length is 1. |
g.spin(); | 4 | Let's assume the random value is 4 again. |
g.currentRun(); | 2 | The current run length is 2. |
Write the complete GameSpinner class. Your implementation must meet all specifications and conform to the examples shown in the table.
00:00