Spin Game Version 2 In this version, start by making a copy of SpinGame1 and nam
ID: 3546474 • Letter: S
Question
Spin Game Version 2
In this version, start by making a copy of SpinGame1 and name it SpinGame2. The spinning functionality in SpinGame2 should be moved to a new component called Spinner. Using the following UML model as a guide, create a new class called Spinner with the attribute randomMaker.
Also:
1. You should remove the randomMaker attribute from SpinGame2, since it will now only be used by the Spinner object.
2. Move the spin() method to the new Spinner class and have it return the random number it generates.
3. Add a constructor to SpinGame2 that accepts an int parameter that will be saved in its new instance variable numberOfRounds.
4. In the SpinGame2 constructor, also create an instance of Spinner and store it in its attribute theSpinner.
5. Change the playGame method in SpinGame2 to use the value of numberOfRounds to control the loop that executes the number of rounds. Also in playGame, now use the Spinner object you created and send it the spin() message to get a valid spin value.
6. Finally, create a GameTester2 class with a main method that prompts the user for a number of rounds, creates an instance of SpinGame2 with the number of rounds as the constructor
In this version, start by making a copy of SpinGame1 and name it SpinGame2. The spinning functionality in SpinGame2 should be moved to a new component called Spinner. Using the following UML model as a guide, create a new class called Spinner with the attribute randomMaker. You should remove the randomMaker attribute from SpinGame2, since it will now only be used by the Spinner object. Move the spin() method to the new Spinner class and have it return the random number it generates. Add a constructor to SpinGame2 that accepts an int parameter that will be saved in its new instance variable numberOfRounds. In the SpinGame2 constructor, also create an instance of Spinner and store it in its attribute theSpinner.Explanation / Answer
// GameTester2.java
import java.util.Scanner;
public class GameTester2
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
//prompt for number of rounds to play:
System.out.print("Enter number of rounds to play: ");
int x = s.nextInt();
//start SpinGame2 passing in the number of rounds to play
SpinGame2 sg = new SpinGame2(x);
sg.playGame();
}
}
// Spinner.java
import java.util.Random;
public class Spinner
{
private Random randomMaker;
private int spinValue;
public Spinner()
{
randomMaker = new Random();
spinValue = 0;
}
public void spin()
{
spinValue = 1 + randomMaker.nextInt(10);
}
public int getSpinValue()
{
return spinValue;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.