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

public class CoinToss { public static void main(String[] args) { // Variable for

ID: 3922056 • Letter: P

Question

 public class CoinToss {    public static void main(String[] args)    {                 // Variable for the number of times to toss.                 int times;                        // Create a Scanner object for keyboard input.       Scanner keyboard = new Scanner(System.in);                        // Get the number of times to toss the coin.                 System.out.print("How many times do you want " +                                  "to toss the coin? ");                 times = keyboard.nextInt();                                  // Simulate the coin toss.                 for (int i = 0; i < times; i++)                         coinToss();    }                  /**                 The coinToss method displays either "heads" or                 "tails" depending on the value of a randomly                 generated number.         */         public static void coinToss()         {       // Create a Random object.       // INSERT THE MISSING STATEMENT TO CREATE A RANDOM OBJECT              // Generate a random number in the range                 // of 0 through 1. Note that the argument                 // passed to the nextInt method is the                  // upper limit, exclusive.       int randNum = rand.nextInt(2);                                  // Display the result of the toss.                 // INSERT THE MISSING IF..ELSE STATEMENT TO OUTPUT HEADS OR TAILS     } } 
    
    
    
 Can Someone fill in the missing statements so this Java code will run 

Explanation / Answer

Program)

import java.util.Scanner;

public class Cointoss {

private enum Coin {
HEADS,
TAILS
};

public static void main(String[] args)
{

new Cointoss();
}

Cointoss() /** The coinToss method displays either "heads" or "tails"
depending on the value of a randomly generated number. */
{

Scanner keyboard = new Scanner(System.in);
int heads = 0, tails = 0;
  

while (true) {
System.out.println("How many times do you want " +"to toss the coin?");
System.out.println("To exit the programpress 0 ");
int times = keyboard.nextInt();
if (times == 0)
break; // or System.exit

for (int i = 0; i < times; i++) {
Coin randNum = toss();
//Coin tossResult = toss();
switch (randNum) {
case HEADS:
heads++;
break;
case TAILS:
tails++;
break;
}
}
System.out.println("Heads: " + heads);
System.out.println("Tails: " + tails);
}
}

private Coin toss() {

return Coin.values()[(int) (Math.random() * Coin.values().length)];
}
}

Output:

How many times do you want to toss the coin?

To exit the programpress 0

100

Heads: 52

Tails: 48