The problem

For this assignment you will create a game program using the coin class from the coin toss simulator exercise. The program should have three 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 lands tails-up. The game is over when your balance reaches one dollar or more. If your balances is exactly one dollar, you win the game. You will lose if your balance exceeds one dollar.

Breaking it down

Coin Class

classCoin{privateStringsideUp;/**
* Default constructor
*/publicCoin(){// initialize sideUptoss();}/**
* This method will simulate the tossing of a coin. It should set the
* sideUp field to either "heads" or "tails".
*/publicvoidtoss(){Randomrand=newRandom();// Get a random value, 0 or 1.intvalue=rand.nextInt(2);if(value==0){this.sideUp="heads";}else{this.sideUp="tails";}}/**
*
* @return The side of the coin facing up.
*/publicStringgetSideUp(){returnsideUp;}}

Program demonstration

// ConstantsstaticfinaldoublePLAY_TO=1.00;staticfinaldoubleTWENTY_FIVE_CENTS=0.25;staticfinaldoubleTEN_CENTS=0.10;staticfinaldoubleFIVE_CENTS=0.05;publicstaticvoidmain(Stringargs[]){TossingCoinsForADollarcoinTossSimulator=newTossingCoinsForADollar();Coinquarter=coinTossSimulator.newCoin();Coindime=coinTossSimulator.newCoin();Coinnickel=coinTossSimulator.newCoin();doublebalance=0;System.out.println("Ready? Set? Go!");// Play the game while the balance// is less than the PLAY_TO constantwhile(balance<PLAY_TO){// toss each coinquarter.toss();dime.toss();nickel.toss();// add appropriate value to balanceif(isHeadsUp(quarter)){balance+=TWENTY_FIVE_CENTS;}if(isHeadsUp(dime)){balance+=TEN_CENTS;}if(isHeadsUp(nickel)){balance+=FIVE_CENTS;}}// Display balance to userSystem.out.printf("Balance: $%,1.2f\n",balance);// Display whether or not they won based// on the program requirementif(balance==PLAY_TO){System.out.println("You win!");}else{System.out.println("You did not win.");}}/**
* Method will determine if the coin is heads up
*
* @param coin
* @return true if coin is heads up
*/publicstaticbooleanisHeadsUp(Coincoin){if(coin.getSideUp().equals("heads")){returntrue;}else{returnfalse;}}

Output

Ready? Set? Go!
Balance: $1.20
You did not win.

Unit tests

@Testpublicvoidheads_up(){// use mockito to mock objectCoincoin=mock(TossingCoinsForADollar.Coin.class);when(coin.getSideUp()).thenReturn("heads");assertTrue(TossingCoinsForADollar.isHeadsUp(coin));}@Testpublicvoidheads_down(){// use mockito to mock objectCoincoin=mock(TossingCoinsForADollar.Coin.class);when(coin.getSideUp()).thenReturn("tails");assertFalse(TossingCoinsForADollar.isHeadsUp(coin));}@Test(expected=NullPointerException.class)publicvoidheads_up_nullpointer(){TossingCoinsForADollar.isHeadsUp(null);}