Read carefully and think about implementing the classes carefully--developing a concise implementation is part of the work in this project. Whenever possible, reduce redundancy in your code by calling other (existing) methods. Program development is an iterative process--sometimes you will write code under a method and later decide that you should take part of that code out and set it up as a separate method. This iterative process is normal--you should not expect your first implementation to be perfect. Look for improvement as (after) you write code.
Let's first look at some example output so that you know what to expect. Notice that the histogram you will draw has horizontal "bars" (rows of asterisks).
Experiment One: Enter number of Dice you want to create: 2 Creating the 1th Die Enter the number of faces of for the Die: 6 Which side is weighted more, 1-6? Enter 0 if it's a fair die: 0 Creating the 2th Die Enter the number of faces of for the Die: 6 Which side is weighted more, 1-6? Enter 0 if it's a fair die: 0 Start simulation... Enter the number of trials: 100000 Calculating sum of two dice... Enter the first die: 1 Enter the second die: 2 The count distribution for the sum of Dice 1 and 2: 1 2 ** 3 *** 4 ***** 5 ******* 6 ******** 7 ********** 8 ******** 9 ******* 10 ***** 11 *** 12 **Another example run is shown at the end of the project statement.
Dice
Dice
has
int top
: Top face
int faces
: Number of faces on the Dice
int[] weight
: weight[i]
is the likelihood of face i
being the top face. We say side i
has weight weight[i]
. A fair Dice
has weight[i]=1
for all i
. A trick dice (say 6-sided) where face 2 is three times more likely to show face-up than face 1, face 3, face 4, ..., etc., has the values {0,1,3,1,1,1,1} in array weight
. Note that we have chosen to keep an extra cell (at index 0). You may decide for yourself whether to use this extra cell. As always, write clear and concise comments to document your code. A Dice
has at most one "weighted side" (face 2 in the previous example).
Dice
has the following instance methods:
/** A Dice has numFaces sides and the topFace is random */
public Dice(int numFaces)
/** An unfair Dice has numFaces sides. Face weightedSide has weight w. The
topFace is random */
public Dice(int numFaces, int weightedSide, int w)
roll
to roll the Dice
based on its array weight
:
/** top gets a value in 1..faces */
public void roll()
topFace()
and numberOfFaces()
DiceSet
DiceSet
is a set of Dice
, of course! A DiceSet
has
Dice[] set
: references an array of Dice
int[][] outcome
: references a 2-d array of outcomes
outcome[d][j]
is the top face value of the d
th Dice
after the j
th roll.
int[][] count
: references a 2-d array of the counts of the outcomes
count[d][j]
: is the number of times that face j
has shown up for Dice
d
. Note that count
may be a ragged array.
double MAXSTARS
, which has the value 10.0. This is the maximum number of asterisks (stars) on any row in the histogram.
A DiceSet
has the following methods:
createDiceSet
creates an instance of DiceSet
by prompting the user for the necessary values to create a DiceSet
(see example runs). This method serves the purpose of the constructor:
public static DiceSet createDiceSet()
DiceSet
set
array
outcome
and count
in this method.
Dice
in the DiceSet
, so the creation of the a Dice
, i.e., the call of the Dice
constructor, should appear inside a loop.
Dice
is weighted.
runSimulation
takes as parameter the number of rolls and fill the outcome
and count
arrays. Before you fill the arrays, of course you need to finish creating them. Recall that in the method createDiceSet
you have created only the first dimension. In this method you must instantiate the arrays for the second dimension.
public void runSimulation(int trials)
Dice
is rolled trials
number of times. Note the terminology: a trial means rolling each Dice
in the DiceSet
once.
calculateCount
computes the counts for an array of outcomes, i.e., it computes the data that can be used later for drawing a histogram. The computed counts are stored in a 1-d array and returned.
public static int[] calculateCount(int[] outcomes)
outcomes
references an array with the values {2,4,3,1,2,3,2,4,4,1}, then the returned array storing the counts should be {0,2,3,2,3} since value 1 occurs 2 times, value 2 occurs 3 times, value 3 occurs 2 times, and value 4 occurs 3 times. This method needs to determine the length of the array to return! Again, your array may not have the first cell with value zero. It is up to you to design your code.
plotHistogram
draws a histogram with horizontal bars (rows of asterisks) given a 1-d array data
. The longest bar is always MAXSTARS
long--we call such a histogram a normalized histogram. In normalizing the bars, always round to the nearest number.
public static void plotHistogram(int[] data)
data
references an array with the values {0,2,3,2,3}, then the following histogram should be drawn:
1 ******* 2 ********** 3 ******* 4 **********Again, your
data
array may not have the first cell with value zero. It is up to you to design your code.
printCount
display the count distribution (histogram) for each Dice
in the DiceSet
. Display a title above each histogram. See example output at the end of the project statement.
public void printCount()
getPairOutcomeSum
calculates the sum of the faces shown by two specified Dice
for each trial. The resulting sums are stored in an array and returned.
/** = array of sums of outcomes for Dice d1 and d2 */
public int[] getPairOutcomeSum(int d1, int d2)
countPairSameFace
that counts the number of times that two specified Dice
show the same face in a trial.
/** = number of times Dice d1 and d2 both show face f */
public int countPairSameFace(int f, int d1, int d2)
- instance method
probabilityAnyPairSameFace
estimates the probability of any two Dice
in the DiceSet
showing the same face in a trial.
/** = probability of any pair of Dice showing face f in a trial */
public double probabilityAnyPairSameFace(int f)
You must use simulation to estimate the probability. The probability of any pair of Dice
showing face f is estimated by dividing the number of times that at least two Dice
show face f with the number of trials.
P5
for simulation
main
method, you will run several experiments.
MAXSTARS
in class DiceSet
to dramatize the results!
Experiment Two: Enter number of Dice you want to create: 2 Creating the 1th Die Enter the number of faces of for the Die: 6 Which side is weighted more, 1-6? Enter 0 if it's a fair die: 0 Creating the 2th Die Enter the number of faces of for the Die: 6 Which side is weighted more, 1-6? Enter 0 if it's a fair die: 3 Enter the weight for side 3: 5 Start simulation... Enter the number of runs: 100000 The count distribution of Dice 1: 1 ********** 2 ********** 3 ********** 4 ********** 5 ********** 6 ********** The count distribution of Dice 2: 1 ** 2 ** 3 ********** 4 ** 5 ** 6 **
Submit your files Dice.java
, DiceSet.java
, and P5.java
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).