Question
Modifying Coin Class Given Below 1. Create a new class named BiasedCoin that models a biased coin (heads and tails are not equally likely outcomes
Modifying Coin Class Given Below
1. Create a new class named BiasedCoin that models a biased coin (heads and tails are not equally likely outcomes of a flip). To do this modify the coin class from the Listing 5.4 of text (in the file Coin.java) as follows: Add a private data member bias of type double. This data member will be a number between 0 and 1 (inclusive) that represents the probability the coin will be HEADS when flipped. So, if bias is 0.5, the coin is an ordinary fair coin. If bias is 0.6, the coin has probability 0.6 of coming up heads (on average, it comes up heads 60% of the time). Modify the default constructor by assigning the value 0.5 to bias before the call to flip. This will make the default coin a fair one. Modify flip so that it generates a random number then assigns face a value of HEADS if the number is less than the bias; otherwise it assigns a value of TAILS. Add a second constructor with a single double parameterthat parameter will be the bias. If the parameter is valid (a number between 0 and 1 inclusive) the constructor should assign the bias data member the value of the parameter; otherwise it should assign bias a value of 0.5. Call flip (as the other constructor does) to initialize the value of face.
2. Compile your class to make sure you have no syntax errors.
3. Write a program that uses three BiasedCoin objects. Instantiate one as a fair coin using the constructor with no parameter. Read in the biases for the other two coins and instantiate those coins using the constructor with the bias as a parameter. Your program should then have a loop that flips each coin 100 times and counts the number of times each is heads. After the loop print the number of heads for each coin. Run the program several times testing out different biases.
//********************************************************************
// Coin.java Author: Lewis/Loftus
//
// Represents a coin with two sides that can be flipped.
//********************************************************************
public class Coin
{
private final int HEADS = 0;
private final int TAILS = 1;
private int face;
//-----------------------------------------------------------------
// Sets up the coin by flipping it initially.
//-----------------------------------------------------------------
public Coin ()
{
flip();
}
//-----------------------------------------------------------------
// Flips the 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.
//-----------------------------------------------------------------
public boolean isHeads ()
{
return (face == HEADS);
}
//-----------------------------------------------------------------
// Returns the current face of the coin as a string.
//-----------------------------------------------------------------
public String toString()
{
String faceName;
if (face == HEADS)
faceName = "Heads";
else
faceName = "Tails";
return faceName;
}
}
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