1 |
import java.util.Random; |
2 |
|
3 |
/** |
4 |
This class models a die that, when cast, lands on a random |
5 |
face. |
6 |
*/ |
7 |
public class Die |
8 |
{ |
9 |
/** |
10 |
Constructs a die with a given number of sides |
11 |
@param s the number of sides, e.g. 6 for a normal die |
12 |
*/ |
13 |
public Die(int s) |
14 |
{ |
15 |
sides = s; |
16 |
generator = new Random(); |
17 |
} |
18 |
|
19 |
/** |
20 |
Simulates a throw of the die |
21 |
@return the face of the die |
22 |
*/ |
23 |
public int cast() |
24 |
{ |
25 |
return 1 + generator.nextInt(sides); |
26 |
} |
27 |
|
28 |
|
29 |
private Random generator; |
30 |
private int sides; |
31 |
} |