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

You must implement the following: 1. A new class called Pool which contains the

ID: 3776440 • Letter: Y

Question

You must implement the following:


1. A new class called Pool which contains the following elements:

(a) The following public symbolic constants. Use these variable names
and data types. Remember symbolic constants are static and final.
Do not use di erent names or data types, and do not add any other
symbolic constants. Use these constants instead of magic numbers
inside your code:

i. DEFAULT_POOL_NAME a String equal to Unnamed"
ii. DEFAULT_POOL_TEMP_CELSIUS a double equal to 40.0
iii. MINIMUM_POOL_TEMP_CELSIUS a double equal to 0.0
iv. MAXIMUM_POOL_TEMP_CELSIUS a double equal to 100.0
v. NEUTRAL_PH a double equal to 7.0
vi. DEFAULT_NUTRIENT_COEFFICIENT a double equal to
0.50
vii. MINIMUM_NUTRIENT_COEFFICIENT a double equal
to 0.0
viii. MAXIMUM_NUTRIENT_COEFFICIENT a double equal
to 1.0.


(b) The following 8 private instance variables. Use these variable
names and data types. Do not use di erent names or data types,
and do not add any other instance variables:

i. name a String
ii. volumeLitres a double
iii. temperatureCelsius a double
iv. pH a double that will always be between 0 and 14.0 inclusive
v. nutrientCoecient, a double that will always be between 0
and 1.0 inclusive
vi. identificationNumber an int
vii. guppiesInPool an array of Guppy
viii. randomNumberGenerator a Random object.


(c) numberOfPools a private static variable with an initial value of 0
that is incremented by 1 each time a new Pool is constructed. Further
details are provided in the section about constructors (remember that
static variables are shared by all objects of a class).


(d) Two (2) constructors:

i. Zero-parameter constructor which sets volumeLitres to 0.0,
and also sets:

A. name is set to DEFAULT_POOL_NAME
B. temperatureCelsius is set to DEFAULT_POOL_TEMP_CELSIUS
C. pH is set to NEUTRAL_PH
D. nutrientCoecient is set to
DEFAULT_NUTRIENT_COEFFICIENT
E. guppiesInPool is initialized to be an empty array of Guppy
(an array of size zero), and randomNumberGenerator
is initialized to be a new Random (use the zero-parameter
constructor in the Random class).

ii. Multi-Parameter constructor which accepts a parameter for
each instance variable (except the identification number, the list
of guppies, and the random number generator) and validates it:

A. The parameter passed for the name field must not be null,
and must not be an empty String or a String composed only
of whitespace. If the argument passed as the parameter is
either null or an empty/whitespace String, set the field to
DEFAULT_POOL_NAME. Otherwise, format the value
correctly (remove the whitespace, capitalize the first letter,
set the rest to lower case) before storing it in the instance
variable.
B. The parameter passed for the volumeLitres field must be
positive, otherwise set the instance variable to 0.0.
C. The parameter passed for the temperatureCelsius instance
variable must be greater than or equal to MINIMUM_POOL_TEMP_CELSIUS
and less than or equal to MAXIMUM_POOL_TEMP_CELSIUS,
else the instance variable must be set to the default temper-
ature.
D. the parameter passed for the pH field must be within the
range [0, 14.0], otherwise set the instance variable to NEU-
TRAL_PH.

E. the parameter passed for the nutrientCoecient must be
within the range [0, 1.0] (between 0 and 1.0 inclusive), oth-
erwise set the instance variable to
DEFAULT_NUTRIENT_COEFFICIENT.
F. guppiesInPool is initialized to be a new array of Guppy.
How big should it be? Divide the volume of the Pool by
Guppy. MINIMUM_WATER_VOLUME_ML and create an ar-
ray of that size.
G. randomNumberGenerator is initialized to be a new Ran-
dom (use the zero-parameter constructor in the Random
class).

iii. Both constructors must create and assign a unique numerical
identification number to the identificationNumber instance
variable. A good way to do this is to increment the static counter
field from inside the constructor, and then assign that newly
incremented value to the new Pool object's identification field.


(e) Accessors (getters) and mutators (setters) for each instance vari-
able except a) do not create an accessor or mutator for the Ran-
dom object b) do not create a mutator for the identification number.
The mutator for guppiesInPool should ignore a null parameter.
(Note: +1 bonus for moving the data validation logic from the multi-
parameter constructor to the mutators).


(f) a method with the header
public void changeNutrientCoefficient(double delta)
which adds the value passed in the delta parameter to the Pool's
nutrient coecient. The parameter may be positive or negative, but
if the new value of the nutrient coecient would be less than the
minimum, set the nutrient coecient to the minimum. If the new
value would be greater than the maximum, set the new value to the
maximum.


(g) a method with the header
public void changeTemperature(double delta)
which changes the temperature by the amount passed in the delta
parameter. If the new Pool temperature would be less than the min-
imum, set the pool temperature to the minimum. If the new Pool
temperature would be greater than the maximum, set it to the max-
imum.


(h) a method with the header public static int getNumberCreated()
which returns the total number of Pools created.


(i) a method with the header public String toString() which
returns a String representation of the Pool. The String should be
formatted identically to the String produced by the Guppy class in
the sample solution, i.e., open and close with square brackets, no
spaces, each instance variable separated by a comma.


(j) a method with the header public void printDetails() which
prints the Pool's details to the screen.


(k) a method with the header public boolean addGuppy(Guppy
guppy)
which adds a Guppy to the Pool. If the parameter equals
null, the method should do nothing. If the parameter is not equal to
null, use a for loop to search through the array until you find a cell
which contains null. Put the Guppy there. If a Guppy is successfully
added, return true, else return false.


(l) a method with the header public int getPopulation() which
returns the number of Guppies in the pool. Return the number of
Guppies in the array, i.e., loop through the array and count the
number of indices which contain a Guppy and not a null.


(m) a method with the header public int applyNutrientCoefficient()
that calculates which Guppies in the Pool have died of malnutrition,
and returns the number of deaths. Use a for loop to iterate over the
array of Guppy in the Pool. For each non-null index in the array,
i.e., each cell that contains a Guppy, generate a di erent random
number between 0.0 and 1.0 inclusive using the Random method
nextDouble(). If this randomly generated number is greater than
the Pool's nutrient coecient, kill that Guppy by setting the appro-
priate boolean field in the Guppy. Note that this method does not
remove any dead Guppies from the Pool, it just kills them. Do not
do anything else.


(n) a method with the header public int removeDeadGuppies()
which uses a for loop to remove Guppies that are not alive from the
Pool. For each Guppy, if it is dead, remove it by setting the contents
of the array at that index to be null. This method should keep track
of how many Guppies are removed. Return the number of Guppies
that have been removed.


(o) a method with the header public double getGuppyVolumeRequirementInLitres()
which returns the total number of litres required by all of the living
Guppies in the Pool. Note that the Guppy class has a helpful method
with returns the total number of milliLitres required per Guppy. You
will need to convert this to litres (note that 1,000 mL = 1 L).


(p) a method with the header public double getAverageAgeInWeeks()
which returns the average age of the living Guppies in the Pool.


(q) a method with the header public double getAverageHealthCoefficient()
which returns the average health coecient of the living Guppies in
the Pool.


(r) a method with the header public double getFemalePercentage()
which returns a double representing the percentage of living Guppies
in the Pool that are female.


(s) a method with the header public double getMedianAge()
which returns median age of the living Guppies in the Pool.

Modify the class bellow to match the information above:

/**
* This class describes a software that checks informations,
* health condions and the threat rates of a specific
* fish in an Aquarium.
*
*
* @author Bakr Al-Humaimidi
* @version 1.0
*/
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;
   }

   /**
   *
   * @param newGenus genus of the fishes
   * @param newSpecies species of the fishes
   * @param newAgeInWeeks of age in weeks of the fishes
   * @param newIsFemale the gender of the fishes
   * @param newGenerationNumber the generation number of the fishes
   * @param newHealthCoefficient are the fishes healthy
   */
   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;
       }
   }

   /**
   *
   * @return MINIMUM_WATER_VOLUME_ML of the Aquarium
   */
   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;
   }

   /**
   *
   * @param delta of the health coefficient
   */
   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;
       }
   }

   /**
   *
   * @return numberOfGuppiesBorn of the fishes
   */
   public static int getNumberOfGuppiesBorn() {
       return numberOfGuppiesBorn;
   }

   /**
   *
   * @return genus of the fishes
   */
   public String getGenus() {
       return genus;
   }

   /**
   *
   * @return species of the fishes
   */
   public String getSpecies() {
       return species;
   }

   /**
   *
   * @return ageInWeeks of the fishes
   */
   public int getAgeInWeeks() {
       return ageInWeeks;
   }

   /**
   *
   * @return isFemale gender of the fishes
   */
   public boolean getIsFemale() {
       return isFemale;
   }

   /**
   *
   * @return generationNumber of the fishes
   */
   public int getGenerationNumber() {
       return generationNumber;
   }

   /**
   *
   * @return isAlive of the fishes
   */
   public boolean getIsAlive() {
       return isAlive;
   }

   /**
   *
   * @return healthCoefficient the health conditions of the fishes
   */
   public double getHealthCoefficient() {
       return healthCoefficient;
   }

   /**
   *
   * @param genus of the fishes
   */
   public void setGenus(String genus) {
       if (genus != null && !genus.isEmpty())
           this.genus = Character.toUpperCase(genus.charAt(0)) + genus.substring(1).toLowerCase();
       ;
   }

   /**
   *
   * @param species of the fishes
   */
   public void setSpecies(String species) {
       if (species != null && !species.isEmpty())
           this.species = species.toLowerCase();
   }

   /**
   *
   * @param ageInWeeks of the fishes
   */
   public void setAgeInWeeks(int ageInWeeks) {
       if (ageInWeeks >= 0 && ageInWeeks <= MAXIMUM_AGE_IN_WEEKS)
           this.ageInWeeks = ageInWeeks;
   }

   /**
   *
   * @param isFemale gender of the fishes
   */
   public void setIsFemale(boolean isFemale) {
       this.isFemale = isFemale;
   }

   /**
   *
   * @param generationNumber of the fishes
   */
   public void setGenerationNumber(int generationNumber) {
       if (generationNumber >= 0)
           this.generationNumber = generationNumber;
   }

   /**
   *
   * @param isAlive of the fishes
   */
   public void setIsAlive(boolean isAlive) {
       this.isAlive = isAlive;
   }

   /**
   *
   * @param healthCoefficient the health conditions of the fishes
   */
   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 + "]";
   }
}

Explanation / Answer

Pool class is now implemented as per the requirements.

import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

/**
* This class contains the information about pools, the water nutrients
* and also the guppies in the pool
* @author kasturi
*
*/
public class Pool {
  
   final static String DEFAULT_POOL_NAME = "Unnamed";
   final static double DEFAULT_POOL_TEMP_CELSIUS = 40.0;
   final static double MINIMUM_POOL_TEMP_CELSIUS = 0.0;
   final static double MAXIMUM_POOL_TEMP_CELSIUS = 100.0;
   final static double NEUTRAL_PH = 7.0;
   final static double DEFAULT_NUTRIENT_COEFFICIENT = 0.50;
   final static double MINIMUM_NUTRIENT_COEFFICIENT = 0.0;
   final static double MAXIMUM_NUTRIENT_COEFFICIENT = 1.0;
  
   private String name;
   private double volumeLitres;
   private double temperatureCelsius;
   private double pH;
   private double nutrientCoecient;
   private int identificationNumber;
   private Guppy[] guppiesInPool;
   private Random randomNumberGenerator;
  
   private static int numberOfPools = 0;
   /**
   * setting up the pool
   */
   Pool()
   {
       volumeLitres = 0.0;
       name = DEFAULT_POOL_NAME;
       temperatureCelsius = DEFAULT_POOL_TEMP_CELSIUS;
       pH = NEUTRAL_PH;
       nutrientCoecient = DEFAULT_NUTRIENT_COEFFICIENT;
       guppiesInPool = new Guppy[0];
       randomNumberGenerator = new Random();
   }
  
   /**
   *
   * @param name of the pool
   * @param litres of water in the pool
   * @param temperature of the pool in celsius
   * @param pH value of the pool water
   * @param nutrient coeeficient in the pool
   */
   Pool(String name, double volumeLitres, double temperatureCelsius, double pH, double nutrientCoecient)
   {
       if(name == null || name.equals(" "))
           this.name = DEFAULT_POOL_NAME;
       else
       {
           name.trim();
           this.name = name.substring(0,1).toUpperCase() + name.substring(1).toLowerCase();
       }
      
       if(volumeLitres >= 0)
           this.volumeLitres = volumeLitres;
       else
           this.volumeLitres = 0.0;
      
       if(temperatureCelsius <= MAXIMUM_POOL_TEMP_CELSIUS && temperatureCelsius >= MINIMUM_POOL_TEMP_CELSIUS)
           this.temperatureCelsius = temperatureCelsius;
       else
           this.temperatureCelsius = DEFAULT_POOL_TEMP_CELSIUS;
      
       if(pH >= 0 && pH <= 14.0)
           this.pH = pH;
       else this.pH = NEUTRAL_PH;
      
       if(nutrientCoecient >=0 && nutrientCoecient <= 14.0)
           this.nutrientCoecient = nutrientCoecient;
       else this.nutrientCoecient = DEFAULT_NUTRIENT_COEFFICIENT;
      
       int sizeOfPool = (int) (volumeLitres/Guppy.MINIMUM_WATER_VOLUME_ML);
       guppiesInPool = new Guppy[sizeOfPool];
      
       randomNumberGenerator = new Random();
      
       identificationNumber = ++numberOfPools;
   }

   public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }

   public double getVolumeLitres() {
       return volumeLitres;
   }

   public void setVolumeLitres(double volumeLitres) {
       this.volumeLitres = volumeLitres;
   }

   public double getTemperatureCelsius() {
       return temperatureCelsius;
   }

   public void setTemperatureCelsius(double temperatureCelsius) {
       this.temperatureCelsius = temperatureCelsius;
   }

   public double getpH() {
       return pH;
   }

   public void setpH(double pH) {
       this.pH = pH;
   }

   public double getNutrientCoecient() {
       return nutrientCoecient;
   }

   public void setNutrientCoecient(double nutrientCoecient) {
       this.nutrientCoecient = nutrientCoecient;
   }

   public Guppy[] getGuppiesInPool() {
       return guppiesInPool;
   }

   public void setGuppiesInPool(Guppy[] guppiesInPool) {
       if(guppiesInPool != null)
           this.guppiesInPool = guppiesInPool;
   }

   public static int getNumberOfPools() {
       return numberOfPools;
   }

   public static void setNumberOfPools(int numberOfPools) {
       Pool.numberOfPools = numberOfPools;
   }
  
   public void changeNutrientCoefficient(double delta)
   {
       if(delta < MINIMUM_NUTRIENT_COEFFICIENT)
           this.nutrientCoecient = MINIMUM_NUTRIENT_COEFFICIENT;
       else if(delta > MAXIMUM_NUTRIENT_COEFFICIENT)
           this.nutrientCoecient = MAXIMUM_NUTRIENT_COEFFICIENT;
       else this.nutrientCoecient = delta;
   }
  
   public void changeTemperature(double delta)
   {
       if(delta < MINIMUM_POOL_TEMP_CELSIUS)
           this.temperatureCelsius = MINIMUM_POOL_TEMP_CELSIUS;
       else if(delta > MAXIMUM_POOL_TEMP_CELSIUS)
           this.temperatureCelsius = MAXIMUM_POOL_TEMP_CELSIUS;
       else this.temperatureCelsius = delta;
   }
  
   public static int getNumberCreated()
   {
       return numberOfPools;
   }
  
   public String toString()
   {
       return "[name=" + name + ",volume=" + volumeLitres + ",temperature=" + temperatureCelsius + ",pH=" + pH + ",nutrientCoefficient="
                   +nutrientCoecient + ",identificationNumber="+ + identificationNumber +
                   "]";
   }
  
   public void printDetails()
   {
       System.out.println("Name: " + name);
       System.out.println("Volume: " + volumeLitres);
       System.out.println("Temperature: " + temperatureCelsius);
       System.out.println("pH: " + pH);
       System.out.println("Nutrient Coefficient: " + nutrientCoecient);
       System.out.println("Identification Number: " + identificationNumber);
       //not sure if guppies have to be printed
   }
  
   public boolean addGuppy(Guppy guppy)
   {
       if(guppy != null)
       {
           for(int i=0; i<guppiesInPool.length; i++)
           {
               if(guppiesInPool[i] == null)
               {
                   guppiesInPool[i] = guppy;
                   return true;
               }
           }
       }
       return false;
   }
     
   public int getPopulation()
   {
       int guppyCount = 0;
       for(int i=0; i<guppiesInPool.length; i++)
       {
           if(guppiesInPool[i] != null)
           {
               ++guppyCount;
           }
       }
       return guppyCount;
   }
     
   public int applyNutrientCoefficient()
   {
       int numOfDeaths = 0;
       for(int i=0; i<guppiesInPool.length; i++)
       {
           if(guppiesInPool[i] != null)
           {
               int random = (int) (randomNumberGenerator.nextDouble());
               if(random > nutrientCoecient)
               {
                   guppiesInPool[i].setIsAlive(false);
                   numOfDeaths++;
               }
           }
       }
       return numOfDeaths;
   }
     
   public int removeDeadGuppies()
   {
       int numberOfGuppiesRemoved = 0;
       for(int i=0; i<guppiesInPool.length; i++)
       {
           if(guppiesInPool[i].getIsAlive() == false)
           {
               guppiesInPool[i] = null;
               numberOfGuppiesRemoved++;
           }
       }
       return numberOfGuppiesRemoved;
   }
     
   public double getGuppyVolumeRequirementInLitres()
   {
       double totalLitres = 0.0;
       for(int i=0; i<guppiesInPool.length; i++)
       {
           if(guppiesInPool[i].getIsAlive())
           {
               totalLitres += guppiesInPool[i].getVoulmeNeeded();
           }
       }
       return totalLitres/1000;
   }
     
   public double getAverageAgeInWeeks()
   {
       int numberOfGuppiesAlive = 0;
       int age = 0;
       for(int i=0; i<guppiesInPool.length; i++)
       {
           if(guppiesInPool[i].getIsAlive())
           {
               age += guppiesInPool[i].getAgeInWeeks();
               numberOfGuppiesAlive++;
           }
       }
       return age/numberOfGuppiesAlive;
   }
     
   public double getAverageHealthCoefficient()
   {
       int numberOfGuppiesAlive = 0;
       double healthCoeffs = 0.0;
       for(int i=0; i<guppiesInPool.length; i++)
       {
           if(guppiesInPool[i].getIsAlive())
           {
               healthCoeffs += guppiesInPool[i].getHealthCoefficient();
           }
       }
       return healthCoeffs/numberOfGuppiesAlive;
   }
     
   public double getFemalePercentage()
   {
       int numberOfGuppiesAlive = 0;
       int numberOfFemaleGuppies = 0;
       for(int i=0; i<guppiesInPool.length; i++)
       {
           if(guppiesInPool[i].getIsAlive())
           {
               numberOfGuppiesAlive++;
               if(guppiesInPool[i].getIsFemale())
               {
                   numberOfFemaleGuppies++;
               }
           }
       }
       return (numberOfFemaleGuppies * 100)/numberOfGuppiesAlive;
   }
     
   public double getMedianAge()
   {
       int middle = 0;
       ArrayList<Integer> ageList = new ArrayList<Integer>();
       for(int i=0; i<guppiesInPool.length; i++)
       {
           if(guppiesInPool[i].getIsAlive())
           {
               ageList.add(guppiesInPool[i].getAgeInWeeks());
           }
       }
       Collections.sort(ageList);
       middle = ageList.size()/2;
       if(ageList.size()%2 == 1)
       {
           return ageList.get(middle);
       }
       else
       {
           return (ageList.get(middle-1) + ageList.get(middle))/2.0;
       }
   }
}

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