Dice
, some Players, and of course the Simulator
.
Dice
Dice
is provided. Read it to make sure that you understand its functionality. You need to implement method roll()
according to specification.
Dice
multiple times, record the outcomes, and calculate some statistics. You will implement the Player class.
A Player has
String name
Dice d
: references a dice
int[] outcome
: references a 1-d array of the outcomes
int[] count
: references a 1-d array of the frequencies of the outcomes
private
.
A Player
has the following methods:
Player
has a name and a dice. The constructor initializes the field name
and instantiates a Dice
object.public Player(String name, int numFaces)
- Method
play
rolls the dice numRolls
times and records the outcomes. (Note that this method needs to instantiate the arrays outcome
and count
.)
public void play(int numRolls)
- Calculate the average frequency for all outcomes. E.g., if the counts for outcomes 1..4 (a 4-sided dice) are 2,4,2,4, then the average frequency is 3.
public double meanCount()
- Find the face that shows face-up with the highest frequency. If multiple faces has the highest frequency, then return any one of them.
public int faceMaxCount()
- Report the statistics of the game: display the frequencies for all outcomes, the average frequency, and the face with the highest frequency.
public void report()
Simulator
main
method, prompt the user for the number of sides of the Dice
and the number of times to roll. Instantiate one Player
(any name). Then the Player
plays the dice game and reports the statistics.
Dice
to implement a trickRoll
method. In a "trick roll," the probability of getting face 1 is x
times as high as getting face 2, x
times as high as getting face 3, x
times as high as getting face 4, etc. The probabilities of getting face 2, face 3, etc, (other than face 1) are equal.
public void trickRoll(int x)Now class
Player
needs a trickPlay
method. trickPlay
has the same functionality as play
but uses the trickRoll
method instead of roll
. trickPlay
has an extra parameter specifying the "weight" of the trick roll (corresponding to parameter x
in method trickRoll
).
public void trickPlay(int numRolls, int weight)Finally, add more code in the
main
method of class Simulator
. Instantiate another Player
. Both Player
s have Dice
with the same number of faces and they roll the same number of times. Player
1 uses the normal roll and Player
2 does trick rolls! Player
2's trick roll gives face 1 three times as often as face 2 (face 3, face 4, ...). Report each Player
's game statistics.
Something to look forward to: Have you noticed that we're adding a lot of code to accommodate the trick roll? First you add the method trickRoll
, then you have to add trickPlay
, and then you have the Player
call the method trickPlay
. In the process, we have duplicated a bit of code. Imagine how much work it would be if we have more tricks up our sleeves?! There is a more elegant way to deal with this situation... Think inheritance! We'll discuss this another time.
In an ASCII text file analysis.txt
, answer the following questions.
Player
). What is the ratio : for Player 1? For Player 2?
Player
). What is the ratio : for Player 1? For Player 2?
Player
). What is the ratio : for Player 1? For Player 2?
trickRoll
method is correctly implemented according to our specification? Why?
Submit your completed files Dice.java
, Player.java
, Simulator.java
, and analysis.txt
on-line using CMS (Course Management System) before the project deadline. Make sure you are submitting the correct, up to date, .java files (not .class or .java~). We will not accept any files after the deadline for any reason (except for documented medical reasons). See the CMS link on the web page for instructions on using CMS. If necessary, turn off the DrJava feature that saves the .java~ files (see course webpage announcement on 2/17 for instructions).