Hi everyone,
I'm reworking an old assignment because I got a low score, due tomorrow. The directions are:
1. Create a class called Dice to represent a single cube. It should have a method called roll() that randomly selects a number from 1 to 6 for the value of the dice.
2. Create a
test main method for the Dice class that creates a Dice, and rolls it many times.
Each time the die is rolled, the Dice value should be stored in an array representing how many times that value has come up.
After a large number of rolls (say, at least 1,000 -- but 1,000,000 should be no problem), the number of times the die rolled 1, 2, 3, 4, 5, and 6 should be printed.
3. How would you describe the results, and why should that be expected?
4. What would happen if you ran the program many times?
5. Create a class called DiceStatistics to represent a pair of dice and the totals of their rolls. It should have an array of two Dice as a private data member (these are the Dice from Problem 1). It should also have, as a data member, an array of integers to represent the possible totals of rolling two dice.
The class should also have the following methods:
* initStats() to initialize all the totals to zero.
* rollOnce() to roll each die once, and add one to the correct totals element
* printStatistics() to print the number of times each total has come up over the number of runs
6. Create a test main method for the DiceStatistics class that creates a DiceStatistics, initializes the statistics, then rolls the dice many times (say, 10000 or so), and then prints the statistics.
7. What is the most likely total, and how likely is it as a percentage?
Notes:
* For the Dice class, you will need a random number generator. The way this is done in
Java is to use the java.util.Random class. After creating an instance, say randomNumber, you can call randomNumber.nextInt(6) to return an integer, randomly distributed from zero to five. You would then add one (since a die goes from one to six, not zero to five) to get the value of the roll.
* Your main test method will have the signature public static void main(
String[] args), as is usual, and the method will create a Dice, and invoke roll() on it many times, adding one to the appropriate value each time. Use a variable for the maximum number of times you want to roll() the die, so you can see what happens as you increase the number.
* DiceStatistics has two arrays in it. One to hold references to the Dice, and the other to hold the counts for the totals from rolling the Dice.
This is my code. I *think* I have it correct through #5, where the DiceStatistics part comes in (but please correct me if I am wrong). I have a successful output, anyway, and my prof didn't say anything.
So now, I am at #5 where I need to create this DiceStatistics code. The sample solution my teacher gave is as follows. I would like to use it simply because I don't understand it, and at this late date I don't have time to understand it.
So my questions are as follows.
1. Is this DiceStatistics program that he wrote compatible with my Dice program?
2. If so, how do I get it to print out the statistic percentages and sums of all the rolls like he wants? Because from my current standpoint, when I hit compile, it's giving me the same output as my Dice program -- I don't see any difference.
Thanks, and I hope that made sense.