Question: Based on the following assumptions, write a program to display the population for each of the next five years. Assume the current population is 312,032,486 and one year has 365 days.
The U.S. Census Bureau projects population based on the following assumptions:
– One birth every 7 seconds
– One death every 13 seconds
– One new immigrant every 45 seconds
Warning!
Please import selected Java packages or use import java.util.*;. Do not copy my code as per copyright and plagiarism regulations. This Java code is provided as it is for demonstration purposes.Solution 1: Without using loops.
public class USPopulation { public static void main (String [] args) { //Store current population double intPop = 312032486; // Store 365 days in seconds double SecondsInYear = 31536000; //Number of births per year double birthsPerYear = SecondsInYear / 7; //Number of deaths per year double deathsPerYear = SecondsInYear / 13; //Immigration per year double immigrantsPerYear = SecondsInYear / 45; //Rate of population change per year double changePerYear = birthsPerYear - deathsPerYear + immigrantsPerYear; System.out.println("Population after one year : " + (double)(intPop + (1 * changePerYear))); System.out.println("Population after two years: " + (double)(intPop + (2 * changePerYear))); System.out.println("Population after three years: " + (double)(intPop + (3 * changePerYear))); System.out.println("Population after four years: " + (double)(intPop + (4 * changePerYear))); System.out.println("Population after five years: " + (double)(intPop + (5 * changePerYear))); } }
Solution 2: With using a for loop and an array containing terms “first”, “second”, “third”, “fourth” and “fifth”.
public class USPopulation { public static void main (String [] args) { //Store current population double intPop = 312032486; // Store 365 days in seconds double SecondsInYear = 31536000; //Number of births per year double birthsPerYear = SecondsInYear / 7; //Number of deaths per year double deathsPerYear = SecondsInYear / 13; //Immigration per year double immigrantsPerYear = SecondsInYear / 45; //Rate of population change per year double changePerYear = birthsPerYear - deathsPerYear + immigrantsPerYear; //For loop for five years String[] yearCount = {"first", "second", "third", "fourth", "fifth"}; for (int d=0;d<=4;d++){ System.out.print("Population after " + yearCount[d] + " years: "); System.out.println(intPop + ((d+1) * changePerYear)); } } }
Output:
Population after one year: 3.148125827032967E8
Population after two years: 3.1759267940659344E8
Population after three years: 3.2037277610989016E8
Population after four years: 3.2315287281318676E8
Population after five years: 3.259329695164835E8
Please note the public class USPopulation and variable names are arbitrary. This is to be expected in any programming language.