You must implement a class called Guppy which contains the f
You must implement a class called Guppy which contains the following elements:
1. The following nine public symbolic constants. Use these variable names
and data types. Remember symbolic constants are static and final. Do
not use different names or data types, and do not add any other symbolic
constants. Use these constants instead of magic numbers inside your code:
(a) YOUNG_FISH an integer equal to 10
(b) MATURE_FISH an integer equal to 30
(c) MAXIMUM_AGE_IN_WEEKS an integer equal to 50
(d) MINIMUM_WATER_VOLUME_ML a double equal to 250.0
(e) DEFAULT_GENUS a String equal to “Poecilia”
(f) DEFAULT_SPECIES a String equal to “reticulata”
(g) DEFAULT_HEALTH_COEFFICIENT a double equal to 0.5
(h) MINIMUM_HEALTH_COEFFICIENT a double equal to 0.0
(i) MAXIMUM_HEALTH_COEFFICIENT a double equal to 1.0
2. The following eight private instance variables. Use these variable names
and data types. Do not use different names or data types, and do not add
any other instance variables:
(a) genus (first word of the scientific binomial two-part name) a String
(b) species (second word of the scientific binomial name) a String
(c) ageInWeeks an integer
(d) isFemale a boolean
(e) generationNumber an integer
(f) isAlive a boolean
(g) healthCoefficient a double
(h) identificationNumber an integer
3. numberOfGuppiesBorn, a private static integer that has an initial value
of 0. This variable is incremented by 1 each time a new guppy is constructed,
and the newly incremented value is assigned to the new guppy’s
identificationNumber instance variable. Further details are provided
in the section about constructors (remember that static variables
are shared by all objects of a class).
4. Two (2) constructors:
(a) A zero-parameter constructor which sets ageInWeeks and generationNumber
to zero, and sets:
i. genus to DEFAULT_GENUS
ii. species to DEFAULT_SPECIES
iii. isFemale to true
iv. isAlive to true
v. healthCoefficient to DEFAULT_HEALTH_COEFFICIENT
vi. identificationNumber to the newly incremented value of
numberOfGuppiesBorn
(b) A second constructor which accepts the following parameters in the
following order:
i. newGenus a String. If the argument passed as the parameter
is either null or an empty String, set the genus instance variable
to DEFAULT_GENUS. Otherwise format the String passed in
newGenus correctly (capital first letter, the rest lower case) and
assign the new String to the genus instance variable.
ii. newSpecies a String. If the argument passed as the parameter
is either null or an empty String, set the species instance variable
to DEFAULT_SPECIES. Otherwise format the String passed
in newSpecies correctly (lower case) and assign the new String
to the species instance variable.
iii. newAgeInWeeks an integer. If the argument passed as the parameter
is less than zero or greater than or equal to
MAXIMUM_AGE_IN_WEEKS, set the ageInWeek instance variable
to zero. Otherwise assign newAgeInWeeks to the ageInWeeks
instance variable.
iv. newIsFemale a boolean to assign to the isFemale instance
variable.
v. newGenerationNumber an integer. If the argument passed as
the parameter is less than zero, set the generationNumber instance
variable to zero. Otherwise assign newGenerationNumber
to the generationNumber instance variable.
vi. newHealthCoefficient a double. If the argument passed as
the parameter is less than MINIMUM_HEALTH_COEFFICIENT or
greater than MAXIMUM_HEALTH_COEFFICIENT, set the
healthCoefficient instance variable to
DEFAULT_HEALTH_COEFFICIENT. Otherwise assign
newHealthCoefficient to the healthCoefficient instance
variable.
vii. This constructor should not accept a parameter for the isAlive
instance variable. Set the isAlive variable to true. Every new
Guppy is alive by default.
(c) Remember that both constructors must increment the static
numberOfGuppiesBorn variable by 1 and and assign the new value
to identificationNumber.
5. A method with the header public void incrementAge() which increases
the value in the ageInWeeks instance variable field by 1. If the
new value in ageInWeeks is equal to MAXIMUM_AGE_IN_WEEKS, set the
isAlive instance variable to false.
6. An accessor (getter) and a mutator (setter) for each of the eight instance
variables with one exception: do not create a mutator for
identificationNumber. Every accessor method name must follow the
pattern getVariableName. Every mutator method name must follow
the pattern setVariableName. Also create a static accessor for the
numberOfGuppiesBorn static variable called
public static int getNumberOfGuppiesBorn(). The mutators
for genus and species must ignore null or empty Strings, and correctly
format and store any other new values. The mutator for ageInWeeks
must ignore values below 0 and above MAXIMUM_AGE_IN_WEEKS. The mutator
for generationNumber must ignore negative values. The mutator
for healthCoefficient must ignore values that would cause the
healthCoefficient variable to exceed its bounds.
7. A method with the header public double getVolumeNeeded() which
returns the volume of water in millilitres that the guppy needs according
to the following formula:
• If the fish is less than 10 weeks old, return MINIMUM_WATER_VOLUME_ML
• If the fish is 10 to 30 weeks old, return MINIMUM_WATER_VOLUME_ML
* ageInWeeks / YOUNG_FISH.
• If the fish is 31 to 50 weeks old, return MINIMUM_WATER_VOLUME_ML
* 1.5.
• If the fish is older than 50 weeks, return 0.0.
8. a method with the header
public void changeHealthCoefficient(double delta)
which adds the value passed in the delta parameter to the
healthCoefficient instance variable. The parameter delta may be
positive or negative, but if the new value of healthCoefficient would
be less than or equal to MINIMUM_HEALTH_COEFFICIENT, in addition to
setting the value of healthCoefficient to 0.0, set the isAlive instance
variable to false. If the new value of healthCoefficient would
be greater than MAXIMUM_HEALTH_COEFFICIENT, set healthCoefficient
to MAXIMUM_HEALTH_COEFFICIENT.
9. a method with the header public String toString() which returns
a String representation of the guppy. The String should begin with [ and
end with ]. Inside these square brackets, print name of each instance
variable, followed by an equal sign (no spaces!) followed by the value of
each instance variable in the order in which they are listed in section 3.2,
separated by commas and without any spaces. This String should be one
(possibly long) line with no new line characters or spaces in it.
Solution
public class Guppy {
static final int YOUNG_FISH = 10;
static final int MATURE_FISH = 30;
static final int MAXIMUM_AGE_IN_WEEKS = 50;
static final double MINIMUM_WATER_VOLUME_ML = 250.0;
static final String DEFAULT_GENUS = \"Poecilia\";
static final String DEFAULT_SPECIES = \"reticulata\";
static final double DEFAULT_HEALTH_COEFFICIENT = 0.5;
static final double MINIMUM_HEALTH_COEFFICIENT = 0.0;
static final double MAXIMUM_HEALTH_COEFFICIENT = 1.0;
private String genus;
private String species;
private int ageInWeeks;
private boolean isFemale;
private int generationNumber;
private boolean isAlive;
private double healthCoefficient;
private final int identificationNumber;
private static int numberOfGuppiesBorn = 0;
public Guppy() {
this.ageInWeeks = 0;
this.generationNumber = 0;
this.genus = DEFAULT_GENUS;
this.species = DEFAULT_SPECIES;
this.isFemale = true;
this.isAlive = true;
this.healthCoefficient = DEFAULT_HEALTH_COEFFICIENT;
this.identificationNumber = ++numberOfGuppiesBorn;
}
public Guppy(String newGenus, String newSpecies, int
newAgeInWeeks, boolean newIsFemale, int newGenerationNumber, double
newHealthCoefficient) {
if (newAgeInWeeks < 0 || newAgeInWeeks >= MAXIMUM_AGE_IN_WEEKS) {
this.ageInWeeks = 0;
} else
this.ageInWeeks = newAgeInWeeks;
if (newGenerationNumber < 0)
this.generationNumber = 0;
else
this.generationNumber = newGenerationNumber;
if (newGenus.isEmpty() || newGenus == null) {
this.genus = DEFAULT_GENUS;
} else {
this.genus = Character.toUpperCase(newGenus.charAt(0)) +
newGenus.substring(1).toLowerCase();
}
if (newSpecies.isEmpty() || newSpecies == null) {
this.species = DEFAULT_SPECIES;
} else {
this.species = newSpecies.toLowerCase();
}
this.isFemale = newIsFemale;
this.isAlive = true;
if (newHealthCoefficient < MINIMUM_HEALTH_COEFFICIENT ||
newHealthCoefficient > MAXIMUM_HEALTH_COEFFICIENT)
this.healthCoefficient = DEFAULT_HEALTH_COEFFICIENT;
else
this.healthCoefficient = newHealthCoefficient;
this.identificationNumber = ++numberOfGuppiesBorn;
}
public void incrementAge() {
ageInWeeks++;
if (ageInWeeks == MAXIMUM_AGE_IN_WEEKS) {
isAlive = false;
}
}
public double getVoulmeNeeded() {
if (ageInWeeks < 10) {
return MINIMUM_WATER_VOLUME_ML;
} else if (ageInWeeks <= 30) {
return MINIMUM_WATER_VOLUME_ML * (ageInWeeks / YOUNG_FISH);
} else if (ageInWeeks <= 50) {
return MINIMUM_WATER_VOLUME_ML * 1.5;
} else
return 0.0;
}
public void changeHealthCoefficient(double delta) {
healthCoefficient += delta;
if (healthCoefficient <= MINIMUM_HEALTH_COEFFICIENT) {
healthCoefficient = 0.0;
isAlive = false;
}
if (healthCoefficient > MAXIMUM_HEALTH_COEFFICIENT) {
healthCoefficient = MAXIMUM_HEALTH_COEFFICIENT;
}
}
public static int getNumberOfGuppiesBorn() {
return numberOfGuppiesBorn;
}
public String getGenus() {
return genus;
}
public String getSpecies() {
return species;
}
public int getAgeInWeeks() {
return ageInWeeks;
}
public boolean getIsFemale() {
return isFemale;
}
public int getGenerationNumber() {
return generationNumber;
}
public boolean getIsAlive() {
return isAlive;
}
public double getHealthCoefficient() {
return healthCoefficient;
}
public void setGenus(String genus) {
if (genus != null && !genus.isEmpty())
this.genus = Character.toUpperCase(genus.charAt(0)) +
genus.substring(1).toLowerCase();
;
}
public void setSpecies(String species) {
if (species != null && !species.isEmpty())
this.species = species.toLowerCase();
}
public void setAgeInWeeks(int ageInWeeks) {
if (ageInWeeks >= 0 && ageInWeeks <= MAXIMUM_AGE_IN_WEEKS)
this.ageInWeeks = ageInWeeks;
}
public void setIsFemale(boolean isFemale) {
this.isFemale = isFemale;
}
public void setGenerationNumber(int generationNumber) {
if (generationNumber >= 0)
this.generationNumber = generationNumber;
}
public void setIsAlive(boolean isAlive) {
this.isAlive = isAlive;
}
public void setHealthCoefficient(int healthCoefficient) {
if (healthCoefficient >= MINIMUM_HEALTH_COEFFICIENT &&
healthCoefficient <= MAXIMUM_HEALTH_COEFFICIENT)
this.healthCoefficient = healthCoefficient;
}
public String toString() {
return \"[genus=\" + genus + \",species=\" + species +
\",ageInWeeks=\" + ageInWeeks + \",isFemale=\" + isFemale +
\",generationNumber=\" + generationNumber + \",isAlive=\" + isAlive +
\",healthCoefficient=\" + healthCoefficient + \",identificationNumber=\" +
identificationNumber + \"]\";
}
}





