I have to create a java program that outputs certain questions for flipping coin
ID: 3539809 • Letter: I
Question
I have to create a java program that outputs certain questions for flipping coins. Here are the outputs:
There are 6 game programs this menu offers.%u2028Which game would you like to play?%u2028Please enter the number.%u20281) Flip a coin a certain number of heads in a row. 2) Race three names to the finish line.
3) Race three names to the finish line.(harder) 4) Draw geometric shapes.%u20285) Guess the characters in a string of words. 6) Build a stack of six boxes.
7) Quit playing these games.
1
This game repeatedly flips a coin.%u2028This game prompts you for the number of times you would like heads to appear in%u2028a row. It then flips the coin until that number of heads in a row are flipped.
You have two implementations from which to choose. 1) Please trace the code as the coin is flipped. 2) Please just give the number of flips.
Please choose 1 or 2.
1
Please enter the number of heads in a row you would like.
3
We start to flip.%u2028You flipped a head!%u2028The number of heads is 1%u2028So far you have flipped the coin 1 times.
You flipped a head!%u2028The number of heads is 2%u2028So far you have flipped the coin 2 times.
You flipped a tails.%u2028You must reset the count of heads to zero. So far you have flipped the coin 3 times.
You flipped a tails.%u2028You must reset the count of heads to zero. So far you have flipped the coin 4 times.
You flipped a head!%u2028The number of heads is 1%u2028So far you have flipped the coin 5 times.
You flipped a head!%u2028The number of heads is 2%u2028So far you have flipped the coin 6 times.
You flipped a tails.%u2028You must reset the count of heads to zero. So far you have flipped the coin 7 times.
You flipped a head!%u2028The number of heads is 1%u2028So far you have flipped the coin 8 times.
You flipped a head!%u2028The number of heads is 2%u2028So far you have flipped the coin 9 times.
You flipped a tails.%u2028You must reset the count of heads to zero. So far you have flipped the coin 10 times.
You flipped a tails.%u2028You must reset the count of heads to zero. So far you have flipped the coin 11 times.
You flipped a head!%u2028The number of heads is 1%u2028So far you have flipped the coin 12 times.
You flipped a tails.%u2028You must reset the count of heads to zero. So far you have flipped the coin 13 times.
You flipped a head!%u2028The number of heads is 1%u2028So far you have flipped the coin 14 times.
You flipped a head!%u2028The number of heads is 2%u2028So far you have flipped the coin 15 times.
You flipped a tails.%u2028You must reset the count of heads to zero. So far you have flipped the coin 16 times.
You flipped a tails.%u2028You must reset the count of heads to zero. So far you have flipped the coin 17 times.
You flipped a head!%u2028The number of heads is 1%u2028So far you have flipped the coin 18 times.
You flipped a head!%u2028The number of heads is 2%u2028So far you have flipped the coin 19 times.
You flipped a tails.%u2028You must reset the count of heads to zero. So far you have flipped the coin 20 times.
You flipped a head!%u2028The number of heads is 1%u2028 So far you have flipped the coin 21 times.
You flipped a head!%u2028The number of heads is 2%u2028 So far you have flipped the coin 22 times.
You flipped a head!%u2028The number of heads is 3 %u2028So far you have flipped the coin 23 times.
Congratulations! It took you 23 flips to get 3 heads in a row.
Would you like to flip the coin again? (yes/no)
Yes
You have two implementations from which to choose. 1) Please trace the code as the coin is flipped. 2) Please just give the number of flips.
Please choose 1 or 2.
2
Please enter the number of heads in a row you would like.
4
It took 4,516,461 flips to get
20 heads in a row.
Would you like to flip the coin again? (yes/no)
No
1
It took 4,516,461 flips to get
20 heads in a row.
Would you like to flip the coin again? (yes/no)
Which game would you like to play?%u2028Please enter the number.%u2028
1) Flip a coin a certain number of heads in a row.
2) Race three names to the finish line.%u2028
3) Race three names to the finish line.(harder)
%u20284) Draw geometric shapes.%u2028
5) Guess the characters in a string of words.
%u20286) Build a stack of six boxes.
7) Quit playing these games.
import.java.text.DecimalFormat;// this imports the DecimalFormat class
DecimalFormat numberFormat = new DecimalFormat("##,###,###,###");// creates an instance of DecimalFormat
String stringNumber = numberFormat.format(numberOfThrows);// stringNumber is a string representing the int numberOfThrows with commas.
Explanation / Answer
import java.util.Random;
import java.util.Scanner;
public class HeadCount
{
public static void main( String[] args ) {
Scanner scan = new Scanner( System.in );
Random rand = new Random();
// prompt for and get user input
System.out.println( "How many coin flips?" );
int flips = scan.nextInt();
int heads = 0; // to count runs of heads
int maxHeads = 0; // to track maximum head count
// flip flips times
for (int i = 0; i < flips; i++) {
// coin face will be treated as H(ead) for 1 and T(ail) fo 2
int face = rand.nextInt( 2 );
// if H(ead)...
if (1 == face) {
System.out.print( 'H' ); // print an "H"
heads++; // increment current count
}
else {
System.out.print( 'T' ); // print a "T"
// if current head count is greater than previously
// recorded maximum count, replace old max
maxHeads = (heads < maxHeads) ? maxHeads : heads;
heads = 0; // reset head count to start over
}
}
System.out.print( " maximum number of consecutive heads was: " + maxHeads );
}
}