-
Notifications
You must be signed in to change notification settings - Fork 0
/
I_a.java
255 lines (229 loc) · 7.21 KB
/
I_a.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import java.util.Random;
/**
* Abstract parent class for I_a (fish) species.
* @author Lisa Miller
* @since 2/11/22
*/
public abstract class I_a implements Comparable<I_a> {
/*** instance constants, these are fixed for each type. ***/
/** The Hawaiian name. */
protected final String name;
/** The English name. */
protected final String englishName;
/** The scientific name. */
protected final String scientificName;
/** The maximum length. */
protected final double maxLength;
/** The minimum length. */
protected final double minLength;
/** The diet eaten. */
protected final String[] diet;
/*** instance variables, these are (possibly) variable for each fish. ***/
/** The actual fish length in inches. */
protected double length;
/** The fish weight in ounces. */
protected double weight;
/** The body color. */
protected String bodyColor;
/** The fin color. */
protected String finColor;
/** The fishes' sex. */
protected String sex;
/** The growth rate max per food eaten.*/
protected double growthRate = 0.8;
/**
* constructor.
* initial length should be generated in subclass.
*@param name This fish type's Hawaiian name.
*@param englishName The fish type's English name.
*@param scientificName The fish species's scientific name.
*@param maxLength The maximum length this name applies.
*@param minLength The minimum length this name applies.
*@param length This fish's size.
*@param weight This fish's weight.
*@param diet The diet preference of this fish type.
*@param bodyColor The body color of this fish.
*@param finColor The fin color of this fish.
*@param sex The sex of this fish.
*@throws FishSizeException if length exceeds maxLength or is less than minLength
**/
public I_a(String name, String englishName, String scientificName,
double maxLength, double minLength, double length, double weight, String[] diet,
String bodyColor, String finColor, String sex) throws FishSizeException {
//set instance variables
this.name = name;
this.englishName = englishName;
this.scientificName = scientificName;
this.maxLength = maxLength;
this.minLength = minLength;
if (length >= minLength && length < maxLength) {
this.length = length;
} else {
throw new FishSizeException(name + " length must be within " + minLength
+ " and " + maxLength + " inches long. But was sent " + length);
}
this.weight = weight;
this.diet = diet;
this.bodyColor = bodyColor;
this.finColor = finColor;
this.sex = sex;
}
/*** abstract methods to be implemented in the sub-classes ****/
/**
* Sets the fish's length to newLength.
* @param newLength the new fish length.
* @throws FishSizeException if the new length is not within min-max length for type
*/
public abstract void setLength(double newLength);
/**
* Sets the fish's weight to newWeight.
* @param newWeight the fish's new weight.
*/
public abstract void setWeight(double newWeight);
/**
* Models eating, should call grow under certain conditions.
* @param food A String with a food name
* @throws FishFoodException if the food is not the fish's diet
*/
public abstract void eat(String food);
/**
* Should be used by eat method to increase length of fish.
* Should determine a new length and internally call setLength
* @throws FishSizeException if the new length is not within min-max length for type
*/
protected abstract void grow();
/** returns new I_a of next level
* When a fish reaches maxSize, it should transform to next
* level Hawaiian type.
* @return a new I_a of the next level type, should replace caller
*/
protected abstract I_a levelUp();
//########### Protected class method ################
/** initialize random fish sex
* choose randomly male or female
* used by some subclasses.
*/
protected void initSex() {
//randomize sex
Random ran = new Random();
int flip = ran.nextInt(2);
if (flip == 0) {
this.sex = "male";
} else {
this.sex = "female";
}
}
/*** public class methods - methods used by all sub-classes ***/
/**
* Returns I_a information as a formatted String.
* @return String representing I_a object data.
*/
public String toString() {
String s = "Name: " + this.name + "\n";
s = s + "English name: " + this.englishName + "\n";
s = s + "Scientific name: " + this.scientificName + "\n";
s = s + "Length: " + this.length + "\n";
s = s + "Weight: " + this.weight + "\n";
s = s + "Body color: " + this.bodyColor + "\n";
s = s + "Fin color: " + this.finColor + "\n";
s = s + "Sex: " + this.sex;
return s;
}
/** Get Methods **/
/**
* Gets the Hawaiian name.
* @return The Hawaiian name of this fish.
*/
public String getName() {
return this.name;
}
/**
* Gets the English name.
* @return The English name of this fish.
*/
public String getEnglishName() {
return this.englishName;
}
/**
* Gets the Scientific name.
* @return The scientific name of this fish.
*/
public String getScientificName() {
return this.scientificName;
}
/**
* Gets the maximum length for this fish type.
* @return The maximum length.
*/
public double getMaxLength() {
return this.maxLength;
}
/**
* Gets the minimum length for this fish type.
* @return The minimum length.
*/
public double getMinLength() {
return this.minLength;
}
/**
* Gets the length.
* @return This fish's length.
*/
public double getLength() {
return this.length;
}
/**
* Gets the fish type diet.
* @return This fish type's diet.
*/
public String[] getDiet() {
return this.diet;
}
/**
* Gets the body color.
* @return The fish's general body color.
*/
public String getBodyColor() {
return this.bodyColor;
}
/**
* Gets the fin color.
* @return bodyColor The fish's general fin color.
*/
public String getFinColor() {
return this.finColor;
}
/**
* Gets this fish's sex.
*@return The fish's sex
*/
public String getSex() {
return this.sex;
}
/**************** Required for Comparable ******************/
/**
* compareTo method imposes sorting order on I_a.
* @param i The I_a being compared to this
* @return 0 if equal, < 0 if this comes before i, > 0 if this comes after i
*/
public int compareTo(I_a i) {
int diff = (int) ((this.length - i.getLength())*100);
if ( diff == 0) {
String s1 = this.name.toLowerCase();
String s2 = i.getName().toLowerCase();
diff = s1.compareTo(s2);
}
return diff;
}
/**
* compares two I_a for equality.
* @param i The I_a being compared to this
* @return true if the two I_a are equal, false if not
*/
public boolean equals(I_a i) {
if (this.compareTo(i) == 0) {
return true;
}
return false;
}
} //close class