Question: Write a method called evenFlip that takes a Random object and simulates flipping a coin until an even number of heads and tails have been
Write a method called evenFlip that takes a Random object and simulates flipping a coin until an even number of heads and tails have been flipped. The method should also print information about the coin-flipping simulation. The method should return the number of times the coin was flipped when the simulation is concluded.
Your method should use the Random object to produce a sequence of simulated coin flips, printing whether each flip comes up "heads" or "tails." Each outcome should be equally likely. Your method should stop flipping when you see the same number of heads flipped as tails. For example, if we construct a Random object and make the following call:
Random r = new Random(); evenFlip(r);
We expect to get output like one of the following lines (not all, just one):
flips: H T // should return 2
flips: T H // should return 2
flips: H H T T // should return 4
flips: T T H T H H // should return 6
flips: T T T T T T H H H H H H // should return 12
flips: H H T H T H H T T T // should return 10
Notice that we abbreviate "heads" as "H" and "tails" as "T." You must exactly reproduce the format of the log above, although the specific output produced will vary on different executions because of the use of the Random object to produce different sequences.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
