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

Flip A Coin Program Write a game program that will ask the user to enter a numbe

ID: 664864 • Letter: F

Question

Flip A Coin Program

Write a game program that will ask the user to enter a number for the heads in a row the program will produce. The game will flip the "coin" until the number of heads is reached. First trace the program as it flips the coin. A random coin flip can be generated by int flip = (int)((Math.random() * 2) + 1).

This is what I got so far but I'm not sure how to complete it.

import java.util.Scanner;
public class FlipACoin
{
   public static void main (String [] args)
   {
       Introduction();
       chooseTheImplementation();
   }
      
   public static void FlipACoin()
   {
       int flip = (int)((Math.random() * 2) + 1);
   }
  
   private static void Introduction()
   {
       System.out.println("This game repeatedly flips a coin." + " "
               + "This game prompts you for the number of" + " "
               + "times you would like heads to appear in" + " "
               + "a row. It then flips the coin untill that" + " "
               + "number of heads in a row are flipped.");
   }
  
   private static void chooseTheImplementation()
   {
       Scanner keyboard = new Scanner(System.in);
       System.out.println();
       System.out.println("You have two implementation from which to choose.");
       System.out.println("1) Please trace the code as the coin is flipped." + " "
               + "2) Please just give the number of flips.");
       System.out.println("Please choose 1 or 2");
       int chooseImplementation = keyboard.nextInt();
      
       if(chooseImplementation == 1)
       {
           givemeTheTraceImplementation();
       }
       else if(chooseImplementation == 2)
       {
           giveMeJustTheAnswerImplementation();
       }
       else
       {
           System.out.println("You have entered a wrong choice");
       }
      
   }
  
   private static void givemeTheTraceImplementation()
   {
       int headCount = 0;
       int tailCount = 0;
       int consecutiveHeads = 0;
      
       Scanner keyboard = new Scanner(System.in);
       System.out.println("Please enter the number of heads in" +" "
               + "a row you would like.");
       int numberOfHeads = keyboard.nextInt();
      
       for(int i = 0; i < numberOfHeads; i++)
       {
           //FlipACoin();
           int flipCoin = (int)((Math.random() * 2) + 1);
          
           if(flipCoin == 1)
           {
               System.out.println("Heads");
               headCount++;
               consecutiveHeads++;
               System.out.println("headCount " + headCount);
               System.out.println("ConsecutiveHeads " + consecutiveHeads);
              
               if(consecutiveHeads == numberOfHeads)
               {
                   System.out.println("You reached " + numberOfHeads + " heads in a row");
               }
           }
           else
           {
               System.out.println("Tails");
               consecutiveHeads = 0;
           }
           consecutiveHeads = numberOfHeads;
       }
      
   }
  
   private static void giveMeJustTheAnswerImplementation()
   {
      
   }
  

}

Explanation / Answer

Please check the program below, I checked and found it is working. Hope it matches your requirement:

import java.util.Scanner;

public class HeadTailCnt {
                      

   public void flip(){
              

        int headCounter = 0; //counts lines that are all heads

        int tailCounter = 0; //counts lines that are all tails

    int hCounter = 0; //counts H's printed

    int tCounter = 0; //counts T's printed

                   

           

    Scanner input = new Scanner(System.in);
   

    System.out.println("Enter the number of runs (1 - 100,000): ");

    int r = input.nextInt();

               

    System.out.println("Enter the number of flips per run (1 - 20): ");

    int f = input.nextInt();

                   

    if(r>0 && f>0) {

        if(r<=100000 && f<=20) {

                 for (int run = 0; run < r; run++) {

                    for (int flip = 0; flip < f; flip++)

                                                                           

                       if(Math.random() < 0.5) {

                           System.out.print("H ");

                           tCounter = 0; //because an H was printed the counter is set back to 0

                           hCounter++;

                       }  

                       else {

                              System.out.print("T ");

                              hCounter = 0; //because a T was printed the counter is set back to 0

                              tCounter++;

                       }  

                                       

                                       

                       if(hCounter == f) {

                           System.out.print(" << ALL HEADS >>");

                           headCounter++;

                       }

                       else if(tCounter == f) {

                           System.out.print(" << ALL TAILS >>");

                           tailCounter++;

               }

                                       

                       System.out.println();

                       hCounter = 0;

                       tCounter = 0;

                            

            }

       }

        else

        {

          System.out.println("Please check your entry and try again.");

          return;

        }

          }

        else

        {

           System.out.println(" Runs and flips must be greater than 0!");

           return;

        }

                       

        System.out.println();

        System.out.printf("Number of lines with all heads: %d ", headCounter);

        System.out.printf("Number of lines with all tails: %d", tailCounter);

                   

    }

               

    public static void main(String[] args) {

           

       HeadTailCnt coin = new HeadTailCnt();

               

       coin.flip();

    }


}