Results 1 to 4 of 4
  1. #1
    wexgal is offline Member
    Join Date
    Oct 2009
    Posts
    1
    Rep Power
    0

    Default 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.

    Can anyone help me, please

    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

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    10

    Default

    How would you get the answer mathematically? Write down those steps.
    Then convert the steps to a Java program.

  3. #3
    Join Date
    Aug 2009
    Posts
    1
    Rep Power
    0

    Default

    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);
    }
    }

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,311
    Blog Entries
    7
    Rep Power
    24

    Default

    Quote Originally Posted by suganya_prasad View Post
    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

Similar Threads

  1. Java Problem. Need Help!
    By bob101 in forum New To Java
    Replies: 6
    Last Post: 03-19-2009, 05:34 AM
  2. JAVA and XML Problem
    By jackchang in forum XML
    Replies: 4
    Last Post: 02-22-2009, 09:28 PM
  3. Java problem
    By grend in forum New To Java
    Replies: 5
    Last Post: 08-18-2008, 11:44 PM
  4. Problem in java
    By saytri in forum New To Java
    Replies: 6
    Last Post: 01-09-2008, 05:13 PM
  5. java SE 6 problem
    By techlance in forum Java Applets
    Replies: 1
    Last Post: 06-28-2007, 10:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •