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

Tossing Coins for a Dollar programming challenge from Starting out with C++ from

ID: 3762915 • Letter: T

Question

Tossing Coins for a Dollar programming challenge from Starting out with C++ from Tony Gaddis.

Coin Toss Simulator Write a class named Coin. The coin class should have the following member variable: A string named sideup. The sideUp member variable will hold either "heads" or "tails" indicating the side of the coin that is facing up. The Coin class should have the following member functions: A default constructor that randomly determines the side of the coin that is facing up ("heads" or "tails") and initializes the sideUp member variable accordingly. A void member function named toss that simulates the tossing of the coin. When the toss member function is called, it randomly determines the side of the coin that is facing up ("heads" or "tails") and sets the sideUp member variable accordingly. A member function named getsideUp that returns the value of the sideup member variable. Write a program that demonstrates the coin class. The program should create an instance of the class and display the side that is initially facing up. Then, use a loop to toss the coin 20 times. Each time the coin is tossed, display the side that is facing up. The program should keep count of the number of times heads is facing up and the number of times tails is facing up, and display those values after the loop finishes. Tossing Coins for a Dollar For this assignment, you will create a game program using the coin class from Programming Challenge 12. 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. Tire game is over when your balance reaches $1 or more. If your balance is exactly $1, you win the game. You lose if your balance exceeds $1.

Explanation / Answer

import java.util.Random;


public class Coin {

   String sideup="";
  
    Random rand = new Random();

   Coin()
   {
       int randomNum = rand.nextInt((2 - 0) + 1) + 0;
   if(randomNum==1)
   {
       sideup="head";
              
   }
   else
       sideup="tail";
   }
  
   void Toss()
   {
       int randomNum = rand.nextInt((2 - 0) + 1) + 0;
       if(randomNum==1)
       {
           sideup="head";
                  
       }
       else
           sideup="tail";
      
      
      
      
   }
  
   String getsideup()
   {
      
       return sideup;
      
   }
   public static void main(String[]args)
   {
       Coin c=new Coin();
       int countH=0,countT=0;
   for(int i=0;i<20;i++)
   {
      
       c.Toss();
   System.out.println("The side of coin facing up is "+c.getsideup());
       if(c.getsideup().equals("head"))
       {
           countH++;
          
       }
       else
           countT++;
   }
      
   System.out.println("Head comes "+countH+" times");
   System.out.println("Tail comes "+countT+" times");

  
   }
  
}

*************************************************

import java.util.Random;


public class Coin {

   String sideup="";
  
    Random rand = new Random();

   Coin()
   {
       int randomNum = rand.nextInt((2 - 0) + 1) + 0;
   if(randomNum==1)
   {
       sideup="head";
              
   }
   else
       sideup="tail";
   }
  
   void Toss()
   {
       int randomNum = rand.nextInt((2 - 0) + 1) + 0;
       if(randomNum==1)
       {
           sideup="head";
                  
       }
       else
           sideup="tail";
      
      
      
      
   }
  
   String getsideup()
   {
      
       return sideup;
      
   }
   public static void main(String[]args)
   {
       Coin quarter=new Coin();
       Coin dime=new Coin();
       Coin nickle=new Coin();
double balance=0;
      
   for(int i=0;i<20;i++)
   {
      
       quarter.Toss();
       dime.Toss();
       nickle.Toss();
       System.out.println(quarter.getsideup());

       if(quarter.getsideup().equals("head"))
       {
          
          
           balance=balance+0.25;
       }
       System.out.println(dime.getsideup());

       if(dime.getsideup().equals("head"))
       {
          
          
           balance=balance+0.1;
       }
       System.out.println(nickle.getsideup());

       if(nickle.getsideup().equals("head"))
       {
          
          
           balance=balance+0.05;
       }
   //   System.out.println(balance);

   if(balance>=1)
       break;
      
      
      
   }
   //System.out.println(balance);
   if(balance==1)
   {
       System.out.println("you Won");
  
      
   }
   else
      
   System.out.println("You lose");

  
   }
  
}