generated from ZipCodeCore/OldTexasCode2
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathDie.java
126 lines (101 loc) · 3.52 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import java.util.Random;
/**
* Models a playing die with sides numbered 1 to N.
* All sides have uniform probablity of being rolled.
*
* @author Summer CS 307 class
*/
public class Die
{ public static final int DEFAULT_SIDES = 6;
private static Random ourRandNumGen = new Random();
private final int iMyNumSides;
private int iMyResult;
/**
* Default constructor.<p>
* pre: none<br>
* post: getNumSides() = DEFAULT_SIDES, getResult() = 1
*/
public Die()
{ this(DEFAULT_SIDES);
}
/**
* Create a Die with numSides sides<p>
* pre: numSides > 1<br>
* post: getNumSides() = numSides, getResult() = 1<br>
* An exception will be generated if the preconditions are not met
*/
public Die(int numSides)
{ assert numSides > 1 : "Violation of precondition: numSides = " + numSides + "numSides must be greater than 1";
iMyNumSides = numSides;
iMyResult = 1;
assert getResult() == 1 && getNumSides() == numSides;
}
/**
* Create a Die with numSides and top side and result set to result<p>
* pre: numSides > 1, 1 <= result <= numSides<br>
* post: getNumSides() = numSides, getResult() = 1<br>
* An exception will be generated if the preconditions are not met
*/
public Die(int numSides, int result)
{ assert numSides > 1 && 1 <= result && result <= numSides : "Violation of precondition";
iMyNumSides = numSides;
iMyResult = result;
}
/**
* roll this Die. Every side has an equal chance of being the new result<p>
* pre: none<br>
* post: 1 <= getResult() <= getNumSides()
* @return the result of the Die after the roll
*/
public int roll()
{ iMyResult = ourRandNumGen.nextInt(iMyNumSides) + 1;
assert ( 1 <= getResult() ) && ( getResult() <= getNumSides() );
return iMyResult;
}
/**
* return how many sides this Die has<p>
* pre: none<br>
* post: return how many sides this Die has
* @return the number of sides on this Die
*/
public int getNumSides()
{ return iMyNumSides; }
/**
* get the current result or top number of this Die<p>
* pre: none<br>
* post: return the number on top of this Die
* @return the current result of this Die
*/
public int getResult()
{ return iMyResult; }
/**
* returns true if this Die and the parameter otherObj are equal<p>
* pre: none<br>
* post: return true if the parameter is a Die object with the same number of sides as this Die and currently has the same result.
* @return true if the the two Dice are equal, false otherwise
*/
public boolean equals(Object otherObj)
{ boolean result = true;
if(otherObj == null)
result = false;
else if(this == otherObj)
result = true;
else if(this.getClass() != otherObj.getClass())
result = false;
else
{ Die otherDie = (Die)otherObj;
result = this.iMyResult == otherDie.iMyResult
&& this.iMyNumSides == otherDie.iMyNumSides;
}
return result;
}
/**
* returns a String containing information about this Die<p>
* pre: none<br>
* post: return a String with information about the current state of this Die
* @return: A String with the number of sides and current result of this Die
*/
public String toString()
{ return "Num sides " + getNumSides() + " result " + getResult();
}
}// end of Die class