Java Programming control structures:\ The population of Town A is less than the population of Town . However, the population of Town A is growing faster than the population of Town B. Write a program that prompts the user to enter the population and growth rate of each town. The program outputs after how many years the population of Town Aa will be greater than or equal to the population of Town B and the populations of both towns at that time. Sample input is: Population of Town A = 5,000, growth rate of town A =4%, populatio of Town B = 8,000, and growth rate of town B = 2%
Java Programming control structures:\
The population of Town A is less than the population of Town . However, the population of Town A is growing faster than the population of Town B. Write a program that prompts the user to enter the population and growth rate of each town. The program outputs after how many years the population of Town Aa will be greater than or equal to the population of Town B and the populations of both towns at that time. Sample input is: Population of Town A = 5,000, growth rate of town A =4%, populatio of Town B = 8,000, and growth rate of town B = 2%
Here's my code and something is not right
static Scanner console = new Scanner(System.in);
public static void main(String[] args) {
int populationA;
int populationB;
int projectedYears;
int futurePopulation;
double townAGrowthRate;
double townBGrowthRate;
System.out.print("Enter the population for Town A and Town B "
+ "(population of Town A should be less than Town B): \n");
populationA = console.nextInt();
populationB = console.nextInt();
System.out.print("Enter the growth rate for Town A and Town B "
+ "(growth rate of Town B should less Town A: \n");
townAGrowthRate = console.nextDouble();
townBGrowthRate = console.nextDouble();
System.out.print("Enter the number of years to calculate the future population growth: \n");
projectedYears = console.nextInt();
int countYears = 0;
//Calculate thw future population of own A and town B
if(populationA < populationB && townAGrowthRate > townBGrowthRate)
{
do{
populationA = (int) ((townAGrowthRate / 100) * populationA) + (populationA);
populationB = (int) ((townBGrowthRate / 100) * populationB) + (populationB);
countYears++;
// System.out.println(" Town A" + populationA);
//System.out.println("Town B" + populationB);
}while(countYears < projectedYears && populationA < populationB );
}
System.out.print("Town A's population will be greater than or equal to Town B's population "
+ " in " + countYears + " years \n");
System.out.print("The population of Town A in " + countYears + " "
+ " years will be: " + populationA + "\n");
System.out.print("The population of Town B in " + countYears + " "
+ "years will be: " + populationB + "\n");
//else{
// System.out.print("Invalid Entry \n");
//}
/*
* Town APopulaton = 5000
* Town A Growth Rate = .04%
* Town B Population = 8000
* Town B Growth Rate = .02%
*
* Enter the population for Town A and Town B (population of Town A should be less than Town B): 10 1000
* Enter the growth rate for Town A and Town B (growth rate of Town B should less Town A: 15 20.5
* Enter the projected number of years:
*
*
* Run on 10/23/19
*
* //Calculate thw future population of own A and town B
if(populationA < populationB && townAGrowthRate > townBGrowthRate)
{
do{
townAPopulation = (int) ((townAGrowthRate / 100) * populationA) + (populationA);
townBPopulation = (int) ((townBGrowthRate / 100) * populationB) + (populationB);
countYears++;
// System.out.println(" Town A" + populationA);
//System.out.println("Town B" + populationB);
}while(countYears < projectedYears && populationA < populationB );
*
*
* Enter the population for Town A and Town B (population of Town A should be less than Town B):
5000 8000
Enter the growth rate for Town A and Town B (growth rate of Town B should less Town A:
.04 .02
Enter the number of years to calculate the future population growth:
10
Town A's population will be greater than or equal to Town B's population in 10 years
The population of Town A in 10 years will be: 5002
The population of Town B in 10 years will be: 8001
*/
}
}
Thank you
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images