• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

World Population Growth

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I always struggle with the logic . Here is a problem I am working on:

Write a program that calculates world population growth each year for the next 75 years, using the simplifying  assumption that the current growth will stay constant. Print the results in a table. The first column should display the year from  year 1 to year 75. The second column should display the anticipated world  population at the end of that year. The third column should display the numerical increase in the world population that would occur that year. Using the results, determine the year in which the population would be double what it is today, if this growth rate were to persist.

Is this correct?

Note: The table just now is rudimentary and prints everything that the problem doesn't ask for.




 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A couple of points:
MainIsPain <-- That's a link.
QuoteYourSources <-- That's a link.

Your solution is still incomplete, the problem statement also asks you to determine the year in which it doubles. What's your planned solution for that ?
 
salvin francis
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am also confused over the growth rate. Instead of being a simple multiplier, I would expect it to be a percent.

This is what you are doing:
population = population * growthRate

(Because your growthRate is 2.0, your population is doubled at the second iteration itself, that should be the expected answer to your problem statement)

This is what I think it should be (eg 10% growth rate):
population = population + (10% * population)


Problem statement does not ask about currentYear, yet you are tracking and iterating it with every loop. Even if you need it for printing, since it's a derived value and is dependent on loop iteration, there is no need to calculate it separately.
i.e. if you have a variable called startYear=2019, currentYear = years + (startYear - 1)  since years starts with 1.
 
Marshal
Posts: 79239
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . and welcome to the Ranch
 
em jan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks well I at least like what the current year looks like in the output. I still don't think I am doing the right thing for the population computation though. And you are right:  I somehow thought that I could write an if and magically the year of double population would show up .

I have moved the bulk of the code from main.  The main method will eventually have a while loop that will allow the user to compute population growth for different inputs.

I will share the source from where the problem is from. when I find a soft copy of it online. This is from a very old dietal How to Program text book.


 
Saloon Keeper
Posts: 7590
177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

salvin francis wrote:This is what you are doing:
population = population * growthRate

This is what I think it should be (eg 10% growth rate):
population = population + (10% * population)


Both approaches are fundamentally the same, though, if the growth rate is 1.1. It's more a matter of how explicit the growth rate should be stated in the code.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Avoid the keyword static as much as possible. That means (see the links in Salvin's first post) you need to get as much code out of the main method as possible:-If you do it that way you will need two classes, which makes that a full‑blown application. Much more object oriented (=OO), too.
I would prefer to use double arithmetic because there is no such thing as part of a person. You can do all that with integer arithmetic:-...but make sure that i is always less than Long.MAX_VALUE ÷ (100 + x) to avoid overflow errors. Also you are stuck with whole number percentages. You can mimic 2.5% growth by entering 25 and writing 1000 rather than 100, but that is awkward isn't it.
Your prediction method is too long. It isn't doing one thing; a method should do one thing rather than trying to do multiple things. You want methods to do the calculations, to find when you get to double current population, etc. The OO way to do that might be to record future populations (maybe an array of Population objects) and the other methods can query this record.
 
reply
    Bookmark Topic Watch Topic
  • New Topic