Write a Java program that simulates the random flipping of t
Write a Java program that simulates the random flipping of two coins. At the start of the program prompt the user for the amount of times to flip the coin. Display each flip, and afterward display the percentages of heads and tails for each coin. Also, display the percentage that both coins were heads, and the percentage both coins were tails. There should be a method that accepts a minimum and maximum integer as two parameters, and returns a random number between the minimum and the maximum inclusive of the minimum, and the maximum.
Solution
import java.util.Random;
import java.util.Scanner;
public class CoinToss {
Random randomNum = new Random();
private int result_coin1;
private int result_coin2;
private int heads_coin1;
private int heads_coin2;
private int both_heads;
private int both_tails;
private String[] print_coin = {\"Heads\", \"Tails\"};
public static void main(String[] args) {
System.out.println(\"Enter number of times to flip coins: \");
Scanner input = new Scanner(System.in);
int times = input.nextInt();
int total = times;
while(times > 0) {
flip();
}
int percentage_heads_coin1 = (heads_coin1 * 100) / total;
int percentage_heads_coin2 = (heads_coin2 * 100) / total;
int percentage_both_heads = (both_heads * 100) / total;
int percentage_both_tails = (both_tails * 100) / total;
System.out.println(\"Percentage of heads for coin 1: \" + percentage_heads_coin1);
System.out.println(\"Percentage of tails for coin 1: \" + (100 - percentage_heads_coin1))
System.out.println(\"Percentage of heads for coin 2: \" + percentage_heads_coin2);
System.out.println(\"Percentage of tails for coin 2: \" + (100 - percentage_heads_coin2));
System.out.println(\"Percentage of both coins being heads: \" + percentage_both_heads);
System.out.println(\"Percentage of both coins being tails: \" + percentage_both_tails);
}
public void flip() {
result_coin1 = randomNum.nextInt(2);
result_coin2 = randonNum.nextInt(2);
System.out.println(print_coin[result_coin1] + \", \" + print_coin[result_coin2]);
if(result_coin1 == 0) {
heads_coin1++;
if(result_coin2 == 0) {
heads_coin2++;
both_heads++;
}
} else {
if(result_coin2 == 0) {
heads_coin2++;
} else {
both_tails++;
}
}
}
public void getRandomBetween(int lower, int upper) {
int rand = randomNum.nextInt((upper - lower) + 1) + lower;
return rand;
}

