Answer:
This question involves the creation and use of a spinner to generate random numbers in a game. A gamespinner object represents a spinner with a given number of sectors, all equal in size. The gamespinner class supports the following behaviors.
creating a new spinner with a specified number of sectors
spinning a spinner and reporting the result
reporting the length of the current run, the number of consecutive spins that are the same as the most recent spin
the following table contains a sample code execution sequence and the corresponding results.
statements value returned
(blank if no value
returned) comment
gamespinner g = new
gamespinner(4); creates a new spinner with four sectors
g. Currentrun(); 0 returns the length of the current run. The length of the current run is initially 0 because no spins have occurred.
g. Spin(); 3 returns a random integer between 1 and 4, inclusive. In this case, 3 is returned.
g. Currentrun(); 1 the length of the current run is 1 because there has been one spin of 3 so far.
g. Spin(); 3 returns a random integer between 1 and 4, inclusive. In this case, 3 is returned.
g. Currentrun(); 2 the length of the current run is 2 because there have been two 3s in a row.
g. Spin(); 4 returns a random integer between 1 and 4, inclusive. In this case, 4 is returned.
g. Currentrun(); 1 the length of the current run is 1 because the spin of 4 is different from the value of the spin in the previous run of two 3s.
g. Spin(); 3 returns a random integer between 1 and 4, inclusive. In this case, 3 is returned.
g. Currentrun(); 1 the length of the current run is 1 because the spin of 3 is different from the value of the spin in the previous run of one 4.
g. Spin(); 1 returns a random integer between 1 and 4, inclusive. In this case, 1 is returned.
g. Spin(); 1 returns a random integer between 1 and 4, inclusive. In this case, 1 is returned.
g. Spin(); 1 returns a random integer between 1 and 4, inclusive. In this case, 1 is returned.
g. Currentrun(); 3 the length of the current run is 3 because there have been three consecutive 1s since the previous run of one 3.
write the complete gamespinner class. Your implementation must meet all specifications and conform to the example
Step-by-step explanation:
Here's a possible implementation of the gamespinner class that meets the specifications and produces the example output:
import java.util.Random;
public class gamespinner {
private int sectors;
private int currentRun;
private int lastSpin;
private Random rand;
public gamespinner(int sectors) {
this.sectors = sectors;
this.currentRun = 0;
this.lastSpin = 0;
this.rand = new Random();
}
public int spin() {
int result = rand.nextInt(this.sectors) + 1;
this.currentRun = (result == this.lastSpin) ? this.currentRun + 1 : 1;
this.lastSpin = result;
return result;
}
public int currentRun() {
return this.currentRun;
}
}
The gamespinner class has four instance variables:
sectors represents the number of sectors in the spinner.
currentRun represents the length of the current run.
lastSpin represents the value of the last spin.
rand represents a Random object that is used to generate random integers.
The class has a constructor that takes an integer argument sectors and initializes the instance variables accordingly.
The spin method generates a random integer between 1 and sectors, inclusive, and updates the currentRun and lastSpin instance variables based on the result. If the result is the same as the last spin, the currentRun is incremented by 1; otherwise, the currentRun is reset to 1. The method returns the result of the spin.
The currentRun method simply returns the value of the currentRun instance variable.
To use the gamespinner class to produce the example output, you could write the following code:
gamespinner g = new gamespinner(4);
System.out.println(g.currentRun()); // 0
System.out.println(g.spin()); // 3
System.out.println(g.currentRun()); // 1
System.out.println(g.spin()); // 3
System.out.println(g.currentRun()); // 2
System.out.println(g.spin()); // 4
System.out.println(g.currentRun()); // 1
System.out.println(g.spin()); // 3
System.out.println(g.currentRun()); // 1
System.out.println(g.spin()); // 1
System.out.println(g.spin()); // 1
System.out.println(g.spin()); // 1
System.out.println(g.currentRun()); // 3