Modifying the Coin Class
- 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 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 parameter -- that
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.
- Compile your class to make sure you have no syntax errors.
- Modify the Runs program from the last exercise to use a BiasedCoin.