Problem 5 (name this Lab7_Problem5) (Java Program) Write a Java program that per
ID: 3757703 • Letter: P
Question
Problem 5 (name this Lab7_Problem5)
(Java Program)
Write a Java program that performs 5 sets of coin tosses. Within each set, flip a coin 10 times and display the results. Base your solution on nested while loops.
Flipping the coin is actually accomplished by generating a random integer, with 0 representing heads and 1 representing tails. The code to flip the coin follows. Embed it in your while loop:
iRandom = iMin + (int) (Math.random() * (iMax - iMin + 1));
Assume that the program already contains this code: variables have been declared and initialized:
// Declarations:
int iCounter; // Loop counter for within-sets (1 to 10)
int iSets; // Loop counter for sets (1 to 5)
int iHeads; // Heads counter (value = 0)
int iTails; // Tails counter (value = 1)
int iMin; // Minimum acceptable random number
int iMax; // Maximum acceptable random number
int iRandom; // Actual outcome (0 or 1) or an individual "coin toss"
iMin = 0; // Set the minimum acceptable random number to 0
iMax = 1; // Set the maximum acceptable random number to 1
Somewhere in your loop, you will need to initialize both counters as well as the totalizers with statements like. Be very wise where you do this:
iCounter = 1;
iSets = 1;
iHeads = 0; // Initialize heads totalizer
iTails = 0; // Initialize tails totalizer
Use two methods to produce your lines of output, and pass each the necessary arguments. One will show the set number, the other will display the heads/tail information as shown in the sample output below.
Continue writing the program so it produces the following output. The output format must look like this; your results will vary depending on the actual outcome of each "toss" of the coin. Notice the percentages are displayed to two decimal places. Make sure yours are too:
Set 1:
Heads: 4/10 (40.00%)
Tails: 6/10 (60.00%.
Set 2:
Heads: 3/10 (30.00%)
Tails: 7/10 (70.00%)
Set 3:
Heads: 6/10 (60.00%)
Tails: 4/ 10 (40.00%)
. . .
Set 5:
Heads: 5/10 (50%)
Tails: 5/10 (50%)
Explanation / Answer
Hope this will serve your need...
import java.io.*;
class CoinFlippingInSets
{
public static void setNumberDisplay(int x)
{
System.out.println("Set "+x+":");
}
public static void tossPercentageDisplay(int heads, int numberOfTosses)
{
System.out.print("Heads: "+heads+"/"+numberOfTosses+" (");
String temp = String.format("%.2f",(float)heads/numberOfTosses*100);
System.out.println(temp+"%)");
System.out.print("Tails: "+(numberOfTosses-heads)+"/"+numberOfTosses+" (");
temp = String.format("%.2f",(float)(numberOfTosses-heads)/numberOfTosses*100);
System.out.println(temp+"%)");
System.out.println();
}
public static void main(String[] args)
{
// Declarations:
int iCounter; // Loop counter for within-sets (1 to 10)
int iSets; // Loop counter for sets (1 to 5)
int iHeads; // Heads counter (value = 0)
int iTails; // Tails counter (value = 1)
int iMin; // Minimum acceptable random number
int iMax; // Maximum acceptable random number
int iRandom; // Actual outcome (0 or 1) or an individual "coin toss"
int numberOfTosses = 10;
int numberOfSets = 5;
iMin = 0; // Set the minimum acceptable random number to 0
iMax = 1; // Set the maximum acceptable random number to 1
for(iSets = 1; iSets <= numberOfSets; iSets++)
{
iHeads = 0; // Initialize heads totalizer
iTails = 0; // Initialize tails totalizer
for(iCounter = 1; iCounter <= numberOfTosses; iCounter++)
{
iRandom = iMin + (int) (Math.random() * (iMax - iMin + 1));
if(iRandom == 0)
iHeads++;
else
iTails++;
}
setNumberDisplay(iSets);
tossPercentageDisplay(iHeads, numberOfTosses);
}
}
}