Flipping Coins
A class representing the state of a coin is in the file
Coin.java. We can use instances of the class to simulate the flipping of a coin. Copy it to your directory,
then write a program to find and print the length of the longest run of
heads in 100 flips of the coin. A skeleton of the program is
in the file Runs.java. To use the
Coin class you need to do the following:
- Your BlueJ project needs to include both Runs.java and Coin.java.
- In the main method of Runs, you'll need to create (and use) a Coin object.
- Inside the loop, you should use the flip method to flip the coin,
the toString method (used implicitly, within a println statement)
to print the results of the flip, and the isHeads method to see if
the result was HEADS. Keeping track of the current run length (the number
of times in a row that the coin was HEADS) and the maximum run length is an
exercise in loop techniques! Take your time thinking about how you will
do this!!
- Have a lab leader sign-off on your pseudo-code solution for this problem before encoding it.
- Print the result after the loop. Here's an example: "Out of 100 flips, there were 48 heads. The longest streak of heads was 3."
Hint: you may find it useful to look at FlipRace.java. The program flips two coins until one of them comes up heads three times in a row.
Submit a zip file, flipCoins.zip, containing the two java files and your lab document showing the output of three runs of the program.