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

Using the Species class: 1) Write a static method in the main called average3 th

ID: 664927 • Letter: U

Question

Using the Species class:

1) Write a static method in the main called average3 that averages the population of 3 Species' instances and returns the value in adouble.

2) Write a static method in the main called equalsPopulation and returns a boolean value indicating that two populations are equal.

3) Write a static method in the main called incrementPopulation that increments the populaton of the owner class.

4) Write a static method called leterCount that returns the number of characters in the name Species.

5) Give the client code that would invoke these methods and the one in the Species class

So, I have my species class done. 1 through 5 are also in the Species class. Need help finishing the main and client code.

MAIN

import java.util.Scanner;

public class SpeciesEqualsDemo {

  
  
   public static void main(String[] args) {
       // TODO Auto-generated method stub
       Species[] speciesInfo = new Species[3];
       Species s1;
       int i;
       for(i = 0; i < 3; i++)
       {
           s1 = new Species();
           speciesInfo[i] = s1;
       }
       Species[] speciesOne;
       Species[] speciesTwo;
       Species[] speciesThree;
      
       speciesOne = new Species[3];
       readSpecies(speciesOne);
      
      
      
       speciesTwo = new Species[3];
       readSpecies(speciesTwo);
      
      
       speciesThree = new Species[3];
       readSpecies(speciesThree);
      
   }
   private static void readSpecies(Species[] type) {
       // TODO Auto-generated method stub

       Scanner keyboard = new Scanner(System.in);
       for (int i = 0; i < 3; i++)
       {
           type[i] = new Species();
           System.out.println();
           String name = keyboard.nextLine();
           double population = keyboard.nextDouble();
           double growthRate = keyboard.nextDouble();
           type[i] = new Species (name, population, growthRate);
       }
   }
       private static void average3(){
      
   }
       private static void equalpopulation(){
          
       }
       private static void incrementPopulation(){
          
       }
       private static void fastGrowing(){
          
       }
       private static void letterCount(){
          
       }
}


Species Class

import java.util.*;
public class Species {
   private String name;
   private double population;
   private double growthRate;
   public Species()
   {
       this.name="";
       this.population=0;
       this.growthRate=0.0;
   }
   public Species(String newName, double newPopulation, double newGrowthRate)
   {
       name = newName;
       population = newPopulation;
       growthRate = newGrowthRate;
   }
   public Species(String name)
   {
       this.name=name;
       this.population=0;
       this.growthRate=0.0;
   }
   public Species(String name, double newPopulation)
   {
       this.name = name;
       this.population = newPopulation;
       this.growthRate = 0.0;
   }
   public Species(int population)
   {
       this.name="";
       if (population >= 0)
           this.population = population;
       else
       {
           System.out.println( "ERROR: using a negative " + "population.");
           System.exit(0);
       }
       this.growthRate=0.0;
   }
   public Species(double growthRate)
   {
       this.name="";
       this.population=0;
       this.growthRate=growthRate;
   }
   public Species(String name, int population, double growthRate)
   {
       this.name=name;
       if (population >= 0)
           this.population = population;
       else
       {
           System.out.println( "ERROR: using a negative " + "population.");
           System.exit(0);
       }
       this.growthRate=growthRate;
   }
   public void setSpecies(String newName, int newPopulation, double newGrowthRate){
       name = newName;
       if (newPopulation >= 0)
           population = newPopulation;
       else{
           System.out.println("Can't use negative number");
           System.exit(0);
       }
       growthRate = newGrowthRate;
   }
   public void setName(String name)
   {
       this.name=name;
   }
   public void setPopulation(int population)
   {
       if (population >= 0)
           this.population = population;
       else
       {
           System.out.println( "ERROR: using a negative " + "population.");
           System.exit(0);
       }
   }
   public void setGrowthRate(double growthRate)
   {
       this.growthRate=growthRate;
   }

   public void readInput(){
       Scanner keyboard = new Scanner(System.in);
       System.out.println("What is the species name?");
       name = keyboard.nextLine();

       System.out.println("What is the population of the species?");
       population = keyboard.nextInt();

       while(population < 0 ){
           System.out.println("Population can't be less than zero!");
           System.out.println("Reenter population");
           population = keyboard.nextInt();
       }
       System.out.print("Enter growth rate(% increase per year)");
       growthRate = keyboard.nextDouble();
   }
   public void writeOutput(){
       System.out.print("Name =" + name);
       System.out.print("Population =" + population);
       System.out.print("Growth rate =" + growthRate);
   }
   public int predictPopulation(int years){
       int result = 0;
       double populationAmount = population;
       int count = years;
       while((count > 0) && (population > 0))
       {
           populationAmount = (populationAmount + (growthRate/100)* populationAmount);
           count--;
       }
       if (populationAmount>0){
           result = (int)populationAmount;
           return result;
       }
   }
   public String getName(){
       return name;
   }
   public double getPopulation(){
       return population;
   }
   public double getGrowthRate(){
       return growthRate;
   }
   public static double average3(Species[] average)
   {
       double Average, sum = 0;
       for(int i = 0; i < 3; i++)
       {
           sum = sum + average[i].population;
       }
       Average = sum / 3;
       return Average;
   }
   public boolean equalsPopulation(Species other)
   {
       if(population == other.population)
           return true;
       else
           return false;
   }
   public static boolean equalsPopulation(double thisPopulation, double otherPopulation)
   {
       if(thisPopulation == otherPopulation)
           return true;
       else
           return false;
   }
   public void incrementPopulation(){
       population++;
   }
   public boolean fastGrowing(){
       if(growthRate >= .2)
           return true;
       else
           return false;
   }
   public int letterCount(){
       int count = 0;
       for(int i = 0; i < name.length(); i++)
       {
           count++;
       }
       return count;
   }
}

Explanation / Answer

Modified code:

import java.util.Scanner;
public class SpeciesEqualsDemo extends Species
{

public static void main(String[] args) {
  // TODO Auto-generated method stub
  Species[] speciesInfo = new Species[3];
  Species s1;
  int i;
  for (i = 0; i < 3; i++) {
   s1 = new Species();
   speciesInfo[i] = s1;
  }
  Species[] speciesOne;
  Species[] speciesTwo;
  Species[] speciesThree;

  speciesOne = new Species[3];
  readSpecies(speciesOne);

  speciesTwo = new Species[3];
  readSpecies(speciesTwo);

  speciesThree = new Species[3];
  readSpecies(speciesThree);

}

private static void readSpecies(Species[] type) {
  // TODO Auto-generated method stub

  Scanner keyboard = new Scanner(System.in);
  for (int i = 0; i < 3; i++) {
   type[i] = new Species();
   System.out.println();
   String name = keyboard.nextLine();
   double population = keyboard.nextDouble();
   double growthRate = keyboard.nextDouble();
   type[i] = new Species(name, population, growthRate);
  }
}

public static double average3(Species[] average) {
  double Average, sum = 0;
  for (int i = 0; i < 3; i++) {
   sum = sum + average[i].population;
  }
  Average = sum / 3;
  return Average;
}

public boolean equalsPopulation(Species other) {
  if (population == other.population)
   return true;
  else
   return false;
}

public static boolean equalsPopulation(double thisPopulation,double otherPopulation) {
  if (thisPopulation == otherPopulation)
   return true;
  else
   return false;
}

public void incrementPopulation() {
  population++;
}

public boolean fastGrowing() {
  if (growthRate >= .2)
   return true;
  else
   return false;
}

public int letterCount() {
  int count = 0;
  for (int i = 0; i < name.length(); i++) {
   count++;
  }
  return count;
}
}

import java.util.*;

public class Species {
protected String name;
protected double population;
protected double growthRate;

public Species() {
  this.name = "";
  this.population = 0;
  this.growthRate = 0.0;
}

public Species(String newName, double newPopulation, double newGrowthRate) {
  name = newName;
  population = newPopulation;
  growthRate = newGrowthRate;
}

public Species(String name) {
  this.name = name;
  this.population = 0;
  this.growthRate = 0.0;
}

public Species(String name, double newPopulation) {
  this.name = name;
  this.population = newPopulation;
  this.growthRate = 0.0;
}

public Species(int population) {
  this.name = "";
  if (population >= 0)
   this.population = population;
  else {
   System.out.println("ERROR: using a negative " + "population.");
   System.exit(0);
  }
  this.growthRate = 0.0;
}

public Species(double growthRate) {
  this.name = "";
  this.population = 0;
  this.growthRate = growthRate;
}

public Species(String name, int population, double growthRate) {
  this.name = name;
  if (population >= 0)
   this.population = population;
  else {
   System.out.println("ERROR: using a negative " + "population.");
   System.exit(0);
  }
  this.growthRate = growthRate;
}

public void setSpecies(String newName, int newPopulation,
   double newGrowthRate) {
  name = newName;
  if (newPopulation >= 0)
   population = newPopulation;
  else {
   System.out.println("Can't use negative number");
   System.exit(0);
  }
  growthRate = newGrowthRate;
}

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

public void setPopulation(int population) {
  if (population >= 0)
   this.population = population;
  else {
   System.out.println("ERROR: using a negative " + "population.");
   System.exit(0);
  }
}

public void setGrowthRate(double growthRate) {
  this.growthRate = growthRate;
}

public void readInput() {
  Scanner keyboard = new Scanner(System.in);
  System.out.println("What is the species name?");
  name = keyboard.nextLine();

  System.out.println("What is the population of the species?");
  population = keyboard.nextInt();

  while (population < 0) {
   System.out.println("Population can't be less than zero!");
   System.out.println("Reenter population");
   population = keyboard.nextInt();
  }
  System.out.print("Enter growth rate(% increase per year)");
  growthRate = keyboard.nextDouble();
}

public void writeOutput() {
  System.out.print("Name =" + name);
  System.out.print("Population =" + population);
  System.out.print("Growth rate =" + growthRate);
}

public int predictPopulation(int years) {
  int result = 0;
  double populationAmount = population;
  int count = years;
  while ((count > 0) && (population > 0)) {
   populationAmount = (populationAmount + (growthRate / 100)
     * populationAmount);
   count--;
  }
  if (populationAmount > 0) {
   result = (int) populationAmount;
   return result;
  }
  return count;
}

public String getName() {
  return name;
}

public double getPopulation() {
  return population;
}

public double getGrowthRate() {
  return growthRate;
}


}

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