The program below is the answer to Deitel’s Java How to Program (9th Edition) Chapter 2 Exercise 2.34.
Click here to see other answers to Java How to Program.
Question: Use the web to determine the current world population and the annual world population growth rate. Write an application that inputs these values, then displays the estimated world population after one, two, three, four and five years.
1 2 |
/**<br /> *<br /> * @Author: Aghatise Osazuwa<br /> * Website: www.cscprogrammingtutorials.com<br /> *<br /> * Exercise 2.34 - World Population Growth Calculator<br /> * This Program Calculates And Displays The Estimated World Population After One, Two, Three, <br /> * Four And Five Years<br /> *<br /> */<br /><br />//Based on data from worldometer World Population clock (worldometers.info/world-population)<br />//Geohive (www.geohive.com/earth/population_now.aspx)<br />//US World Population Clock (www.census.gov/popclock)<br />//Current world population is 7,321,870,923 with an average yearly growth rate of 1.10% <br /><br />import java.util.Scanner;<br /><br />public class Ex02_34 {<br /> public static void main (String [] args) {<br /><br /> Scanner input = new Scanner (System.in);<br /> <br /> double currentPop; <br /> double growthRate;<br /> double annualPopIncrease;<br /> double estimatedPop;<br /> <br /> System.out.print ("Enter Current World Population: ");<br /> currentPop = input.nextLong();<br /> System.out.print ("Enter Annual Population Growth Rate: ");<br /> growthRate = input.nextInt();<br /> annualPopIncrease = (growthRate / 100) * currentPop;<br /> estimatedPop = currentPop + annualPopIncrease;<br /><br /> System.out.println ();<br /><br /> System.out.println("Estimated population after one year: " + estimatedPop);<br /> System.out.println("Estimated population after two years: " + estimatedPop + (annualPopIncrease * 2));<br /> System.out.println("Estimated population after three years: " + estimatedPop + (annualPopIncrease * 3));<br /> System.out.println("Estimated population after four years: " + estimatedPop + (annualPopIncrease * 4));<br /> System.out.println("Estimated population after five years: " + estimatedPop + (annualPopIncrease * 5));<br /><br /> }<br />}<br /> |
Click here to see other answers to Java How to Program.
8 Comments
I had to change input.nextLong() to input.nextDouble() and worked well. Otherwise thanks again
Thanks
I'm not sure. It is supposed to work. Check your value very well. Maybe you typed extra digits.
I tried double for currentPop but in Eclipse Neon.3 it tells me that value over 7 billion is out of range. Why?
Thank you so so much for the correction. Didn't think of it that way – logic error! It has been corrected.
P.S. I'm glad you find it helpful.
The workig data estimated pop * number of years is wron i Think as estimatedpop becomes after one year but multiplying estimatedpop by 2 for the population after two years is mathematical problem which staes that growth rate was 200%. Instead it should be estimatedpop + growth/100 * estimated pop to be precise but if it was just a simple calculation you need to multiply the annualpopincrease by 2 and add it to estimatedpop.
I do not mean to be rude and also THANKS A LOT FOR PROVIDING THE SOLUTIONS IT IS HELPING ME A LOT.
Thanks for pointing it out. It had input mismatch but it's been corrected and now works fine.
not working broda