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

The program should have three instances of the Coin class: one representing a qu

ID: 3541448 • Letter: T

Question

The program should have three instances of the Coin class: one representing a quarter, one representing a dime, and one representing a                        nickel. When the game begins, your starting balance is $0. During each round of the game, the program will toss the simulated coins. When a coin is tossed,                        the value of the coin is added to your balance if it lands heads-up. For example, if the quarter lands heads-up, 25 cents is added to your balance. Nothing                        is added to your balance for coins that land tails-up. The game is over when your balance reaches one dollar or more. If your balance is exactly one                        dollar, you win the game. You lose if your balance exceeds one dollar.

Explanation / Answer

There ll be two classes.

1. Coin class





public class Coin {

//declaring a String variable

private String sideUp;


//no-argument constructor which will determine the simulation of the coin

public Coin() {

// Using random function which is inbuilt in java. If it is less than 0.5, then

//set it to heads else to tails.

if(Math.random() < 0.5)

this.sideUp = "heads";

else

this.sideUp = "tails";

}



// toss() method to determine the simulation of the coin and sets the fields to head or tail

public void toss() {

// Using random function which is inbuilt in java. If it is less than 0.5, then

//set it to heads else to tails.

if(Math.random() < 0.5)

this.sideUp = "heads";

else

this.sideUp = "tails";


}


//this method return the side which is up

public String getSideUp() {

return this.sideUp;

}


}












2. CoinDemo class which ll contain the main method



public class CoinDemo {


public static void main(String[] args) {

//declaring three objects of Coin class

//first one for the quarters

Coin coinQuarter = new Coin();

//second object for the dimes

Coin coinDime = new Coin();

//third object for the nickels

Coin coinNickel = new Coin();

//an integer variable balance initialized to 0.

int balance = 0;

//toss the coins till the balance is less than 100. Once it becomes 100 or more, terminate the loop.

while(balance < 100){

//tossing the quarter coin.

coinQuarter.toss();

// if the result of tossing the coin is head then add 25 cents to the balance

if(coinQuarter.getSideUp().equals("heads") ){

balance +=25;

}

// In case, the balance becomes 100, terminate the loop.

if (balance== 100){

break;

}

//tossing the dime coin.

coinDime.toss();

// if the result of tossing the coin is head then add 10 cents to the balance

if(coinDime.getSideUp().equals("heads")){

balance +=10;

}

// In case, the balance becomes 100, terminate the loop.

if (balance== 100){

break;

}

//tossing the nickel coin.

coinNickel.toss();

// if the result of tossing the coin is head then add 5 cents to the balance

if(coinNickel.getSideUp().equals("heads")) {

balance +=5;

}

}

//if the balance after the while loop terminates is 100, print you won.

if (balance == 100){

double balanceDOuble = (double)balance/100;

System.out.println("Total after toss is $:"+balanceDOuble);

System.out.println("You won");

}

//if the balance after the while loop terminates is more than 100, print you lose.

else if(balance > 100){

double balanceDOuble = (double)balance/100;

System.out.println("Total after toss is $:"+balanceDOuble);

System.out.println("You lose");

}

}

}