Question
Write a program to help answer questions like the following: Suppose the species Klingon ox has a population of 100 and a growth rate of
Write a program to help 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? You can assume that this will happen within 10 years.
Use the version of the class Species from Sakais Week 7 Source Code folder as your starting point download it to your Java programs folder. 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, so youll have to figure out which is which; you might assign them to lower and upper Species variables. You can assume the populations are different to start.
The main method in this program has been set up for you to create two Species objects and run a loop with increasing future year numbers (add 1 each time) until the lower population species outnumbers the higher population species, using the predictPopulation() method. Again, you can assume that this will happen within 10 years or not at all.
Note that its possible the species with the smaller population will never outnumber the other species. In this case, your program should just display a suitable message.
Hint: If the species with the lower population has a growth rate less than or equal to the species with the higher population then it will never outnumber the other species, otherwise it must outnumber it at some year in the future. You may assume that both species have growth rates greater than 0 for this exercise.
Your program must output one of the following two statements at the end; here and are the names of the two species, and is the number of years it takes for the lower one to overtake the higher one:
Species will not overtake " . Species will overtake in years
IN JAVA:
/** File name: YearsToOvertake.java This program does the following: Reads in the names, populations and growth rates for two species and calculates the number of years it will take the one with the lower initial population to overtake the other or prints a message that it will never have a higher population). Preconditions: none. Postconditions: Displays either the number of years it takes for the species with the lower population to have a population greater than the other, or a message that it will not exceed the other species' population within ten years. */
public class YearsToOvertake { public static void main(String[] args) { Species s1 = new Species(); Species s2 = new Species(); Species lower = new Species(); // has lower initial population Species higher = new Species(); // has higher initial population
// Read in two species s1.readInput(); s2.readInput();
// Determine which has the lower initial population - if they are // the same, assign the 1st species to "lower" and the 2nd to "higher" /* your code to do that goes here */
// In a loop, find number of years it takes the species with the // lower initial population to overtake the other one. // Here are some useful variables for that loop: int years = 1; int nextPopulationLower; int nextPopulationHigher; /* write the appropriate while loop here - only go to 10 years */ years--; // Adjust years to correct value (it is 1 too many on exit from while-loop)
// If years == 10 "lower" may or may not have overtaken "higher" // so you must do a final check. /* write that check here and print out the appropriate statement */
} }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started