The program below is the answer to Liang’s Introduction to Java Programming (9th Edition) Chapter 2 Exercise 2.11.
Question: Rewrite Exercise 1.11 to prompt the user to enter the number of years and displays the population after the number of years.
1 2 |
/**<br /> *<br /> * @Author: Aghatise Osazuwa<br /> * Website: www.cscprogrammingtutorials.com<br /> *<br /> * Exercise 2.11 - Population Projection<br /> *<br /> */ <br /><br />import java.util.Scanner;<br /><br />public class CalculatingEnergy {<br /><br /> public static void main(String[] args) {<br /> <br /> //Display Program Information<br /> System.out.println("This Program Calculates The Population In The "<br /> + "Specified Number Of Years' Time.n");<br /> System.out.println("Current population is assumed to be 312,032,486.n");<br /><br /> //create Scanner <br /> Scanner input = new Scanner(System.in);<br /><br /> //prompt user to enter details<br /> System.out.println("Enter the number of years:");<br /> int years = input.nextInt();<br /><br /> int currentPopulation = 312032486;<br /> int births = 7;<br /> int deaths = 13;<br /> int immigrants = 45;<br /><br /> //calculate yearly births, yearly deaths and yearly immigrants<br /> int yearlyBirths = (60 * 60 * 24 * 365) / births;<br /> int yearlyDeaths = (60 * 60 * 24 * 365) / deaths;<br /> int yearlyImmigrants = (60 * 60 * 24 * 365) / immigrants;<br /><br /> //calculate yearly population<br /> int yearlyPopulation = (yearlyBirths + yearlyImmigrants) - yearlyDeaths;<br /><br /> //calculate population in the specified number of years<br /> int population = currentPopulation + (yearlyPopulation * years);<br /><br /> //display the result<br /> System.out.println("The population in " + years + " years is " + population + ".n");<br /> }<br />}<br /> |
Click here to see other solutions to Introduction to Java Programming.