Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Spin Game Version 2 In this version, start by making a copy of SpinGame1 and nam

ID: 3545527 • 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.

UML below


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âs paramenter, then sends it the playGame() message:

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();

}

}

Sample output from running GameTester2:

$ java GameTester2

Enter number of rounds to play: 3

-------------------------------------------

ROUND 1

Fred spins: 6 2 2 3 6 2 7 9 5


Fred's score is now 8

-------------------------------------------

ROUND 2

Fred spins: 4 10 2 4 6 1 9 7 5

Fred's score is now 16

-------------------------------------------

ROUND 3

Fred spins: 2 10 8 3 9 5

Fred's score is now 21



The Original Script

----------------------------------------------------

class GameTester1


{


public static void main(String args[]) throws Exception


{


SpinGame1 sg=new SpinGame1();


sg.playGame();


}


}

-------------------------------------- and


import java.util.*;


import java.io.*;



class SpinGame1


{


String playerName;


int playerScore;


Random randomMaker;



public SpinGame1()


{


playerName="Fred";


playerScore=0;


randomMaker=new Random();


}


public void playGame()


{


for(int i=0;i<5;i++)


{


System.out.println("ROUND "+(i+1));


int count=0;


while(true)


{


int spin=spin();


System.out.print(spin+" ");


count++;



if(spin==5)


{


break;


}


}


playerScore=playerScore+count;


System.out.println("Round "+(i+1)+" is over");


System.out.println(playerName+ "'s score is now "+playerScore);


}


}


public int spin()


{


return randomMaker.nextInt(10)+1;


}


}

-----------------------------------

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.

Explanation / Answer

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();

}

}


_______________________________________________________________________________________________



class SpinGame2


{


String playerName;


int playerScore,numberOfRounds;


Spinner theSpinner;


public SpinGame2(int x)


{


playerName = "Fred";


playerScore = 0;


numberOfRounds=x;

theSpinner=new Spinner();


}


public void playGame()


{


for (int i = 0; i < numberOfRounds; i++)


{


System.out.println("ROUND " + (i + 1));


int count = 0;


while (true)


{


int spin = theSpinner.spin();


System.out.print(spin + " ");


count++;


if (spin == 5)


{


break;


}


}


playerScore = playerScore + count;


System.out.println("Round " + (i + 1) + " is over");


System.out.println(playerName + "'s score is now " + playerScore);


}


}




}

________________________________________________________________________________________________



import java.util.*;



public class Spinner {


Random randomMaker;

public Spinner()

{

randomMaker = new Random();

}

public int spin()

{


return randomMaker.nextInt(10) + 1;


}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote