The Dice Problem
- Write a class called Die. (That's the singular form of the word "dice" for those of you who loathe ambiguity.) The Die class should have the following:
In public static void main(), create an array of four six-sided Die objects. We will discuss how to do this in class; if you miss class, find a classmate or find me to make sure you can do this.
Roll four dice 1000 times and display the number of times each sum is rolled.
- A constructor that takes a number of sides as its input;
- An appropriate field to store that number of sides;
- An appropriate field to store the current value of the die (whatever is facing up after the die was last rolled);
- A method roll() that randomly generates a number between 1 and the number of sides--you will need to be clever with Math.random(); you can decide whether roll() should be of type void or type int;
- A method getValue() that returns result that roll() produced; if a Die has not been rolled, getValue() should return 0.
(1 XC point) If you want to go for the glory, display these results in a histogram. You will need an array to store results. Fancy colors are unnecessary. You will also need a class that extends JFrame. Just make clear what the numbers are and that the bars are of appropriate length.
Here is the definition of a histogram.