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

Problem 5 (name this Lab7_Problem5) (Java Program) Write a Java program that per

ID: 3757673 • 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

import java.util.Random;

public class Coin
{
public static void main(String[] args) {
int heads=0, tails=0, tossResult, total;
double tailsPercent, headsPercent;
      
      
Random coinTossing = new Random();

  
System.out.println( "Total" + " " + "Heads" + " " + "heads%" + " " + "tails" + " " +
"tails%");
  
  
for (int i = 1; i <= 10; i++) {
tossResult = coinTossing.nextInt(2);
if (tossResult == 0){
heads++;
}
else {
tails++;
}
if ((i%1)==0) {
total = heads+tails;
headsPercent = (heads*100)/total;
tailsPercent = (tails*100)/total;
  
System.out.printf( " %d %d %.1f %d %.1f ", total,heads, headsPercent, tails,
tailsPercent );
}
}
}
}

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