-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDie.java
83 lines (67 loc) · 1.83 KB
/
Die.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import java.util.Random;
/**
Represents one die with faces showing values between 1 and the
number of faces on the die.
*/
public class Die
{
private static int MIN_SIDES = 4; // no less than 4 sides on a die
private int sides ; // The number of sides
private int value; // The value of the die
private Random rand; // Random number generator
/**
The Default constructor calls the roll method
creates the Random object, sets number of sides to 6 and
sets the intitial value of the die to a random
number.
*/
public Die()
{
// create Random object
rand = new Random();
// set number of side on die
sides = 6;
// Call the roll method to randomly
// set the value of the die.
roll();
}
/**
Alternate constructor calls the roll allows user to specify
number of side son die. The method creates the Random object,
sets number of sides to value user specified and sets the intitial
value of the die to a random number.
@param numSides The number of sides on the die
*/
public Die(int numSides)
{
// create Random object
rand = new Random();
// set number of side on die
if ( numSides < MIN_SIDES){
sides = MIN_SIDES;
}
else{
sides = numSides;
}
// Call the roll method to randomly
// set the value of the die.
roll();
}
/**
The roll method sets the value of the die
to a random number.
*/
public void roll()
{
// Set the value to a random number.
value = rand.nextInt(sides) + 1;
}
/**
The getValue method returns the value
of the die.
*/
public int getValue()
{
return value;
}
}