Spin Game Version 3 In this version, start by making a copy of SpinGame2 and nam
ID: 3546473 • Letter: S
Question
Spin Game Version 3
In this version, start by making a copy of SpinGame2 and name it SpinGame3. The Player functionality in SpinGame2 should be moved to a new component called Player. Using the following UML model as a guide, create a new class called Player with the attributes name and score:
Also:
1. Create a constructor in Player that accepts a String parameter. Use the contents of that String to set the value of the Player
In this version, start by making a copy of SpinGame2 and name it SpinGame3. The Player functionality in SpinGame2 should be moved to a new component called Player. Using the following UML model as a guide, create a new class called Player with the attributes name and score: Create a constructor in Player that accepts a String parameter. Use the contents of that String to set the value of the Player's name attribute. Also in the constructor, set the value of the Player's score to 0.Create getter methods for name and score inPlayer. In the SpinGame3 constructor, create a Spinner object and a Player object and store references to them in SpinGame3's attributes theSpinner and thePlayer.Explanation / Answer
// GameTester3.java
import java.util.Scanner;
public class GameTester3
{
// test the game3
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 SpinGame3 passing in the number of rounds to play
SpinGame3 sg = new SpinGame3(x);
sg.playGame();
}
}
// Player.java
public class Player
{
private String name;
private int score;
public Player(String n)
{
name = n;
score = 0;
}
public void takeTurn(Spinner spin)
{
System.out.println("Starting " + name + "'s turn with score = " + score);
System.out.print("Spin values: ");
int sc = 0;
spin.spin();
while (spin.getSpinValue() != 5)
{
sc++;
System.out.print(spin.getSpinValue() + " ");
spin.spin();
}
score += sc;
System.out.println("5");
System.out.println(name + "'s score is now " + score);
}
public String getName()
{
return name;
}
public int getScore()
{
return score;
}
}
// 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;
}
}
// SpinGame3.java
public class SpinGame3
{
private int numberOfRounds;
private Spinner theSpinner;
private Player thePlayer;
public SpinGame3(int x)
{
numberOfRounds = x;
theSpinner = new Spinner();
thePlayer = new Player("Tom");
}
// Play the game
public void playGame()
{
for (int x = 1; x <= numberOfRounds; x++)
{
System.out.println("-------------------------------------------");
System.out.println("ROUND " + x);
thePlayer.takeTurn(theSpinner);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.