Write a program to answer questions like the following: Suppose the species Klin
ID: 3624346 • 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.17. 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.Explanation / Answer
Since you don't provide any details on the Species class to use with it, let's just go with population 1 and population 2. Also, the never-outnumber case can just be a really large number of years as the max value beyond which we consider the outnumbering unlikely.
Here is a basic version of the code, you can enhance it as you see fit:
======================================================================================
import java.util.Scanner;
public class PopulationGrowthCalculator{
public static void main(String[] args) {
// We get the information for both populations from the user
Scanner scan = new Scanner(System.in); // we use this to read user input
System.out.println("Enter Population 1 >> ");
double pop1 = scan.nextDouble();
System.out.println("Enter Growth Rate 1 >> ");
double rate1 = scan.nextDouble();
System.out.println("Enter Population 2 >> ");
double pop2 = scan.nextDouble();
System.out.println("Enter Growth Rate 2 >> ");
double rate2 = scan.nextDouble();
//let's say if it's been 100000 years and the population still couldn't catch up, then it's not happening
//100000 is just a personal random choice, you can make it any really big number you want
int yearsToOutNumber = 0;
if(pop1 > pop2)
// the first population is greater,
//so we compute how long it takes the second one to outnumber it
{
while(pop1 > pop2)
{
if(yearsToOutNumber > 100000)
{
System.out.println("Population 2 won't overtake population 1 anytime soon...");
break;
}
pop2 = Math.floor(pop2*(1 + rate2/100)); // we discard the decimal detail of the population
pop1 = Math.floor(pop1 * (1 + rate1/100)); // we discard the decimal detail of the population
yearsToOutNumber++;
}
//once the program exits the while loop -
//it means that pop1 > pop2 is no loger true
//so either pop2 outnumbered pop1
if(yearsToOutNumber < 100000)
System.out.println("It takes " + yearsToOutNumber + " years for population 2 to outnumber population 1.");
}
else if(pop2 > pop1)
// the second population is greater
//so we compute how long it takes the first one to outnumber it
{
while(pop2 > pop1)
{
if(yearsToOutNumber > 100000)
{
System.out.println("Population 1 won't overtake population 2 anytime soon...");
break;
}
pop2 = Math.floor(pop2*(1 + rate2/100)); // we discard the decimal detail of the population
pop1 = Math.floor(pop1 * (1 + rate1/100)); // we discard the decimal detail of the population
yearsToOutNumber++;
}
//once the program exits the while loop -
//it means that pop2 > pop1 is no loger true
//so either pop1 outnumbered pop2
if(yearsToOutNumber < 100000)
System.out.println("It takes " + yearsToOutNumber + " years for population 1 to outnumber population 2.");
}
else
{
System.out.println("Both populations are already equal. Move on...");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.