Question
public class Coin { /** Internal representation of coin showing heads. */ public static final int HEADS = 0; /** Internal representation of coin showing
public class Coin {
/** Internal representation of coin showing heads. */
public static final int HEADS = 0;
/** Internal representation of coin showing tails. */
public static final int TAILS = 1;
/** Coin's current face showing. */
private int face;
/**
* Constructs a Coin object and flips it to give it a starting value.
*/
public Coin() {
flip();
}
/**
* Flips this Coin by randomly choosing a face value.
*/
public void flip() {
face = (int) (Math.random() * 2);
}
/**
* Returns true if the current face of the Coin is heads.
* @return true if current face is heads, else false.
*/
public boolean isHeads() {
return (face == HEADS);
}
/**
* Returns the current face value of this Coin as a String.
* @return toString description
*/
public String toString() {
String faceName;
if (face == HEADS) {
faceName = "Heads";
} else {
faceName = "Tails";
}
return faceName;
}
}
There is a very simple Coin class in chapter 5 of the text. You can find it in the COMP 1510 Examples project in the chapter 5 package. Copy it to your lab 9 project.
Lets write a program called CoinRunner. CoinRunner only contains a main method.
Inside the main method, you will need to declare and instantiate a Coin.
Using a for loop, flip the coin 100 times. Determine the longest run of heads in 100 flips
of the coin:
Suppose we flip the coin 10 times: H, H, H, H, T, T, T, H, H, T. The longest run of
heads in this case is 4.
Suppose we flip the coin 5 times: H, T, H, H, T. The longest run of heads in this
case is 2.
Your program should print out the length of the longest run of heads. For example, we
flipped a coin 10 times and the output looked like this:
Tails Tails Heads Heads Heads Heads Heads Heads Tails Heads The longest run of heads is 6
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started