Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Modify the classes bellow to match the information here: You must implement the

ID: 3571526 • Letter: M

Question

Modify the classes bellow to match the information here:

You must implement the following:

1. The Guppy class requires the following modification:


(a) public ArrayList<Guppy> spawn()
It’s finally time to implement reproduction for the Guppies! How do
we determine if a Guppy has babies? Here are the rules:

i. Only female Guppies can have babies
ii. Female Guppies must be 10 weeks of age or older to spawn
iii. Each week, each female Guppy who is 10 weeks old or older has
a 25 percent chance of spawning (hint: use the Random)
iv. A spawning female has between 0 and 100 fry (Note: Guppies
don’t lay eggs, they have live babies called fry! Really!)
v. Each fry is the same genus and species as its mother
vi. Each fry has a 50 percent chance of being female
vii. A new fry has a health coefficient equal to (1.0 + mother’s health
quotient) / 2.0
viii. The generation number of a new fry is 1 + the mother’s generation
number.

Here’s a good way to approach this. The method should begin by
checking if the Guppy is female. If not, or if the Guppy is 0 - 9 weeks
old, return null. Otherwise create a local ArrayList<Guppy> called

babyGuppies. Use a Random object to generate a double between
0 and 1.0. If the random number is equal to or less than 0.25, the
female will spawn some babies. If the female is going to have babies,
use the Random object to generate a random int between 0 and 100
inclusive for the female. Use a loop statement to create this number
of Guppies (0 - 100) with the correct attributes, and add them to
the babyGuppies ArrayList. After the loop is done, return the newly
created ArrayList. That’s all.


2. The Pool class requires the following modifications:


(a) public int spawn()
Iterate over the Guppies in the Pool and invoke the spawn method
on each one. Each Guppy will return null (if it is male, or too
young to reproduce) or an ArrayList of Guppies containing the new
fry. Add the new baby Guppies to the Pool’s collection of Guppies.
You can add each mother’s new fry to the Pool like this:
guppiesInPool.addAll(newBabies);. Count the total number
of new Guppies that have been born and added to the Pool, and
return the total.

(b) public int incrementAges()
This method increments the ages of every Guppy in the Pool and
returns the number that have died of old age (reached 50 weeks). Do
not remove the dead Guppies from the collection, just kill them. The
dead Guppies still need to be removed from the system.


3. Implement a new class called Ecosystem. Ecosystem contains and drives
the simulation and requires the following elements:


(a) One private instance variable, private ArrayList<Pool> pools.
Do not add any other instance variables:
(b) Zero-parameter constructor that must initialize the ArrayList of
Pools. Do not create a one-parameter constructor.
(c) Create an accessor and mutator for the ArrayList of Pools. The
mutator should ignore a null parameter.
(d) Create a method called public void addPool(Pool newPool)
which adds the Pool passed as a parameter to the Collection of Pools.
Ignore null parameters, e.g., do not add any nulls to the Pool Collection.
(e) Create a method called public void reset() that resets the
Ecosystem by emptying the ArrayList of Pool (hint: check out the
clear() method available to the ArrayList).
(f) Create a method called public int getGuppyPopulation()
which returns the total number of living Guppies in the Ecosystem
(Hint: use a for each loop to invoke the getPopulation() method
on each pool and add the result to a local accumulator variable).
(g) A method called public void setupSimulation() should create
the following Ecosystem:

i. The first pool is called Skookumchuk. It has a total volume of
1,000 litres. Its temperature is 42 degrees Celsius and its pH is
7.9. The nutrient coefficient is 0.9.
ii. The second pool is called Rutherford. It has a total volume of
5,000 litres. Its temperature is 39 degrees Celsius and its pH is
7.7. The nutrient coefficient is 0.85.
iii. The third and final pool is called Gamelin. It has a total volume
of 4,300 litres. Its temperature is 37 degrees Celsius and its pH
is 7.5. The nutrient coefficient is 1.0.
iv. There are 100 Guppies in Skookumchuk Pool. They are all
Poecilia reticulata. Use your Random object and the multiparameter
Guppy constructor to create each new Guppy with an
age between 10 and 25 weeks inclusive and a health coefficient
between 0.5 and 0.8 inclusive. Each Guppy has a 50 percent
chance of being female.
v. There are 100 Guppies in Rutherford Pool. They are all Poecilia
reticulata. Use your Random object and the multi-parameter
Guppy constructor to create each new Guppy with an age between
10 and 15 weeks inclusive and a health coefficient between
0.8 and 1.0 inclusive. Each Guppy has a 50 percent chance of
being female.
vi. There are 30 Guppies in Gamelin Pool. They are all Poecilia
reticulata. Use your Random object and the non-default Guppy
constructor to create each new Guppy with an age between 15
and 49 weeks inclusive and a health coefficient between 0.0 and
1.0 inclusive. Each Guppy has a 50 percent chance of being
female.


(h) public void simulate(int numberOfWeeks)
Uses a loop to invoke the next method, simulateOneWeek, the specified
number of times.
(i) Create a method called public void simulateOneWeek() that
runs the simulation for one week. This method must:

i. Invoke incrementAges() on each Pool and add the return
value to a local int called diedOfOldAge
ii. Invoke removeDeadGuppies() on each Pool and add the return
value to a local int called numberRemoved
iii. Invoke applyNutrientCoefficient() on each Pool and add
the return value to a local int called starvedToDeath
iv. Invoke removeDeadGuppies() again (second time) and add
the return value to numberRemoved
v. Invoke spawn() and add the return value to a local int called
newFry
vi. Invoke adjustForCrowding() (see below) and assign
the return value to a local int called crowdedOut
vii. Invoke removeDeadGuppies() again (third time,
only if you implement adjustForCrowding() ) and add the
return value to numberRemoved
viii. Check that diedOfOldAge + starvedToDeath + crowdedOut ==
numberRemoved. If they are not equal, you have a logic error.
Otherwise, print:

A. Week number
B. Number of deaths this week due to old age
C. Number of deaths this week due to starvation
D. Number of deaths this week due to overcrowding
E. Number of births (new fry) this week
F. List of pools and their current populations at the end of the
week
G. Total population of the Ecosystem at the end of the week

(j) public int adjustForCrowding()
This Pool method extinguishes the Guppies that have suffocated due
to overcrowding. If the total volume needed by a collection of Guppies
exceeds the volume of their Pool, then some of the Guppies must
be extinguished. If the total volume required by the Guppies in the
Pool exceeds the total volume of the Pool, then we must loop through
the Guppies in the Pool and terminate the weakest, e.g., the Guppy
with the lowest health coefficient. We must do this again and again,
and keep terminating the weakest Guppy in the Pool until the total
volume requirement of the group of Guppies is equal to or less than
than the volume of the Pool. Note that the crowded out Guppies are
only terminated. They are not removed from the Pool. Return the
total number of Guppies that have died due to crowding.

Create a method in Ecosystem called public int adjustForCrowding()
(same name as the Pool method!)
which iterates
over all the Pools and invokes each Pool’s adjustForCrowding()
method and adds the number of Guppies that have been crowded
out to a running total. Once all the Pools have been “de-crowded,”
adjustForCrowding should return the total number of Guppies that
have been squeezed out of the entire ecosystem.

/**
* Guppy
*/
public class Guppy {
public static final int YOUNG_FISH = 10;
public static final int MATURE_FISH = 30;
public static final int MAXIMUM_AGE_IN_WEEKS = 50;
public static final double MINIMUM_WATER_VOLUME_ML = 250.0;
public static final String DEFAULT_GENUS = "Poecilia";
public static final String DEFAULT_SPECIES = "reticulata";
public static final double DEFAULT_HEALTH_COEFFICIENT = 0.5;
public static final double MINIMUM_HEALTH_COEFFICIENT = 0.0;
public 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 int identificationNumber;

private static int numberOfGuppiesBorn = 0;

/**
* Zero-parameter constructor for objects of class Guppy.
*/
public Guppy() {
genus = DEFAULT_GENUS;
species = DEFAULT_SPECIES;
ageInWeeks = 0;
isFemale = true;
generationNumber = 0;
isAlive = true;
healthCoefficient = DEFAULT_HEALTH_COEFFICIENT;
identificationNumber = ++numberOfGuppiesBorn;
}

/**
* Multi-parameter constructor for objects of class Guppy. Invalid arguments
* will be replaced with default values.
*
* @param newGenus
* cannot be null, empty, or a String of whitespace
* @param newSpecies
* cannot be null, empty, or a String of whitespace
* @param newAgeInWeeks
* must be between 0 and MAXIMUM_AGE_IN_WEEKS
* @param newIsFemale
* @param newGenerationNumber
* must be positive
* @param newHealthCoefficient
* must be between MINIMUM_HEALTH_COEFFICIENT or
* MAXIMUM_HEALTH_COEFFICIENT
*/
public Guppy(String newGenus,
String newSpecies,
int newAgeInWeeks,
boolean newIsFemale,
int newGenerationNumber,
double newHealthCoefficient) {
if (newGenus != null && newGenus.trim().length() > 0) {
genus = formatNameTitleCase(newGenus.trim());
} else {
genus = DEFAULT_GENUS;
}

if (newSpecies != null && newSpecies.trim().length() > 0) {
species = newSpecies.trim().toLowerCase();
} else {
species = DEFAULT_SPECIES;
}

if (newAgeInWeeks < 0 || newAgeInWeeks > MAXIMUM_AGE_IN_WEEKS) {
ageInWeeks = 0;
} else {
ageInWeeks = newAgeInWeeks;
}

isFemale = newIsFemale;

if (newGenerationNumber < 0) {
generationNumber = 0;
} else {
generationNumber = newGenerationNumber;
}

if (newHealthCoefficient < MINIMUM_HEALTH_COEFFICIENT || newHealthCoefficient > MAXIMUM_HEALTH_COEFFICIENT) {
healthCoefficient = DEFAULT_HEALTH_COEFFICIENT;
} else {
healthCoefficient = newHealthCoefficient;
}

isAlive = true;
identificationNumber = ++numberOfGuppiesBorn;
}

/**
* Increments the age of the Guppy by 1 week. If after incrementing the age
* getAge() >= MAXIMUM_AGE_IN_WEEKS then isAlive() = false.
*/
public void incrementAge() {
++ageInWeeks;
if (getAgeInWeeks() >= MAXIMUM_AGE_IN_WEEKS) {
setIsAlive(false);
}
}

/**
* Returns genus.
*
* @return genus as a String
*/
public String getGenus() {
return genus;
}

/**
* Returns species.
*
* @return species as a String
*/
public String getSpecies() {
return species;
}

/**
* Returns age in weeks.
*
* @return ageInWeeks as an int
*/
public int getAgeInWeeks() {
return ageInWeeks;
}

/**
* Returns true if the Guppy is female, else false.
*
* @return isFemale as a boolean
*/
public boolean getIsFemale() {
return isFemale;
}

/**
* Returns generation number.
*
* @return generationNumber as an int
*/
public int getGenerationNumber() {
return generationNumber;
}

/**
* Returns true if the Guppy is alive, else false.
*
* @return isAlive as a boolean
*/
public boolean getIsAlive() {
return isAlive;
}

/**
* Returns the health coefficient. A health coefficient of 0 means the Guppy
* is dead. A health coefficient of 1.0 means the Guppy is in perfect
* health.
*/
public double getHealthCoefficient() {
return healthCoefficient;
}

/**
* Returns the identification number.
*
* @return identificationNumber as an int
*/
public int getIdentificationNumber() {
return identificationNumber;
}

/**
* Returns the number of Guppy objects created.
*
* @return numberOfGuppiesBorn as an int
*/
public static int getNumberOfGuppiesBorn() {
return numberOfGuppiesBorn;
}

/**
* Sets the genus. Ignores null, empty, or a String of whitespace.
*
* @param newGenus
*/
public void setGenus(String newGenus) {
if (newGenus != null && newGenus.trim().length() > 0) {
genus = formatNameTitleCase(newGenus.trim());
}
}

/**
* Sets the species. Ignores null, empty, or a String of whitespace.
*
* @param newSpecies
*/
public void setSpecies(String newSpecies) {
if (newSpecies != null && newSpecies.trim().length() > 0) {
species = newSpecies.trim().toLowerCase();
}
}

/**
* Sets the age in weeks. Ignores values < 0 and > MAXIMUM_AGE_IN_WEEKS.
*
* @param newAgeInWeeks
*/
public void setAgeInWeeks(int newAgeInWeeks) {
if (newAgeInWeeks >= 0 && newAgeInWeeks <= MAXIMUM_AGE_IN_WEEKS) {
ageInWeeks = newAgeInWeeks;
}
}

/**
* Sets the Guppy's sex.
*
* @pararm newIsFemale
*/
public void setIsFemale(boolean newIsFemale) {
isFemale = newIsFemale;
}

/**
* Sets the generation number. Ignores negative values.
*
* @param newGenerationNumber
*/
public void setGenerationNumber(int newGenerationNumber) {
if (newGenerationNumber >= 0) {
generationNumber = newGenerationNumber;
}
}

/**
* Sets whether the Guppy is alive.
*
* @pararm newIsAlive
*/
public void setIsAlive(boolean newIsAlive) {
isAlive = newIsAlive;
}

/**
* Sets the health coefficient. Ignores values < MINIMUM_HEALTH_COEFFICIENT
* and ignores values > MAXIMUM_HEALTH_COEFFICIENT.
*
* @param newHealthCoefficient.
*/
public void setHealthCoefficient(double newHealthCoefficient) {
if (newHealthCoefficient >= MINIMUM_HEALTH_COEFFICIENT && newHealthCoefficient <= MAXIMUM_HEALTH_COEFFICIENT) {
healthCoefficient = newHealthCoefficient;
}
}

/**
* Returns the volume of water needed by this Guppy in millilitres.
*
* @return volume in mL
*/
public double getVolumeNeeded() {
if (getAgeInWeeks() < YOUNG_FISH) {
return MINIMUM_WATER_VOLUME_ML;
} else if (getAgeInWeeks() <= MATURE_FISH) {
return MINIMUM_WATER_VOLUME_ML * getAgeInWeeks() / YOUNG_FISH;
} else if (getAgeInWeeks() <= MAXIMUM_AGE_IN_WEEKS) {
return MINIMUM_WATER_VOLUME_ML * 1.5;
} else {
return 0;
}
}

/**
* Changes the health coefficient by the specified delta. If the new health
* coefficient < MINIMUM_HEALTH_COEFFICIENT, the fish dies. The health
* coefficient cannot exceed the bounds [0.0, 1.0]
*/
public void changeHealthCoefficient(double delta) {
double newHealthCoefficient = healthCoefficient + delta;
if (newHealthCoefficient <= MINIMUM_HEALTH_COEFFICIENT) {
setHealthCoefficient(MINIMUM_HEALTH_COEFFICIENT);
setIsAlive(false);
} else if (newHealthCoefficient > MAXIMUM_HEALTH_COEFFICIENT) {
setHealthCoefficient(MAXIMUM_HEALTH_COEFFICIENT);
} else {
setHealthCoefficient(newHealthCoefficient);
}
}

/**
* Returns a String representation of this Guppy.
*
* @return toString a String representation
*/
public String toString() {
return "[genus=" + genus + ",species=" + species + ",ageInWeeks=" + ageInWeeks + ",isFemale=" + isFemale
+ ",generationNumber=" + generationNumber + ",isAlive=" + isAlive + ",healthCoefficient"
+ healthCoefficient + ",identificationNumber" + identificationNumber + "]";
}

/**
* Formats a name and returns it with the first letter in upper case and the
* rest in lower case. If passed null, returns a null.
*
* @param name
* the name to format
* @return the correctly formatted name, as a String
*/
public static String formatNameTitleCase(String name) {
if (name != null && name.trim().length() > 0) {
String firstLetter = name.trim().toUpperCase().substring(0, 1);
String theRest = name.trim().toLowerCase().substring(1);
return firstLetter + theRest;
} else {
return null;
}
}
}

-------------------------------------------------------------------

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;

/**
* Pool. A simple data class.
*/
public class Pool {

public static final String DEFAULT_POOL_NAME = "Unnamed";
public static final double DEFAULT_POOL_TEMP_CELSIUS = 40.0;
public static final double MINIMUM_POOL_TEMP_CELSIUS = 0.0;
public static final double MAXIMUM_POOL_TEMP_CELSIUS = 100.0;
public static final double DEFAULT_PH = 7.0;
public static final double MINIMUM_PH = 0.0;
public static final double MAXIMUM_PH = 14.0;
public static final double DEFAULT_NUTRIENT_COEFFICIENT = 0.5;
public static final double MINIMUM_NUTRIENT_COEFFICIENT = 0.0;
public static final double MAXIMUM_NUTRIENT_COEFFICIENT = 1.0;

private String name;
private double volumeLitres;
private double temperatureCelsius;
private double pH;
private double nutrientCoefficient;
private ArrayList<Guppy> guppiesInPool;
private Random randomNumberGenerator;
private int identificationNumber;

private static int numberOfPools = 0;

/**
* Zero-parameter constructor for objects of class Pool initializes
* all fields to defaults. Note that the identification number is set automatically.
*/
public Pool() {
name = DEFAULT_POOL_NAME;
volumeLitres = 0.0;
temperatureCelsius = DEFAULT_POOL_TEMP_CELSIUS;
pH = DEFAULT_PH;
nutrientCoefficient = DEFAULT_NUTRIENT_COEFFICIENT;
guppiesInPool = new ArrayList<>();
randomNumberGenerator = new Random();
numberOfPools = numberOfPools + 1;
identificationNumber = numberOfPools;
}

/**
* Constructor for objects of class Pool. Note that the identification
* number is set automatically.
*
* @param name
* the Pool's name, a String. Pool names cannot be null and cannot
* be Strings containing only whitespace.
* @param volumeLitres
* a double representing the Pool's volume in litres
* @param temperatureCelsius
* a double representing the Pool's temperature. Must be in the
* range [MINIMUM_POOL_TEMP_CELSIUS, MAXIMUM_POOL_TEMP_CELSIUS] else set to
* DEFAULT_POOL_TEMP_CELSIUS.
* @param pH
* a double representing the Pool's pH. Must be in the range
* [MINIMUM_PH, MAXIMUM_PH] else set to DEFAULT_PH.
* @param nutrientCoefficient
* a double representing the Pool's nutrientCoefficient. Must be
* in the range [MINIMUM_NUTRIENT_COEFFICIENT,
* MAXIMUM_NUTRIENT_COEFFICIENT] else set to DEFAULT_NUTRIENT_COEFFICIENT.
*/
public Pool(String name,
double volumeLitres,
double temperatureCelsius,
double pH,
double nutrientCoefficient) {
setName(name);
setVolume(volumeLitres);
setTemperature(temperatureCelsius);

if (temperatureCelsius < MINIMUM_POOL_TEMP_CELSIUS || temperatureCelsius > MAXIMUM_POOL_TEMP_CELSIUS) {
setTemperature(DEFAULT_POOL_TEMP_CELSIUS);
} else {
setTemperature(temperatureCelsius);
}

if (pH < MINIMUM_PH || pH > MAXIMUM_PH) {
setPH(DEFAULT_PH);
} else {
setPH(pH);
}

if (nutrientCoefficient < MINIMUM_NUTRIENT_COEFFICIENT || nutrientCoefficient > MAXIMUM_NUTRIENT_COEFFICIENT) {
setNutrientCoefficient(DEFAULT_NUTRIENT_COEFFICIENT);
} else {
setNutrientCoefficient(nutrientCoefficient);
}

identificationNumber = ++numberOfPools;

guppiesInPool = new ArrayList<>();
randomNumberGenerator = new Random();
}

/**
* Returns the name.
*
* @return name as a String
*/
public String getName() {
return name;
}

/**
* Sets the name in title case. Ignores null and empty String parameters.
*
* @param name a String
*/
public final void setName(String name) {
if (name != null && name.trim().length() > 0) {
this.name = formatNameTitleCase(name);
}
if (this.name == null) {
this.name = DEFAULT_POOL_NAME;
}
}

/**
* Returns the volume in litres of this Pool.
*
* @return volumeLitres a double
*/
public double getVolume() {
return volumeLitres;
}

/**
* Sets the volume of this Pool in litres. Ignores negative parameters.
*
* @param volumeLitres a double
*/
public final void setVolume(double volumeLitres) {
if (volumeLitres >= 0) {
this.volumeLitres = volumeLitres;
}
}

/**
* Returns the temperature of this Pool in degrees Celsius.
*
* @return temperatureCelsius a double
*/
public double getTemperature() {
return temperatureCelsius;
}

/**
* Sets the temperature of this Pool in degrees Celsius. Ignores parameters
* outside of range [MINIMUM_POOL_TEMP_CELSIUS, MAXIMUM_POOL_TEMP_CELSIUS].
*
* @param temperatureCelsius a double
*/
public final void setTemperature(double temperatureCelsius) {
if (temperatureCelsius >= MINIMUM_POOL_TEMP_CELSIUS && temperatureCelsius <= MAXIMUM_POOL_TEMP_CELSIUS) {
this.temperatureCelsius = temperatureCelsius;
}
}

/**
* Returns the pH of this Pool.
*
* @return pH a double
*/
public double getPH() {
return pH;
}

/**
* Sets the pH of this Pool. Ignores parameters outside of range
* [MINIMUM_PH, MAXIMUM_PH].
*
* @param pH a double
*/
public final void setPH(double pH) {
if (pH >= MINIMUM_PH && pH <= MAXIMUM_PH) {
this.pH = pH;
}
}

/**
* Returns the nutrient coefficient.
*
* @return nutrientCoefficient a double
*/
public double getNutrientCoefficient() {
return nutrientCoefficient;
}

/**
* Sets the nutrient coefficient. Ignores parameters outside of range
* [MINIMUM_NUTRIENT_COEFFICIENT, MAXIMUM_NUTRIENT_COEFFICIENT].
*
* @param nutrient coefficient a double
*/
public final void setNutrientCoefficient(double nutrientCoefficient) {
if (nutrientCoefficient >= MINIMUM_NUTRIENT_COEFFICIENT
&& nutrientCoefficient <= MAXIMUM_NUTRIENT_COEFFICIENT) {
this.nutrientCoefficient = nutrientCoefficient;
}
}

/**
* Returns this Pool's identification number.
*
* @return identificationNumber as an int
*/
public int getIdentificationNumber() {
return identificationNumber;
}

/**
* Returns a reference to the collection of Guppies.
*
* @return guppyPopulation an ArrayList<Guppy>
*/
public ArrayList<Guppy> getGuppies() {
return guppiesInPool;
}

/**
* Sets the collection of Guppies.
*
* @param guppyPopulation an ArrayList<Guppy>
*/
public void setGuppies(ArrayList<Guppy> guppyPopulation) {
if (guppyPopulation != null) {
this.guppiesInPool = guppyPopulation;
}
}

/**
* Changes the nutrient coefficient by the specified delta. The nutrient
* coefficient will remain in the range [MINIMUM_NUTRIENT_COEFFICIENT, MAXIMUM_NUTRIENT_COEFFICIENT].
*
* @param delta the amount to change the nutrient coeffficient a double
*/
public void changeNutrientCoefficient(double delta) {
double newNutrientCoefficient = getNutrientCoefficient() + delta;

if (newNutrientCoefficient < MINIMUM_NUTRIENT_COEFFICIENT) {
setNutrientCoefficient(MINIMUM_NUTRIENT_COEFFICIENT);
} else if (newNutrientCoefficient > MAXIMUM_NUTRIENT_COEFFICIENT) {
setNutrientCoefficient(MAXIMUM_NUTRIENT_COEFFICIENT);
} else {
setNutrientCoefficient(newNutrientCoefficient);
}
}

/**
* Changes the temperature by the specified delta. The temperature will
* remain in the range [MINIMUM_POOL_TEMP_CELSIUS, MAXIMUM_POOL_TEMP_CELSIUS].
*
* @param delta the amount to change the temperature a double
*/
public void changeTemperature(double delta) {
double newTemperature = getTemperature() + delta;

if (newTemperature < MINIMUM_POOL_TEMP_CELSIUS) {
setTemperature(MINIMUM_POOL_TEMP_CELSIUS);
} else if (newTemperature > MAXIMUM_POOL_TEMP_CELSIUS) {
setTemperature(MAXIMUM_POOL_TEMP_CELSIUS);
} else {
setTemperature(newTemperature);
}
}

/**
* Returns total number of Pools created.
*
* @return numberOfPools the number of Pool objects instantiated
*/
public static int getNumberCreated() {
return numberOfPools;
}

/**
* Adds the specified Guppy to the pool.
*
* @param guppy the Guppy to add
*/
public void addGuppy(Guppy guppy) {
if (guppy != null) {
guppiesInPool.add(guppy);
}
}

/**
* Returns the Pool's Guppy population.
*
* @return population an int
*/
public int getPopulation() {
return guppiesInPool.size();
}

/**
* Calculates which Guppies have died of malnutrition and returns
* the number of Guppies that have died. The dead Guppies are NOT
* removed from the Pool. They are only killed. They still need
* to be removed from the Pool.
*
* @return numberStarved the number of Guppies that have died of starvation
*/
public int applyNutrientCoefficient() {
int numberStarved = 0;
Iterator<Guppy> iterator = guppiesInPool.iterator();
while (iterator.hasNext()) {
Guppy guppy = iterator.next();
if (guppy.getIsAlive() == true && randomNumberGenerator.nextDouble() > getNutrientCoefficient()) {
guppy.setIsAlive(false);
numberStarved++;
}
}
return numberStarved;
}

/**
* Removes (culls) the dead Guppies from this Pool and returns
* the number that have been removed from this Pool.
*
* @return numberRemoved the number of dead Guppies removed.
*/
public int removeDeadGuppies() {
int numberRemoved = 0;
Iterator<Guppy> iterator = guppiesInPool.iterator();
while (iterator.hasNext()) {
if (iterator.next().getIsAlive() == false) {
iterator.remove();
numberRemoved++;
}
}
return numberRemoved;
}

/**
* Returns the total volume requirement in litres of the living Guppies
* in this Pool. Guppies know how many *millilitres* they require. This
* returns total Litres where 1,000.0 mL = 1.0 L.
*
* @return totalVolumeLitres a double
*/
public double getGuppyVolumeRequirement() {
double totalVolumeInMillilitres = 0.0;
Iterator<Guppy> iterator = guppiesInPool.iterator();
while (iterator.hasNext()) {
Guppy aPoolGuppy = iterator.next();
if (aPoolGuppy.getIsAlive() == true) {
totalVolumeInMillilitres += aPoolGuppy.getVolumeNeeded();
}
}
return totalVolumeInMillilitres / 1000;
}

/**
* Returns the average age in weeks of the living Guppies in this Pool.
*
* @return averageAgeInWeeks a double
*/
public double getAverageAge() {
double totalAge = 0.0; // Use a double here to force the compiler to return a double
int numberOfLivingGuppies = 0;
Iterator<Guppy> iterator = guppiesInPool.iterator();
while (iterator.hasNext()) {
Guppy aPoolGuppy = iterator.next();
if (aPoolGuppy.getIsAlive() == true) {
numberOfLivingGuppies++;
totalAge += aPoolGuppy.getAgeInWeeks();
}
}
return totalAge / numberOfLivingGuppies;
}

/**
* Returns the average health coefficient of the living Guppies in this Pool.
* A health coefficient is always contained within the range [MINIMUM_NUTRIENT_COEFFICIENT, MAXIMUM_NUTRIENT_COEFFICIENT].
*
* @return averageHealthCoefficient as a double
*/
public double getAverageHealthCoefficient() {
double totalHealthCoefficient = 0.0;
int numberOfLivingGuppies = 0;
Iterator<Guppy> iterator = guppiesInPool.iterator();
while (iterator.hasNext()) {
Guppy aPoolGuppy = iterator.next();
if (aPoolGuppy.getIsAlive() == true) {
numberOfLivingGuppies++;
totalHealthCoefficient += aPoolGuppy.getHealthCoefficient();
}
}
return totalHealthCoefficient / numberOfLivingGuppies;
}

/**
* Returns the percentage of Guppies in this Pool that are female.
*
* @return percentageFemale as a double
*/
public double getFemalePercentage() {
double females = 0.0; // Use a double here to force the compiler to return a double
int numberOfLivingGuppies = 0;
Iterator<Guppy> iterator = guppiesInPool.iterator();
while (iterator.hasNext()) {
Guppy aPoolGuppy = iterator.next();
if (aPoolGuppy.getIsAlive() == true) {
numberOfLivingGuppies++;
if (aPoolGuppy.getIsFemale() == true) {
females += 1.0;
}
}
}
return females / numberOfLivingGuppies;
}

/**
* Returns the median age in weeks of the living Guppies in this Pool.
*
* @return medianAge as a double
*/
public double getMedianAge() {
// If you didn't complete me in A2, try completing me in A3!
return 0.0;
}

/**
* Returns a description of this Pool.
*
* @return description of this Pool as a String.
*/
public String toString() {
return "[name=" + getName() + ",id=" + getIdentificationNumber() + ",volume (litres)=" + getVolume() +
",Temperature (Celsius): " + getTemperature() + ",pH: " + getPH() + ",Nutrient Coefficient: " +
getNutrientCoefficient() + ",Guppy population: " + guppiesInPool.size() + "]";
}

/**
* Prints this Pool's details.
*/
public void printDetails() {
System.out.println(this.toString());
}

/**
* Formats a name and returns it with the first letter in upper case and the
* rest in lower case. If passed null, returns a null.
*
* @param name the name to format
* @return the correctly formatted name, as a String
*/
public static String formatNameTitleCase(String name) {
if (name != null && name.trim().length() > 0) {
String firstLetter = name.trim().toUpperCase().substring(0, 1);
String theRest = name.trim().toLowerCase().substring(1);
return firstLetter + theRest;
} else {
return null;
}
}
}

Explanation / Answer

/* Its big piece of program only 1 all subparts and 2 all sub parts of questions completed */

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;

public class Guppy {
public static final int YOUNG_FISH = 10;
public static final int MATURE_FISH = 30;
public static final int MAXIMUM_AGE_IN_WEEKS = 50;
public static final double MINIMUM_WATER_VOLUME_ML = 250.0;
public static final String DEFAULT_GENUS = "Poecilia";
public static final String DEFAULT_SPECIES = "reticulata";
public static final double DEFAULT_HEALTH_COEFFICIENT = 0.5;
public static final double MINIMUM_HEALTH_COEFFICIENT = 0.0;
public static final double MAXIMUM_HEALTH_COEFFICIENT = 1.0;
public static final int MAXIMUM_BABIES = 100;
public static final double MAXIMUM_CHANCE_OF_SPAWN = 0.25;
public static final int MINIMUM_AGE_OF_SPAWN = 10;

private String genus;
private String species;
private int ageInWeeks;
private boolean isFemale;
private int generationNumber;
private boolean isAlive;
private double healthCoefficient;
private int identificationNumber;
private static int numberOfGuppiesBorn = 0;
/**
* Zero-parameter constructor for objects of class Guppy.
*/
public Guppy() {
genus = DEFAULT_GENUS;
species = DEFAULT_SPECIES;
ageInWeeks = 0;
isFemale = true;
generationNumber = 0;
isAlive = true;
healthCoefficient = DEFAULT_HEALTH_COEFFICIENT;
identificationNumber = ++numberOfGuppiesBorn;
}
/**
* Multi-parameter constructor for objects of class Guppy. Invalid arguments
* will be replaced with default values.
*
* @param newGenus
* cannot be null, empty, or a String of whitespace
* @param newSpecies
* cannot be null, empty, or a String of whitespace
* @param newAgeInWeeks
* must be between 0 and MAXIMUM_AGE_IN_WEEKS
* @param newIsFemale
* @param newGenerationNumber
* must be positive
* @param newHealthCoefficient
* must be between MINIMUM_HEALTH_COEFFICIENT or
* MAXIMUM_HEALTH_COEFFICIENT
*/
public Guppy(String newGenus,
String newSpecies,
int newAgeInWeeks,
boolean newIsFemale,
int newGenerationNumber,
double newHealthCoefficient) {
if (newGenus != null && newGenus.trim().length() > 0) {
genus = formatNameTitleCase(newGenus.trim());
} else {
genus = DEFAULT_GENUS;
}
if (newSpecies != null && newSpecies.trim().length() > 0) {
species = newSpecies.trim().toLowerCase();
} else {
species = DEFAULT_SPECIES;
}
if (newAgeInWeeks < 0 || newAgeInWeeks > MAXIMUM_AGE_IN_WEEKS) {
ageInWeeks = 0;
} else {
ageInWeeks = newAgeInWeeks;
}
isFemale = newIsFemale;
if (newGenerationNumber < 0) {
generationNumber = 0;
} else {
generationNumber = newGenerationNumber;
}
if (newHealthCoefficient < MINIMUM_HEALTH_COEFFICIENT || newHealthCoefficient > MAXIMUM_HEALTH_COEFFICIENT) {
healthCoefficient = DEFAULT_HEALTH_COEFFICIENT;
} else {
healthCoefficient = newHealthCoefficient;
}
isAlive = true;
identificationNumber = ++numberOfGuppiesBorn;
}
/**
* Increments the age of the Guppy by 1 week. If after incrementing the age
* getAge() >= MAXIMUM_AGE_IN_WEEKS then isAlive() = false.
*/
public void incrementAge() {
++ageInWeeks;
if (getAgeInWeeks() >= MAXIMUM_AGE_IN_WEEKS) {
setIsAlive(false);
}
}
/**
* Returns genus.
*
* @return genus as a String
*/
public String getGenus() {
return genus;
}
/**
* Returns species.
*
* @return species as a String
*/
public String getSpecies() {
return species;
}
/**
* Returns age in weeks.
*
* @return ageInWeeks as an int
*/
public int getAgeInWeeks() {
return ageInWeeks;
}
/**
* Returns true if the Guppy is female, else false.
*
* @return isFemale as a boolean
*/
public boolean getIsFemale() {
return isFemale;
}
/**
* Returns generation number.
*
* @return generationNumber as an int
*/
public int getGenerationNumber() {
return generationNumber;
}
/**
* Returns true if the Guppy is alive, else false.
*
* @return isAlive as a boolean
*/
public boolean getIsAlive() {
return isAlive;
}
/**
* Returns the health coefficient. A health coefficient of 0 means the Guppy
* is dead. A health coefficient of 1.0 means the Guppy is in perfect
* health.
*/
public double getHealthCoefficient() {
return healthCoefficient;
}
/**
* Returns the identification number.
*
* @return identificationNumber as an int
*/
public int getIdentificationNumber() {
return identificationNumber;
}
/**
* Returns the number of Guppy objects created.
*
* @return numberOfGuppiesBorn as an int
*/
public static int getNumberOfGuppiesBorn() {
return numberOfGuppiesBorn;
}
/**
* Sets the genus. Ignores null, empty, or a String of whitespace.
*
* @param newGenus
*/
public void setGenus(String newGenus) {
if (newGenus != null && newGenus.trim().length() > 0) {
genus = formatNameTitleCase(newGenus.trim());
}
}
/**
* Sets the species. Ignores null, empty, or a String of whitespace.
*
* @param newSpecies
*/
public void setSpecies(String newSpecies) {
if (newSpecies != null && newSpecies.trim().length() > 0) {
species = newSpecies.trim().toLowerCase();
}
}
/**
* Sets the age in weeks. Ignores values < 0 and > MAXIMUM_AGE_IN_WEEKS.
*
* @param newAgeInWeeks
*/
public void setAgeInWeeks(int newAgeInWeeks) {
if (newAgeInWeeks >= 0 && newAgeInWeeks <= MAXIMUM_AGE_IN_WEEKS) {
ageInWeeks = newAgeInWeeks;
}
}
/**
* Sets the Guppy's sex.
*
* @pararm newIsFemale
*/
public void setIsFemale(boolean newIsFemale) {
isFemale = newIsFemale;
}
/**
* Sets the generation number. Ignores negative values.
*
* @param newGenerationNumber
*/
public void setGenerationNumber(int newGenerationNumber) {
if (newGenerationNumber >= 0) {
generationNumber = newGenerationNumber;
}
}
/**
* Sets whether the Guppy is alive.
*
* @pararm newIsAlive
*/
public void setIsAlive(boolean newIsAlive) {
isAlive = newIsAlive;
}
/**
* Sets the health coefficient. Ignores values < MINIMUM_HEALTH_COEFFICIENT
* and ignores values > MAXIMUM_HEALTH_COEFFICIENT.
*
* @param newHealthCoefficient.
*/
public void setHealthCoefficient(double newHealthCoefficient) {
if (newHealthCoefficient >= MINIMUM_HEALTH_COEFFICIENT && newHealthCoefficient <= MAXIMUM_HEALTH_COEFFICIENT) {
healthCoefficient = newHealthCoefficient;
}
}
/**
* Returns the volume of water needed by this Guppy in millilitres.
*
* @return volume in mL
*/
public double getVolumeNeeded() {
if (getAgeInWeeks() < YOUNG_FISH) {
return MINIMUM_WATER_VOLUME_ML;
} else if (getAgeInWeeks() <= MATURE_FISH) {
return MINIMUM_WATER_VOLUME_ML * getAgeInWeeks() / YOUNG_FISH;
} else if (getAgeInWeeks() <= MAXIMUM_AGE_IN_WEEKS) {
return MINIMUM_WATER_VOLUME_ML * 1.5;
} else {
return 0;
}
}
/**
* Changes the health coefficient by the specified delta. If the new health
* coefficient < MINIMUM_HEALTH_COEFFICIENT, the fish dies. The health
* coefficient cannot exceed the bounds [0.0, 1.0]
*/
public void changeHealthCoefficient(double delta) {
double newHealthCoefficient = healthCoefficient + delta;
if (newHealthCoefficient <= MINIMUM_HEALTH_COEFFICIENT) {
setHealthCoefficient(MINIMUM_HEALTH_COEFFICIENT);
setIsAlive(false);
} else if (newHealthCoefficient > MAXIMUM_HEALTH_COEFFICIENT) {
setHealthCoefficient(MAXIMUM_HEALTH_COEFFICIENT);
} else {
setHealthCoefficient(newHealthCoefficient);
}
}
/**
* Returns a String representation of this Guppy.
*
* @return toString a String representation
*/
public String toString() {
return "[genus=" + genus + ",species=" + species + ",ageInWeeks=" + ageInWeeks + ",isFemale=" + isFemale + ",generationNumber=" + generationNumber + ",isAlive=" + isAlive + ",healthCoefficient" + healthCoefficient + ",identificationNumber" + identificationNumber + "]";
}
/**
* Formats a name and returns it with the first letter in upper case and the
* rest in lower case. If passed null, returns a null.
*
* @param name
* the name to format
* @return the correctly formatted name, as a String
*/
public static String formatNameTitleCase(String name) {
if (name != null && name.trim().length() > 0) {
String firstLetter = name.trim().toUpperCase().substring(0, 1);
String theRest = name.trim().toLowerCase().substring(1);
return firstLetter + theRest;
} else {
return null;
}
}
public ArrayList<Guppy> spawn(){
   if (getIsFemale() && getAgeInWeeks()>=MINIMUM_AGE_OF_SPAWN) { //Check is female and spawn age is >=10
       Random r=new Random();
       if(r.nextDouble()>MAXIMUM_CHANCE_OF_SPAWN) return null; // don't spawn chance greater than 0.25
       int cntBabies=r.nextInt(MAXIMUM_BABIES+1);
       if (cntBabies>0) {
           ArrayList<Guppy> babyGuppies=new ArrayList<Guppy>();
           for(int i=0;i<cntBabies;i++){
               babyGuppies.add(new Guppy( getGenus()                       //Mother's genus
                                       ,getSpecies()                   //Mother's species
                                       ,0                               //0 age
                                       ,r.nextBoolean()                   //50% chance of male or female so 0 or 1
                                       ,getGenerationNumber()+1           //Generationnumber of Mother + 1
                                       ,(getHealthCoefficient()+1)/2   //(Health co-eff of Mother + 1)/2
                                       )
                           );
           }
           return babyGuppies;
       }
   }
   return null;
}

}

class Pool {
public static final String DEFAULT_POOL_NAME = "Unnamed";
public static final double DEFAULT_POOL_TEMP_CELSIUS = 40.0;
public static final double MINIMUM_POOL_TEMP_CELSIUS = 0.0;
public static final double MAXIMUM_POOL_TEMP_CELSIUS = 100.0;
public static final double DEFAULT_PH = 7.0;
public static final double MINIMUM_PH = 0.0;
public static final double MAXIMUM_PH = 14.0;
public static final double DEFAULT_NUTRIENT_COEFFICIENT = 0.5;
public static final double MINIMUM_NUTRIENT_COEFFICIENT = 0.0;
public static final double MAXIMUM_NUTRIENT_COEFFICIENT = 1.0;
private String name;
private double volumeLitres;
private double temperatureCelsius;
private double pH;
private double nutrientCoefficient;
private ArrayList <Guppy> guppiesInPool;
private Random randomNumberGenerator;
private int identificationNumber;
private static int numberOfPools = 0;
/**
* Zero-parameter constructor for objects of class Pool initializes
* all fields to defaults. Note that the identification number is set automatically.
*/
public Pool() {
name = DEFAULT_POOL_NAME;
volumeLitres = 0.0;
temperatureCelsius = DEFAULT_POOL_TEMP_CELSIUS;
pH = DEFAULT_PH;
nutrientCoefficient = DEFAULT_NUTRIENT_COEFFICIENT;
guppiesInPool = new ArrayList < > ();
randomNumberGenerator = new Random();
numberOfPools = numberOfPools + 1;
identificationNumber = numberOfPools;
}
/**
* Constructor for objects of class Pool. Note that the identification
* number is set automatically.
*
* @param name
* the Pool's name, a String. Pool names cannot be null and cannot
* be Strings containing only whitespace.
* @param volumeLitres
* a double representing the Pool's volume in litres
* @param temperatureCelsius
* a double representing the Pool's temperature. Must be in the
* range [MINIMUM_POOL_TEMP_CELSIUS, MAXIMUM_POOL_TEMP_CELSIUS] else set to
* DEFAULT_POOL_TEMP_CELSIUS.
* @param pH
* a double representing the Pool's pH. Must be in the range
* [MINIMUM_PH, MAXIMUM_PH] else set to DEFAULT_PH.
* @param nutrientCoefficient
* a double representing the Pool's nutrientCoefficient. Must be
* in the range [MINIMUM_NUTRIENT_COEFFICIENT,
* MAXIMUM_NUTRIENT_COEFFICIENT] else set to DEFAULT_NUTRIENT_COEFFICIENT.
*/
public Pool(String name,
double volumeLitres,
double temperatureCelsius,
double pH,
double nutrientCoefficient) {
setName(name);
setVolume(volumeLitres);
setTemperature(temperatureCelsius);
if (temperatureCelsius < MINIMUM_POOL_TEMP_CELSIUS || temperatureCelsius > MAXIMUM_POOL_TEMP_CELSIUS) {
setTemperature(DEFAULT_POOL_TEMP_CELSIUS);
} else {
setTemperature(temperatureCelsius);
}
if (pH < MINIMUM_PH || pH > MAXIMUM_PH) {
setPH(DEFAULT_PH);
} else {
setPH(pH);
}
if (nutrientCoefficient < MINIMUM_NUTRIENT_COEFFICIENT || nutrientCoefficient > MAXIMUM_NUTRIENT_COEFFICIENT) {
setNutrientCoefficient(DEFAULT_NUTRIENT_COEFFICIENT);
} else {
setNutrientCoefficient(nutrientCoefficient);
}
identificationNumber = ++numberOfPools;
guppiesInPool = new ArrayList < > ();
randomNumberGenerator = new Random();
}
/**
* Returns the name.
*
* @return name as a String
*/
public String getName() {
return name;
}
/**
* Sets the name in title case. Ignores null and empty String parameters.
*
* @param name a String
*/
public final void setName(String name) {
if (name != null && name.trim().length() > 0) {
this.name = formatNameTitleCase(name);
}
if (this.name == null) {
this.name = DEFAULT_POOL_NAME;
}
}
/**
* Returns the volume in litres of this Pool.
*
* @return volumeLitres a double
*/
public double getVolume() {
return volumeLitres;
}
/**
* Sets the volume of this Pool in litres. Ignores negative parameters.
*
* @param volumeLitres a double
*/
public final void setVolume(double volumeLitres) {
if (volumeLitres >= 0) {
this.volumeLitres = volumeLitres;
}
}
/**
* Returns the temperature of this Pool in degrees Celsius.
*
* @return temperatureCelsius a double
*/
public double getTemperature() {
return temperatureCelsius;
}
/**
* Sets the temperature of this Pool in degrees Celsius. Ignores parameters
* outside of range [MINIMUM_POOL_TEMP_CELSIUS, MAXIMUM_POOL_TEMP_CELSIUS].
*
* @param temperatureCelsius a double
*/
public final void setTemperature(double temperatureCelsius) {
if (temperatureCelsius >= MINIMUM_POOL_TEMP_CELSIUS && temperatureCelsius <= MAXIMUM_POOL_TEMP_CELSIUS) {
this.temperatureCelsius = temperatureCelsius;
}
}
/**
* Returns the pH of this Pool.
*
* @return pH a double
*/
public double getPH() {
return pH;
}
/**
* Sets the pH of this Pool. Ignores parameters outside of range
* [MINIMUM_PH, MAXIMUM_PH].
*
* @param pH a double
*/
public final void setPH(double pH) {
if (pH >= MINIMUM_PH && pH <= MAXIMUM_PH) {
this.pH = pH;
}
}
/**
* Returns the nutrient coefficient.
*
* @return nutrientCoefficient a double
*/
public double getNutrientCoefficient() {
return nutrientCoefficient;
}
/**
* Sets the nutrient coefficient. Ignores parameters outside of range
* [MINIMUM_NUTRIENT_COEFFICIENT, MAXIMUM_NUTRIENT_COEFFICIENT].
*
* @param nutrient coefficient a double
*/
public final void setNutrientCoefficient(double nutrientCoefficient) {
if (nutrientCoefficient >= MINIMUM_NUTRIENT_COEFFICIENT && nutrientCoefficient <= MAXIMUM_NUTRIENT_COEFFICIENT) {
this.nutrientCoefficient = nutrientCoefficient;
}
}
/**
* Returns this Pool's identification number.
*
* @return identificationNumber as an int
*/
public int getIdentificationNumber() {
return identificationNumber;
}
/**
* Returns a reference to the collection of Guppies.
*
* @return guppyPopulation an ArrayList<Guppy>
*/
public ArrayList <Guppy> getGuppies() {
return guppiesInPool;
}
/**
* Sets the collection of Guppies.
*
* @param guppyPopulation an ArrayList<Guppy>
*/
public void setGuppies(ArrayList <Guppy> guppyPopulation) {
if (guppyPopulation != null) {
this.guppiesInPool = guppyPopulation;
}
}
/**
* Changes the nutrient coefficient by the specified delta. The nutrient
* coefficient will remain in the range [MINIMUM_NUTRIENT_COEFFICIENT, MAXIMUM_NUTRIENT_COEFFICIENT].
*
* @param delta the amount to change the nutrient coeffficient a double
*/
public void changeNutrientCoefficient(double delta) {
double newNutrientCoefficient = getNutrientCoefficient() + delta;
if (newNutrientCoefficient < MINIMUM_NUTRIENT_COEFFICIENT) {
setNutrientCoefficient(MINIMUM_NUTRIENT_COEFFICIENT);
} else if (newNutrientCoefficient > MAXIMUM_NUTRIENT_COEFFICIENT) {
setNutrientCoefficient(MAXIMUM_NUTRIENT_COEFFICIENT);
} else {
setNutrientCoefficient(newNutrientCoefficient);
}
}
/**
* Changes the temperature by the specified delta. The temperature will
* remain in the range [MINIMUM_POOL_TEMP_CELSIUS, MAXIMUM_POOL_TEMP_CELSIUS].
*
* @param delta the amount to change the temperature a double
*/
public void changeTemperature(double delta) {
double newTemperature = getTemperature() + delta;
if (newTemperature < MINIMUM_POOL_TEMP_CELSIUS) {
setTemperature(MINIMUM_POOL_TEMP_CELSIUS);
} else if (newTemperature > MAXIMUM_POOL_TEMP_CELSIUS) {
setTemperature(MAXIMUM_POOL_TEMP_CELSIUS);
} else {
setTemperature(newTemperature);
}
}
/**
* Returns total number of Pools created.
*
* @return numberOfPools the number of Pool objects instantiated
*/
public static int getNumberCreated() {
return numberOfPools;
}
/**
* Adds the specified Guppy to the pool.
*
* @param guppy the Guppy to add
*/
public void addGuppy(Guppy guppy) {
if (guppy != null) {
guppiesInPool.add(guppy);
}
}
/**
* Returns the Pool's Guppy population.
*
* @return population an int
*/
public int getPopulation() {
return guppiesInPool.size();
}
/**
* Calculates which Guppies have died of malnutrition and returns
* the number of Guppies that have died. The dead Guppies are NOT
* removed from the Pool. They are only killed. They still need
* to be removed from the Pool.
*
* @return numberStarved the number of Guppies that have died of starvation
*/
public int applyNutrientCoefficient() {
int numberStarved = 0;
Iterator <Guppy> iterator = guppiesInPool.iterator();
while (iterator.hasNext()) {
Guppy guppy = iterator.next();
if (guppy.getIsAlive() == true && randomNumberGenerator.nextDouble() > getNutrientCoefficient()) {
guppy.setIsAlive(false);
numberStarved++;
}
}
return numberStarved;
}
/**
* Removes (culls) the dead Guppies from this Pool and returns
* the number that have been removed from this Pool.
*
* @return numberRemoved the number of dead Guppies removed.
*/
public int removeDeadGuppies() {
int numberRemoved = 0;
Iterator <Guppy> iterator = guppiesInPool.iterator();
while (iterator.hasNext()) {
if (iterator.next().getIsAlive() == false) {
iterator.remove();
numberRemoved++;
}
}
return numberRemoved;
}
/**
* Returns the total volume requirement in litres of the living Guppies
* in this Pool. Guppies know how many *millilitres* they require. This
* returns total Litres where 1,000.0 mL = 1.0 L.
*
* @return totalVolumeLitres a double
*/
public double getGuppyVolumeRequirement() {
double totalVolumeInMillilitres = 0.0;
Iterator <Guppy> iterator = guppiesInPool.iterator();
while (iterator.hasNext()) {
Guppy aPoolGuppy = iterator.next();
if (aPoolGuppy.getIsAlive() == true) {
totalVolumeInMillilitres += aPoolGuppy.getVolumeNeeded();
}
}
return totalVolumeInMillilitres / 1000;
}
/**
* Returns the average age in weeks of the living Guppies in this Pool.
*
* @return averageAgeInWeeks a double
*/
public double getAverageAge() {
double totalAge = 0.0; // Use a double here to force the compiler to return a double
int numberOfLivingGuppies = 0;
Iterator <Guppy> iterator = guppiesInPool.iterator();
while (iterator.hasNext()) {
Guppy aPoolGuppy = iterator.next();
if (aPoolGuppy.getIsAlive() == true) {
numberOfLivingGuppies++;
totalAge += aPoolGuppy.getAgeInWeeks();
}
}
return totalAge / numberOfLivingGuppies;
}
/**
* Returns the average health coefficient of the living Guppies in this Pool.
* A health coefficient is always contained within the range [MINIMUM_NUTRIENT_COEFFICIENT, MAXIMUM_NUTRIENT_COEFFICIENT].
*
* @return averageHealthCoefficient as a double
*/
public double getAverageHealthCoefficient() {
double totalHealthCoefficient = 0.0;
int numberOfLivingGuppies = 0;
Iterator <Guppy> iterator = guppiesInPool.iterator();
while (iterator.hasNext()) {
Guppy aPoolGuppy = iterator.next();
if (aPoolGuppy.getIsAlive() == true) {
numberOfLivingGuppies++;
totalHealthCoefficient += aPoolGuppy.getHealthCoefficient();
}
}
return totalHealthCoefficient / numberOfLivingGuppies;
}
/**
* Returns the percentage of Guppies in this Pool that are female.
*
* @return percentageFemale as a double
*/
public double getFemalePercentage() {
double females = 0.0; // Use a double here to force the compiler to return a double
int numberOfLivingGuppies = 0;
Iterator <Guppy> iterator = guppiesInPool.iterator();
while (iterator.hasNext()) {
Guppy aPoolGuppy = iterator.next();
if (aPoolGuppy.getIsAlive() == true) {
numberOfLivingGuppies++;
if (aPoolGuppy.getIsFemale() == true) {
females += 1.0;
}
}
}
return females / numberOfLivingGuppies;
}
/**
* Returns the median age in weeks of the living Guppies in this Pool.
*
* @return medianAge as a double
*/
public double getMedianAge() {
// If you didn't complete me in A2, try completing me in A3!
return 0.0;
}
/**
* Returns a description of this Pool.
*
* @return description of this Pool as a String.
*/
public String toString() {
return "[name=" + getName() + ",id=" + getIdentificationNumber() + ",volume (litres)=" + getVolume() +
",Temperature (Celsius): " + getTemperature() + ",pH: " + getPH() + ",Nutrient Coefficient: " +
getNutrientCoefficient() + ",Guppy population: " + guppiesInPool.size() + "]";
}
/**
* Prints this Pool's details.
*/
public void printDetails() {
System.out.println(this.toString());
}
/**
* Formats a name and returns it with the first letter in upper case and the
* rest in lower case. If passed null, returns a null.
*
* @param name the name to format
* @return the correctly formatted name, as a String
*/
public static String formatNameTitleCase(String name) {
if (name != null && name.trim().length() > 0) {
String firstLetter = name.trim().toUpperCase().substring(0, 1);
String theRest = name.trim().toLowerCase().substring(1);
return firstLetter + theRest;
} else {
return null;
}
}

public int spawn(){
Iterator<Guppy> itr = guppiesInPool.iterator();
int newBorn=0;
while(itr.hasNext()) {
   ArrayList<Guppy> newBabies=itr.next().spawn();
   newBorn+=newBabies.size();
   guppiesInPool.addAll(newBabies);
}
return newBorn;
}
public int incrementAges(){
Iterator<Guppy> itr = guppiesInPool.iterator();
int deadGp=0;
while(itr.hasNext()) {
   Guppy g=itr.next();
   g.incrementAge();
   if(g.getAgeInWeeks()>=50) deadGp++;
}
return deadGp;
}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote