{"id":11572,"date":"2020-09-20T09:13:30","date_gmt":"2020-09-20T15:13:30","guid":{"rendered":"https:\/\/sanuja.com\/blog\/?p=11572"},"modified":"2020-09-28T09:35:54","modified_gmt":"2020-09-28T15:35:54","slug":"projecting-population-in-java","status":"publish","type":"post","link":"https:\/\/sanuja.com\/blog\/projecting-population-in-java","title":{"rendered":"Projecting Population in Java"},"content":{"rendered":"<p><strong>Question:<\/strong> 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.<\/p>\n<p>The U.S. Census Bureau projects population based on the following assumptions:<br \/><!--more--><br \/>\n&#8211; One birth every 7 seconds<br \/>\n&#8211; One death every 13 seconds<br \/>\n&#8211; One new immigrant every 45 seconds<\/p>\n<div class=\"alert alert-warning\" role=\"alert\"><p class=\"printonly\"><strong>Warning!<\/strong><\/p> Please import selected Java packages or use <em>import java.util.*;<\/em>. Do not copy my code as per copyright and plagiarism regulations. This Java code is provided as it is for demonstration purposes.<\/div>\n<p><strong>Solution 1:<\/strong> Without using loops.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class USPopulation {\r\n    public static void main (String &#x5B;] args) {\r\n        \/\/Store current population\r\n        double intPop = 312032486;\r\n\r\n        \/\/ Store 365 days in seconds\r\n        double SecondsInYear = 31536000;\r\n\r\n        \/\/Number of births per year\r\n        double birthsPerYear = SecondsInYear \/ 7;\r\n        \r\n        \/\/Number of deaths per year\r\n        double deathsPerYear = SecondsInYear \/ 13;\r\n        \r\n        \/\/Immigration per year\r\n        double immigrantsPerYear = SecondsInYear \/ 45;\r\n        \r\n        \/\/Rate of population change per year\r\n        double changePerYear = birthsPerYear - deathsPerYear + immigrantsPerYear;\r\n        \r\n        System.out.println(&quot;Population after one year : &quot; + (double)(intPop + (1 * changePerYear)));\r\n        System.out.println(&quot;Population after two years:  &quot; + (double)(intPop + (2 * changePerYear)));\r\n        System.out.println(&quot;Population after three years:  &quot; + (double)(intPop + (3 * changePerYear)));\r\n        System.out.println(&quot;Population after four years:  &quot; + (double)(intPop + (4 * changePerYear)));\r\n        System.out.println(&quot;Population after five years:  &quot; + (double)(intPop + (5 * changePerYear)));\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Solution 2:<\/strong> With using a <em>for loop<\/em> and an <em>array<\/em> containing terms &#8220;first&#8221;, &#8220;second&#8221;, &#8220;third&#8221;, &#8220;fourth&#8221; and &#8220;fifth&#8221;.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class USPopulation {\r\n    public static void main (String &#x5B;] args) {\r\n        \/\/Store current population\r\n        double intPop = 312032486;\r\n\r\n        \/\/ Store 365 days in seconds\r\n        double SecondsInYear = 31536000;\r\n\r\n        \/\/Number of births per year\r\n        double birthsPerYear = SecondsInYear \/ 7;\r\n        \r\n        \/\/Number of deaths per year\r\n        double deathsPerYear = SecondsInYear \/ 13;\r\n        \r\n        \/\/Immigration per year\r\n        double immigrantsPerYear = SecondsInYear \/ 45;\r\n        \r\n        \/\/Rate of population change per year\r\n        double changePerYear = birthsPerYear - deathsPerYear + immigrantsPerYear;\r\n       \r\n        \/\/For loop for five years\r\n        String&#x5B;] yearCount = {&quot;first&quot;, &quot;second&quot;, &quot;third&quot;, &quot;fourth&quot;, &quot;fifth&quot;};\r\n            for (int d=0;d&lt;=4;d++){\r\n                System.out.print(&quot;Population after &quot; + yearCount&#x5B;d] + &quot; years: &quot;);\r\n                System.out.println(intPop + ((d+1) * changePerYear));\r\n            }  \r\n    }\r\n}\r\n<\/pre>\n<p><strong>Output:<\/strong><br \/>\nPopulation after one year: 3.148125827032967E8<br \/>Population after two years: 3.1759267940659344E8<br \/>Population after three years: 3.2037277610989016E8<br \/>Population after four years: 3.2315287281318676E8<br \/>Population after five years: 3.259329695164835E8<\/p>\n<p>Please note the public class <em>USPopulation<\/em> and variable names are arbitrary. This is to be expected in any programming language.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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:<\/p>\n","protected":false},"author":2,"featured_media":5871,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3],"tags":[18,48],"class_list":["post-11572","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-computer-science","tag-computing","tag-java"],"jetpack_featured_media_url":"https:\/\/sanuja.com\/blog\/wp-content\/uploads\/2013\/05\/code_black.jpg","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/posts\/11572","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/comments?post=11572"}],"version-history":[{"count":0,"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/posts\/11572\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/media\/5871"}],"wp:attachment":[{"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/media?parent=11572"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/categories?post=11572"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/tags?post=11572"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}