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

For this assignment you will create agame program using the coin class from Prog

ID: 3543021 • Letter: F

Question

For this assignment you will create agame program using the coin class from Programming Challenge 12(see attachment). The program should have three three instances of the coin class: one representing a quarter, one respresenting 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.


Here is programming challenge 12:

public class CoinFlippa
{
   static java.util.Random r = new java.util.Random();
   
   public static void main(String[] args)
   {
      new CoinFlippa().run();
   }
   
   private void run()
  {
      Coin coin = new Coin();
      for (int i = 0; i < 20; i++)
      {
         coin.toss();
         System.out.printf("%2d - %s%n", i + 1, coin.getSideUp());
   
      }
   }
   
   // anonymous, inner class
   // can make a separate file, put both in same folder
   // cd, to folder then run -- $> java CoinFlippa
   
   class Coin
   {
   
      private String sideUp;
   
   
  public Coin()
   {
      toss();
   }
   
   void toss()
   {
      sideUp = (r.nextInt(100) % 2 == 0) ? "heads" : "tails";
   }
   
   public String getSideUp()
   {
      return sideUp;
   }
   }
}

Explanation / Answer


class Coin
{
static java.util.Random r = new java.util.Random();
private String sideUp;
public Coin()
{
toss();
}
void toss()
{
sideUp = (r.nextInt(100) % 2 == 0) ? "heads" : "tails";
}
public String getSideUp()
{
return sideUp;
}
}

public class game
{
public static void main(String[] args)
{
double sum =0;
Coin quarter = new Coin();
int quarter_count=0;
Coin nickel = new Coin();
int nickel_count=0;
Coin dime = new Coin();
int dime_count=0;
while(sum<1)
{
quarter.toss();
if(quarter.getSideUp().equalsIgnoreCase("heads"))
{
quarter_count++;
sum = sum + 0.25;
}
nickel.toss();
if(nickel.getSideUp().equalsIgnoreCase("heads"))
{
nickel_count++;
sum = sum + 0.05;
}
dime.toss();
if(dime.getSideUp().equalsIgnoreCase("heads"))
{
dime_count++;
sum = sum + 0.1;
}
}
if(sum==1)
{
System.out.println("You won the game ");
System.out.println("You got " + quarter_count + " quarters " + ", " + dime_count + " dimes and " + nickel_count+ " nickels");
System.out.printf(" and their sum is %.2f",sum);
}
else
{
System.out.println("You lost the game ");
System.out.println("You got " + quarter_count + " quarters " + ", " + dime_count + " dimes and " + nickel_count+ " nickels");
System.out.printf(" and their sum is %.2f",sum);
}
}
}

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