Answer:
Here is the JAVA program:
import java.util.Scanner; // to get input from user
import java.util.Random; // to generate random numbers
public class CoinFlip {
// function for flipping a coin to make decisions
public static String HeadsOrTails(Random rand) {
//function to return head or tail
String flip; // string type variable flip to return one of the heads or tails
if ((rand.nextInt() % 2) == 0)
// if next random generated value's mod with 2 is equal to 0
{flip = "heads";} // sets the value of flip to heads
else // when the IF condition is false
{ flip = "tails";} // sets value of flip to tails
return flip; } //return the value of flip which is one of either heads or tails
public static void main(String[] args) { //start of main function body
Scanner rand= new Scanner(System.in); // creates Scanner instance
// creates random object and seeds the program with value of 2
Random no_gen = new Random(2);
int decision = rand.nextInt(); //calls nextInt() to take number of decisions
for (int i = 0; i < decision; i++) {
// produces heads or tails output for the number of decisions needed
System.out.println(HeadsOrTails(no_gen)); } } }
//prints heads or tails
Step-by-step explanation:
The above program has method HeadsOrTails(Random rand) to return the heads or tails based on the condition if ((rand.nextInt() % 2) == 0) . Here nextInt() is obtains next random number from the sequence of random number generator and take the modulus of that integer value with 2. If the result of modulus is 0 then the value of flip is set to heads otherwise tails.
In the main() function, a random object is created and unique seed 2 is set. The decision variable gets the number of decisions. In the for loop the loop variable i is initialized to 0. The loop body continues to execute until the value of i exceeds the number of decisions needed. The loop body calls the HeadsOrTails(Random rand) method in order to output either heads or tails.
The program and output is attached in a screenshot.