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

Write a program to answer questions like the following: Suppose the species Klin

ID: 3634092 • Letter: W

Question

Write a program to answer questions like the following: Suppose the species Klingon ox has a population of 100 and a growth rate of 15 percent, and the species elephant has a population of 10 and a growth rate of 35 percent. How many years will it take for the elephant population to exceed the Klingon ox population? Use the class Species in Listing 5.19. Your program will ask for the data on both species and will respond by telling you how many years it will take for the species that starts with the lower population to outnumber the species that starts with the higher population. The two species may be entered in any order. It is possible that the species with the smaller population will never outnumber the other species. In this case, your program should display a suitable message stating this fact. Below is the Species class:

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 population of the species?");
population = keyboard.nextInt( );
while (population < 0)
{
System.out.println("Population cannot be negative.");
System.out.println("Reenter population:");
population = keyboard.nextInt( );
}

System.out.println("Enter growth rate (% increase per year):");
growthRate = keyboard.nextDouble( );
}

public void writeOutput( )
{
System.out.println("Name = " + name);
System.out.println("Population = " + population);
System.out.println("Growth rate = " + growthRate + "%");
}
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);
}
}

Explanation / Answer

/* Program for calculate growth in population */ import java.util.Scanner; // create class Species public class Species { private String name; private int population; private double growthRate; // method reads the input data from user public void readInput(){ Scanner keyboard=new Scanner(System.in); System.out.println("What is the name of Species? "); name=keyboard.nextLine(); System.out.println("What is the population of the Species ? "); population=keyboard.nextInt(); // check the population number while(population
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