# Java problem help!

• 10-22-2009, 01:46 PM
wexgal
Java problem help!
Ok so I am studying Java for the first time and am really stuck on the following program.

The population of a town is now at 15000 and it is expanding at 5% per year . Write a program to determine how many years it will take for the population to exceed 85000.

I understand mathematically how you get the answer but not in java.

Below is my lousy attempt :)

import java.util.*;
population
{

//The population of a town is now at 15000 and it is expanding at 5% per year . Write a program to determine how many years it will take for the population to exceed 85000.

public static void main(String[] args)

{
Scanner sc = new Scanner(System.in);
// variables

double population, year1, year2, yearn;
final DOUBLE ENDPOP = 85000;

// initialise loop control variable

while (population < ENDPOP)

{
// year 1 population increase

System.out.println("enter current population");
population = sc.nextDouble():

year1 = (population * 1.05);
year2 = (year1 * 1.05);
yearn = (year(n-1) * 1.05);

} // end while

System.out.println("Number of years for population to exceed 85000 is " + ?);
}// end main

}// end class
• 10-22-2009, 02:14 PM
r035198x
How would you get the answer mathematically? Write down those steps.
Then convert the steps to a Java program.
• 10-26-2009, 01:26 PM
class Population
{
public static void main(String args[])
{
double intialpopulation=15000D;
double finalpopulation=85000D;
double percentage=5;
double yearlyIncreaseinpoulation=(percentage/100)*intialpopulation;
System.out.println("yearlyIncreaseinpoulation"+yea rlyIncreaseinpoulation);
double noofyears=(finalpopulation-intialpopulation)/yearlyIncreaseinpoulation;
System.out.println(noofyears);
}
}
• 10-26-2009, 01:52 PM
JosAH
Quote:

class Population
{
public static void main(String args[])
{
double intialpopulation=15000D;
double finalpopulation=85000D;
double percentage=5;
double yearlyIncreaseinpoulation=(percentage/100)*intialpopulation;
System.out.println("yearlyIncreaseinpoulation"+yea rlyIncreaseinpoulation);
double noofyears=(finalpopulation-intialpopulation)/yearlyIncreaseinpoulation;
System.out.println(noofyears);
}
}

That most certainly isn't true: if you start with a population P(0) and the growth is i percent per year, then after n years the population P(n) is: (1+i/100)^n*P(0); if you have P(0) == 15000 and P(n) == 85000 then a bit of 101 math tells you: 85000 ==1.05^n*15000 --> 85000/15000= 1.05^n --> n == 35.5... little babies grow up and start making little babies too you know.

kind regards,

Jos