Please view the below program.
In this assignment you will modify your Coin Toss Game from Assignment 2. The new and improved Coin Toss game will use the same two classes (you may use my solution from Assignment 2 as a starting point if yours did not run properly). The new game will allow users to keep playing as long as they wish. It will do this by having multiple rounds where each round is worth $50 flat. If you win, you add $100 to your total and if you lose, you lose $50 from the total. The game will keep prompting for heads, tails or quit. You can choose to quit at any time and keep your winnings or owe the game money. Your job for assignment three is to make the following changes to accomplish the above functionality:
1. Add a method that is called getUserInput() in the GamePlayer class. This method will be called from the main method and will take in either the String heads, tails, or quit from the user. The method should return 0 for heads, 1 for tails and -1 for quit. Use a switch statement to accomplish this. 2. Change the functionality so that the game keeps going until the user enters quit (so until the getUserInput() method returns -1) . 3. Change the functionality so that the user is no longer prompted to enter a bet, each bet is automatically worth $50. You can change your Coin class to hold this value ($50) and change the constructor accordingly. When a user wins they will still receive 2*bet (so $100) but when they lose they will lose $50. 4. Add a method in the Coin class called getLoss() which returns 50 (or negative 50, however you choose to model it). You can add/subtract this from the total in your main method when the user loses.
5. Add functionality to keep track of both the user’s running total and the number of rounds that they have played so far. 6. Add a quit() method in the GamePlayer class. When the user decides to quit, they should see their total winnings (or loss if they have a negative amount of money) and total number of rounds played. The game should tell them that they owe money if that is the case.
Sample Output (user walks away with winnings):
run GamePlayer
To play, first enter your guess as heads, tails or quit
heads
You won! Your prize amount is
$100.00
Your running total after round 1 is
$100.00
To play, first enter your guess as heads, tails or quit
tails
You won! Your prize amount is
$100.00
Your running total after round 2 is
$200.00
To play, first enter your guess as heads, tails or quit
quit
Thank you for playing 2 round(s). Your total was:
$200.00
Sample Output (user owes the game money):
run GamePlayer
To play, first enter your guess as heads, tails or quit
tails
You lost...please play again
Your running total after round 1 is
$-50.00
To play, first enter your guess as heads, tails or quit
quit
Thank you for playing 1 round(s). You owe the game:
$50.00
.
here is the code what i got .
coin class
/*
* A class that models a coin Object. The coin has an initial value.
* The coin also has a prize value worth twice as much as the initial value.
* The coin has a method to simulate flipping it and returns a 0 for heads and a 1 for tails.
* */
import java.util.Scanner;
public class Coin
{
private float initialValue;//the initial value of the coin
/**
* A construcror to create a coin with an initial bet value
* */
public Coin (float userBet)
{
initialValue=50;
}
/**
* A method that flips the coin by generating a random number
* and assigning heads as 0.5 or less and tails as greater than 0.5
* Returns 0 for heads and 1 for tails.
* */
public int flip()
{
double headsOrTails= Math.random();
if (headsOrTails >0.5)
{
return 1;//if the random number is greter than 0.5, return 1 (tails)
}
else
{
return 0;//numbers below 0.5 will return 0 (heads)
}
}
/**
* A method that returns the prie value for the user as a float
* */
public float getPrize()
{
return 2*initialValue;//as per the requirements, return twice the initial bet
}
}
/**
* A class that tests the Coin class. It simulates playing a game
* where a user fips a coin. Heads is represented as 0 and tails as 1.
* The user alos places an initial bet and can win twice the bet value.
* */
import java.util.Scanner;
public class GamePlayer
{
/**
* The main method asks the user for a bet value and to enter either 1 for tails or 0 for heads
* A coin is then created and tossed. The value (heads or tails) is compared with the original guess.
* The user is then informed of the result and any prize money.
* */
public static void main (String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Now enter your guess, heads,tails or quit");
String input = sc.nextLine();
int userGuess = sc.nextInt();
Coin game = new Coin(50);//create a new coin called game
int headsOrTails = game.flip();
if (headsOrTails ==userGuess)
{
System.out.println("You won! Your prize amount is: ");
System.out.printf("%s%.2f\n", "$", game.getPrize());//formats the value to two decimal places, as currency in dollars
}
else
{
System.out.println("You lost...please play again");
}
}
public int getUserInput(String input)
{
int round=round+1;
switch (input)
{
case "heads":
Coin game = new Coin(50);//create a new coin called game
String mguess = game.flip();
if(input==mguess)
{
System.out.println("you won, your total amount is" +bet*2);
}
}
}
}
Please help me find the solution