#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Location
    South Florida
    Posts
    3
    Rep Power
    0

    Population Projection


    Hello All,

    I have been asked to make a program that will calculate a the population over the next 5 years.

    I have been trying to get this but it seems I am off by one each year as the calculation goes.

    Can someone point me in the right direction of what I am doing wrong? I tried to adjust the datatype but was not able to get a better answer.


    My answer I am getting is
    314812582
    317592678
    320372774
    323152870
    325932966


    Code:
    public class Population 
     { public static void main(String[] args) 
         { 
            int currentPopulation = 312032486; 
            int secondsInYear = 60 * 60 * 24 * 365; 
            int birthsPerYear = secondsInYear / 7;               
            int deathsPerYear = secondsInYear / 13; 
            int immigrantPerYear = secondsInYear / 45; 
            int perYearAdditions = birthsPerYear - deathsPerYear + immigrantPerYear; 
            int yearOnePopulation = currentPopulation + perYearAdditions; 
    
    System.out.println(yearOnePopulation);  
    System.out.println(yearOnePopulation + perYearAdditions); 
    System.out.println(yearOnePopulation + (perYearAdditions * 2)); 
    System.out.println(yearOnePopulation + (perYearAdditions * 3)); 
    System.out.println(yearOnePopulation + (perYearAdditions * 4)); 
    } 
    }
    thanks

    Jerry
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Can you add some comments to the program's output and say what the output should be?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Location
    South Florida
    Posts
    3
    Rep Power
    0
    Originally Posted by NormR
    Can you add some comments to the program's output and say what the output should be?
    The programs results should match the following outputs:
    314812582
    317592679
    320372776
    323152873
    325932970


    I seem to be off by one per year but I dont see why.

    I also looked at rewriting without variables
    Code:
    System.out.println(((60 * 60 * 24 * 365) / 7) - ((60 * 60 * 24 * 365) / 13) + ((60 * 60 * 24 * 365) / 45) + 312032486);
    this would output the first year but I would still need to complete this for the following 4 years.
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    You make it hard to compare the program's output to the desired output by not putting them close to each other.

    For easier testing start with a simple starting population like 10000.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Location
    South Florida
    Posts
    3
    Rep Power
    0
    I was able to find my errors. It was suggested to me to do it with out variables to remove the datatype issues.

    Code:
    public class Population { 
    public static void main(String[] args) { 
    System.out.println(((60 * 60 * 24 * 365) / 7) - ((60 * 60 * 24 * 365) / 13) + ((60 * 60 * 24 * 365) / 45) + 312032486); 
    System.out.println((2 *(60 * 60 * 24 * 365) / 7) - 2 *((60 * 60 * 24 * 365) / 13) + 2 *((60 * 60 * 24 * 365) / 45) + 312032486); 
    System.out.println((3 *(60 * 60 * 24 * 365) / 7) - 3 *((60 * 60 * 24 * 365) / 13) + 3 *((60 * 60 * 24 * 365) / 45) + 312032486); 
    System.out.println((4 *(60 * 60 * 24 * 365) / 7) - 4 *((60 * 60 * 24 * 365) / 13) + 4 *((60 * 60 * 24 * 365) / 45) + 312032486); 
    System.out.println((5 *(60 * 60 * 24 * 365) / 7) - 5 *((60 * 60 * 24 * 365) / 13) + 5 *((60 * 60 * 24 * 365) / 45) + 312032486); 
    } 
    }
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Makes for ugly code.

IMN logo majestic logo threadwatch logo seochat tools logo