This is supposed to predict a population of organisms with a recursive method bu
ID: 672798 • Letter: T
Question
This is supposed to predict a population of organisms with a recursive method but Im having problems.
*/
public class PopulationRecur {
public static void main(String[] args) {
int numDays;
int days;
int organism;
int increase;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the number of organisms: ");
organism = keyboard.nextInt();
while (organism < 2) {
System.out.print("The starting population must me 2 or higher: ");
organism = keyboard.nextInt();
}
System.out.print("Enter the daily increase: ");
increase = keyboard.nextInt();
while (increase < 0) {
System.out.print("The daily increase must be a positive number: ");
increase = keyboard.nextInt();
}
System.out.print("Enter the number of days the organisms will multiply:");
days = keyboard.nextInt();
while (days < 1) {
System.out.print("The number of days must be 1 or greater: ");
days = keyboard.nextInt();
}
numDays = days;
showPopulation(numDays,days,organism,increase);
}
private static double showPopulation(int dayNum, int days, double organisms, double dailyIncrease) {
if (dayNum == days) {
return organisms;
} else {
}
System.out.println("Population for day" + (dayNum + 1) + " is " + (organisms + organisms * (dailyIncrease / 100)));
return showPopulation(dayNum + 1, days, organisms + organisms * (dailyIncrease / 100), dailyIncrease);
}
}
Explanation / Answer
The syntactical problem with your code is:
1. You missed importing the packages. As you are using the Scanner class, you should import java.util.Scanner.
The logical problems with your code is:
1. You called a function showPopulation() to display the daily population details. Its good till then. But, look at the code just before the call to that function in main(), and the almost first line in the showPopulation() function. In main, you are assigning days to numDays (using the code numDays = days), which means after executing this line the value in numDays and days is the same. And you are passing both these variables as the first and second parameter to the function showPopulation().Immediately in that function, almost in the first step, you are checking with the condition(exit case of the recursive function) if(dayNum == days), which will obviously satisfy as both the first and second parameter values are the same(as you did in the just before step of the function call). Hence it will be satisfied, and will exit immediately. And the System.out.println() function will never be executed. Infact there is no recursion at all.
The solution to this is:
Instead of assigning days value to numDays, in main(), you should assign zero(0) to numDays. Please do note that this variable is storing the first day, and will proceed till the last day(means the value which you read into the variable days). So, if you assign 0 to that variable and call the recursion, it means, starting from day-1 till day-days.
Now it will run recursively for all the days.
2. Still it is showing some floating point values. Population will always be in whole numbers, so, its a wise idea to handle in integers. I didn't did this as if this number in thousands or milliions, it can also be floating point.
So, the modified code which works is:
import java.util.Scanner;
public class PopulationRecur {
public static void main(String[] args) {
int numDays;
int days;
int organism;
int increase;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the number of organisms: ");
organism = keyboard.nextInt();
while (organism < 2) {
System.out.print("The starting population must me 2 or higher: ");
organism = keyboard.nextInt();
}
System.out.print("Enter the daily increase: ");
increase = keyboard.nextInt();
while (increase < 0) {
System.out.print("The daily increase must be a positive number: ");
increase = keyboard.nextInt();
}
System.out.print("Enter the number of days the organisms will multiply:");
days = keyboard.nextInt();
while (days < 1) {
System.out.print("The number of days must be 1 or greater: ");
days = keyboard.nextInt();
}
numDays = 0;
showPopulation(numDays,days,organism,increase);
}
private static double showPopulation(int dayNum, int days, double organisms, double dailyIncrease) {
if (dayNum == days) {
return organisms;
} else {
}
System.out.println("Population for the end of day" + (dayNum + 1) + " is " + (organisms + organisms * (dailyIncrease / 100)));
return showPopulation(dayNum + 1, days, organisms + organisms * (dailyIncrease / 100), dailyIncrease);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.