Question: Java program Write a static method named sameFlip that accepts a Random object as a parameter. Your method should flip a coin until the same

Java program

Write a static method named sameFlip that accepts a Random object as a parameter. Your method should flip a coin until the same result occurs twice in a row. In other words, if a head is flipped followed by another head or if a tail is flipped followed by another tail, your method should end. You should use the Random object to give an equal chance to a head or tail appearing. Each time the coin is flipped, print H for heads or T for tails.

For example, if the following variable is initialized:

Random r = new Random();

Here are some sample calls along with possible output:

Call

Output

sameFlip(r);

HTHH

sameFlip(r);

HTHTT

sameFlip(r);

TT

sameFlip(r);

THTHTHH

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!