Tuesday, September 27, 2016

// // Leave a Comment

Exercise 2.11 Solution - Population Projection

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.

/**
 *
 * @Author: Aghatise Osazuwa
 * Website: www.cscprogrammingtutorials.com
 *
 * Exercise 2.11 - Population Projection
 *
 */ 

import java.util.Scanner;

public class CalculatingEnergy {

    public static void main(String[] args) {
        
        //Display Program Information
        System.out.println("This Program Calculates The Population In The "
                + "Specified Number Of Years' Time.\n");
        System.out.println("Current population is assumed to be 312,032,486.\n");

        //create Scanner 
        Scanner input = new Scanner(System.in);

        //prompt user to enter details
        System.out.println("Enter the number of years:");
        int years = input.nextInt();

        int currentPopulation = 312032486;
        int births = 7;
        int deaths = 13;
        int immigrants = 45;

        //calculate yearly births, yearly deaths and yearly immigrants
        int yearlyBirths = (60 * 60 * 24 * 365) / births;
        int yearlyDeaths = (60 * 60 * 24 * 365) / deaths;
        int yearlyImmigrants = (60 * 60 * 24 * 365) / immigrants;

        //calculate yearly population
        int yearlyPopulation = (yearlyBirths + yearlyImmigrants) - yearlyDeaths;

        //calculate population in the specified number of years
        int population = currentPopulation + (yearlyPopulation * years);

        //display the result
        System.out.println("The population in " + years + " years is " + population + ".\n");
    }
}

Click here to see other solutions to Introduction to Java Programming.

0 comments:

Post a Comment


DISCLAIMER: Opinions expressed in comments are those of the comment writers alone and does not reflect or represent the views of the post author. We reserve the right to delete any post deemed inappropriate or offensive and/or spammy. Please do not use abusive words/hate speech.

I Appreciate your valuable Feedback. So, Please DO NOT SPAM - Spam comments will be deleted immediately.

Don't use brand name in name field and you're not allowed to use links in comments unless it's necessary. Such comments will be removed immediately.

Thanks.