Die.java
1    /* 
2     * Model a die in terms of the 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
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       public Die(int nrOfSides) {
24           order = nrOfSides;
25           top = (int)((Math.random()*nrOfSides)+1);
26       }
27   
28       // the methods (behavior) 
29   
30       public int top() {
31           return top;
32       }
33   
34       public void roll() {
35           top = top = (int)((Math.random()*order)+1);
36       }
37   
38       public String toString() {
39           String thisString = "d"+ order + ": " + top();
40           return thisString;
41       }
42   }
43