15.8k views
1 vote
Write a program that simulates flipping a coin to make decisions. The input is how many decisions are needed, and the output is either heads or tails. Assume the input is a value greater than 0.

Ex: If the input is 3, the output is:
tails
heads
heads
For reproducibility needed for auto-grading, seed the program with a value of 2. In a real program, you would seed with the current time. In that case, every program's output would be different, which is what is desired but can't be auto-graded.
Note: A common student mistake is to create an instance of Random before each call to rand.nextInt(). But seeding should only be done once, at the start of the program, after which rand.nextInt() can be called any number of times.
Your program must define and call the following method that returns "heads" or "tails".
public static String HeadsOrTails(Random rand)

1 Answer

4 votes

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.

Write a program that simulates flipping a coin to make decisions. The input is how-example-1
User Elfred
by
5.4k points