Spin Game Version 1 In version 1, you will create the game in a class called Spi
ID: 3546475 • Letter: S
Question
Spin Game Version 1
In version 1, you will create the game in a class called SpinGame1 which does not have a main method. Create another class classed GameTester1 with a main method that includes the following code:
public static void main(String[] args) {
SpinGame1 game = new SpinGame1();
sg.playGame();
}
Using the following UML model as a guide, create the SpinGame1 class with the three attributes and two methods shown in the model below. Set the playerName to a value of your choice and initialize the playScore to zero. Also create a Random instance to be used by the spin method.
GameTester1
+main(String[]):void
---I 1
| game
| 1
V
SpinGame1
-playerName: String
-playerScore: int
-randomMaker: Random
+playerGame(): void
+spin():int
Sample output for GameTester1:
Round 1
8 10 4 1 8 2 7 10 2 2 4 2 7 10 3 7 9 1 10 4 10 2 3 3 6 6 8 5
Round 1 over. Fred
GameTester1
+main(String[]):void
Explanation / Answer
// GameTester1.java
public class GameTester1
{
public static void main(String[] args)
{
SpinGame1 sg = new SpinGame1();
sg.playGame();
}
}
// SpinGame1.java
import java.util.Random;
public class SpinGame1
{
private String playerName = "Tom";
private int playerScore = 0;
private Random randomMaker = new Random();
public void playGame()
{
for (int r = 1; r <= 5; r++)
{
System.out.println("ROUND " + r);
int score = 0;
int num = spin();
while (num != 5)
{
score++;
System.out.print(num + " ");
num = spin();
}
playerScore += score;
System.out.println("5");
System.out.println("Round " + r + " over. " + playerName
+ "'s score is now " + playerScore);
}
}
public int spin()
{
return randomMaker.nextInt(10) + 1;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.