-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBear.java
More file actions
85 lines (71 loc) · 1.85 KB
/
Bear.java
File metadata and controls
85 lines (71 loc) · 1.85 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
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
import java.util.Random;
/**
* Models a bear in the river ecosystem.
*
* @author Jackson Eshbaugh
* @version 1/27/2024
*/
public class Bear extends Animal {
private final int MAX_AGE = 9;
/**
* Creates a bear of random age
* (within a bear's normal
* lifespan) and random gender.
*/
public Bear() {
// Pick a random age ...
Random r = new Random();
this.age = r.nextInt(MAX_AGE + 1);
// ... and a random gender
this.gender = Animal.generateRandomGender();
}
/**
* Creates a bear of specified age and gender.
* If the specified age is larger than the
* maximum age of a bear, the age is set to that
* maximum.
*
* @param age The age of the animal
* @param gender The gender of the animal
*/
public Bear(int age, Gender gender) {
super(age, gender);
if(this.age > MAX_AGE)
this.age = MAX_AGE;
}
@Override
public boolean maxAge() {
return age == MAX_AGE;
}
@Override
public boolean incrAge() {
// Don't increment age if at max age.
if(maxAge()) return false;
age++;
return true;
}
/**
* Get this bear's strength,
* a function of its age.
*
* @return The strength of the bear, an
* integer value from 0 to 5.
*/
public int getStrength() {
if(age <= 4) return age + 1;
return 9 - age;
}
/**
* Renders this animal as a
* String value of the form
* {@code BGA}, where {@code B} stands
* for Bear, {@code G} is gender and
* {@code A} is age.
*
* @return the String value "BGA" described above.
*/
@Override
public String toString() {
return "B" + super.toString();
}
}