flip
Category: Programming
Author: Stuart Reges
Book Chapter: 5.1
Problem: flip
Write a method called flip that takes a Random
object and an integer n as parameters and that prints information about a
coin-flipping simulation. 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 n heads in a row (where n is the second
value passed as a parameter to the method).
For example, if we construct a Random object and make the following calls:
Random r = new Random();
flip(r, 2);
flip(r, 4);
We expect to get output like the following:
flips: H T T T T T T H H
flips: H H T H T H T H H H T T T T T H T H H H H
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. You may assume that the integer
passed as a parameter to your method is greater than 0.