-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyRandomNumber.java
More file actions
51 lines (41 loc) · 1.47 KB
/
MyRandomNumber.java
File metadata and controls
51 lines (41 loc) · 1.47 KB
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
public final class MyRandomNumber extends RandomNumber {
protected int minimum; // the lowest number the random object should create
protected int maximum; // the highest number the random object should create
//Constructor
public MyRandomNumber() {
this.SetMinimum(1); //default min value
this.SetMaximum(10); //default max value
}
//Overloaded Constructor. Set min and max as required
public MyRandomNumber(int min, int max) {
this.SetMinimum(min);
this.SetMaximum(max);
}
//getters
public int GetMinimum() {return this.minimum;}
public int GetMaximum() {return this.maximum;}
//setters
public void SetMinimum(int min) {
this.minimum = min;
}
public void SetMaximum(int max) {
this.maximum = max;
}
//Random Number Generator.
@Override
public int generateRandomNumber() {
//In case of min and max are equal, returns the minimum
if (this.minimum == this.maximum) {
return this.minimum;
}
//In case of min and max are swapped, swap back before getting the random number
if (this.minimum > this.maximum) {
int temp = this.maximum;
this.maximum = this.minimum;
this.minimum = temp;
}
//random number being created between min and max
currentRandomNumber = random.nextInt(this.maximum) + this.minimum;
return currentRandomNumber;
}
}//END class