AP Computer Science Name: _______________

advertisement
AP Computer Science
Name: _______________
1. Create a Coin class with the following:
Data:
Constants: HEADS and TAILS
int value – value of the coin (25 is a quarter, 10 is a dime).
int state – current state: HEADS or TAILS
int bias - number out of 100 flips the coin should come up HEADS (percentage).
(default=50 is a standard coin, =60 means 60% of flips will be HEADS)
Methods:
2 Constructors – default sets the coin to a quarter, the other should allow you to
specify the value of the coin. Both should call flip() to randomly set state
of the coin.
flip() – call a random generator
isHeads() – returns Boolean
isTails() – returns Boolean
toString() – display “HEADS” or “TAILS”
2. Create a Client class which creates a Coin object, flips it 100 times, and displays the
results of the first 10 flips, and then displays the percentage of Heads for the 100 flips.
Tails
Tails
Heads
Heads
Tails
Heads
Heads
Heads
Heads
Heads
percent: 52% HEADS
3. Modify your Coin class to support a bias feature.
Add a method, setBias(int numHeadsPer100Flips)
Modify your flip() method to support the bias feature.
Repeat step 2 with the bias feature.
4. Create a Client class which creates 3 Coin objects. Write a program where you flip
all the coins at the same time and determine which one comes up heads 4 consecutive
times first. Display the output for each flip:
flip
1
2
3
4
5
6
player 1
Heads
Tails
Heads
Heads
Heads
Heads
player 1
Tails
Tails
Heads
Tails
Tails
Heads
player 3
Heads
Heads
Tails
Heads
Heads
Tails
player 1 wins!!
5. Repeat step 4 and set the bias for 1 of the coins to 60/100.
**Teacher of this course does not condone cheating and gambling and is only using this
exercise to inject the random generator and probability into the exercise.
Download