Die.java
1    /* 
2     * Model a die in terms of two properties: 
3     * - order, the number of faces 
4     * -top, the value of the top face 
5     */
6    
7    package chance;
8    
9    public class Die {
10   
11       // THE INSTANCE VARIABLES (STATE)
12   
13       private int order;
14       private int top;
15   
16       // THE CONSTRUCTORS
17   
18       public Die() {
19           order = 6;
20           top = (int) ( ( Math.random() * 6 ) + 1);
21   
22       }
23   
24       public Die(int nrOfSides) {
25           order = nrOfSides;
26           top = (int) ( ( Math.random() * nrOfSides ) + 1);
27       }
28   
29       // THE METHODS (BEHAVIOR)
30   
31       public int top() {
32           return top;
33       }
34   
35       public void roll() {
36           top = (int) ( ( Math.random() * order ) + 1);
37       }
38   }