import java.util.Scanner; public class Species { private String name; private in
ID: 3644983 • Letter: I
Question
import java.util.Scanner;public class Species {
private String name;
private int population;
private double 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 species' population?");
population=keyboard.nextInt();
while (population<0)
{
System.out.println("Population cannot be less than 0, please reenter population?");
population=keyboard.nextInt();
}
System.out.println("What is the species' gorwth rate?");
growthRate=keyboard.nextDouble();
}
public void writeOutput()
{
System.out.println("Name = " + name);
System.out.println("Population = " + population);
System.out.println("Growth rate = " + growthRate + "%");
}
/**
Precondition: years is a nonnegative number.
Returns the projected population of the receiving object
after the specified number of years.
*/
public int predictPopulation(int years)
{
int result = 0;
double populationAmount = population;
int count = years;
while ((count > 0) && (populationAmount > 0))
{
populationAmount = (populationAmount + ( growthRate / 100) * populationAmount);
count--;
}
if (populationAmount > 0)
result = (int)populationAmount;
return result;
}
public void setSpecies(String newName, int newPopulation,double newGrowthRate)
{
name = newName;
if (newPopulation >= 0)
population = newPopulation;
else
{
System.out.println( "ERROR: using a negative " + "population.");
System.exit(0);
}
growthRate = newGrowthRate;
}
public String getName()
{
return name;
}
public int getPopulation()
{
return population;
}
public double getGrowthRate()
{
return growthRate;
}
public boolean equals(Species otherObject)
{
return (name.equalsIgnoreCase(otherObject.name)) &&
(population == otherObject.population) &&
(growthRate == otherObject.growthRate);
}
}
Modify the definition of the class Species in Listing 5.19 of Chapter 5{which is above} by removing the method setSpecies and adding the following methods:
-Five constructors one for each instance variable,one with three parameters for the three instance variables, and a default constructor. Be surethat each constructor sets all of the instance variables.
-Four methods named set that can reset values: one is the same as themethod setSpecies in Listing 5.16, and the other three each reset oneof the instance variables.
Then write a test program to test all the methods you have added. Finally,// repeat Programming Project 1 in Chapter 5, but be sure to use some constructor other than the default constructor when you define new objects of the class Species.
Explanation / Answer
Five Constructors: 1: public Species(String name) { this.name=name; this.population=0; this.growthRate=0.0; } 2: 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; } 3: public Species(double growthRate) { this.name="" this.population=0; this.growthRate=growthRate; } 4: 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; } 5:Default Constructor public Species() { this.name=""; this.population=0; this.growthRate=0.0; } Four Set Methods: 1: public void setSpecies(String name, int population, double growthRate) [already given] 2: public void setName(String name) { this.name=name; } 3: public void setPopulation(int population) { if (population >= 0) this.population = population; else { System.out.println( "ERROR: using a negative " + "population."); System.exit(0); } } 4: public void setGrowthRate(double growthRate) { this.growthRate=growthRate; } Test Program: public static void main(String[] args) { Species s1("Apes"),s2(6000),s3(6.3),s4("Human",567980,98.2); System.out.println("s1: "+s1.getName()+" "+s1.getPopulation()+" "+s1.getGrowthRate()); System.out.println("s2: "+s2.getName()+" "+s2.getPopulation()+" "+s2.getGrowthRate()); System.out.println("s3: "+s3.getName()+" "+s3.getPopulation()+" "+s3.getGrowthRate()); System.out.println("s4: "+s4.getName()+" "+s4.getPopulation()+" "+s4.getGrowthRate()); s1.setPopulation(122220); s1.setGrowthRate(23.3); s2.setName("Fish"); s2.setGrowthRate(23.35); s3.setName("Bacteria"); s3.setPopulation(35568); System.out.println("s1: "+s1.getName()+" "+s1.getPopulation()+" "+s1.getGrowthRate()); System.out.println("s2: "+s2.getName()+" "+s2.getPopulation()+" "+s2.getGrowthRate()); System.out.println("s3: "+s3.getName()+" "+s3.getPopulation()+" "+s3.getGrowthRate()); System.out.println("s4: "+s4.getName()+" "+s4.getPopulation()+" "+s4.getGrowthRate()); }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.