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

Create a Coin class with the following information: - A double field for the val

ID: 3759328 • Letter: C

Question

Create a Coin class with the following information: - A double field for the value of the coin - A boolean field indicating the side of the coin that is facing up - heads (true) or tails (false) - A constructor that assigns values to both fields to begin. - An accessor method that returns the side of the coin that is facing up - A method called toss, which randomly determines the side of the coin that is facing up. Write a seperate main class that contains a main method. In the main, we will play a game with the user in which we will toss all three coins and add up their values. Keep tossing the coins until the value exceeds $1. If they are able to get exactly $1 they win the game. To begin, create 3 coin objects, giving one a value of 25c, one a value of 10c and another a value of 5c. Next, ask the user if they wish to start a game. If yes, toss all three coins and if they fall heads up, add their value to the total. If the total is below $1, toss all three coins again. Output the result and total after each toss, and output if they won or busted. Then ask if they wish to play again, and if yes, reset the totals and repeat the tossing process.

Explanation / Answer

If you have any further queries, just get back to me.The Main class:

public class Main
{
public static void main(String[] args)
{
while(true) //Keep doing till you ask to exit.
{
Coin nickel = new Coin(0.05, false); //Constructor for nickel object.
Coin dime = new Coin(0.10, false); //Constructor for dime object.
Coin quarter= new Coin(0.25, false); //Constructor for quarter object.

double moneyEarned = 0; //Initialize moneyEarned to 0.
Scanner in = new Scanner(System.in);
System.out.print("Do you wish to start a game: 1. Yes. 2. No.");
int n = in.nextInt();
if(n == 1)
{
while(moneyEarned < 1)
{
nickel.toss();
dime.toss();
quarter.toss();
if(nickel.getSideUp() == true)
moneyEarned += nickel.getValue();
if(dime.getSideUp() == true)
moneyEarned += dime.getValue();
if(quarter.getSideUp() == true)
moneyEarned += quarter.getValue();
System.out.println("Toss: nickel: ""+nickel.getSideUp()+
                   "" dime: ""+dime.getSideUp()+
                   "" quarter: ""+quarter.getSideUp()+
                   "" balance: "+moneyEarned);
}
}
if(moneyEarned == 1)
System.out.println("Congratulations... You won the game.");
else
System.out.println("Oops... You busted.");
System.out.print("Do you wish to play again... 1. Yes. 2. No.");   
int playAgain = in.nextInt();
if(playAgain != 1)
return;
}
}
}

The Coin class:

public class Coin
{
double valueOfCoin;
boolean face;
Coin(double val, boolean f)
{
valueOfCoin = val;
face = f;
}
void toss()
{
int randomNum = (int)(Math.random() + 0.5 );
if(randomNum == 1)
face = true;
else
face = false;
}
boolean getSideUp()
{
return face;
}
double getValue()
{
return valueOfCoin;
}   
}