Untitled

mail@pastecode.io avatar
unknown
plain_text
3 years ago
949 B
2
Indexable
Never
1 import java.util.Random;
23
/**
4 The Die class simulates a six-sided die.
5 */
67
public class Die
8 {
9 private int sides; // Number of sides
10 private int value; // The die's value
11
12 /**
13 The constructor performs an initial
14 roll of the die.
15 @param numSides The number of sides for this die.
16 */
17
18 public Die(int numSides)
19 {
20 sides = numSides;
21 roll();
22 }
23
24 /**
25 The roll method simulates the rolling of
26 the die.
27 */
28
29 public void roll()
30 {
31 // Create a Random object.
32 Random rand = new Random();
33
34 // Get a random value for the die.
35 value = rand.nextInt(sides) + 1;
36 }
37
38 /**
39 getSides method
40 @return The number of sides for this die.
41 */
42
43 public int getSides()
44 {45 return sides;
46 }
47
48 /**
49 getValue method
50 @return The value of the die.
51 */
52
53 public int getValue()
54 {
55 return value;
56 }
57 }