{"id":11607,"date":"2020-09-30T06:40:38","date_gmt":"2020-09-30T12:40:38","guid":{"rendered":"https:\/\/sanuja.com\/blog\/?p=11607"},"modified":"2020-09-30T06:44:44","modified_gmt":"2020-09-30T12:44:44","slug":"java-financial-application-calculate-future-investment-value","status":"publish","type":"post","link":"https:\/\/sanuja.com\/blog\/java-financial-application-calculate-future-investment-value","title":{"rendered":"Java Financial Application: calculate future investment value"},"content":{"rendered":"<p><strong>Question:<\/strong> Write a program that reads in investment amount, annual interest rate, and number of years, and displays the future investment value using the following formula:<br \/>\n<i>futureInvestmentValue = investmentAmount * (1 + monthlyInterestRate)^(numberOfYears * 12)<\/i><\/p>\n<p><!--more--><br \/>\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><\/p>\n<p><strong>Solution:<\/strong> Note the two possible methods of printing the output.<br \/>\n1) Display result by division method: In Java, if you divide a value with a decimal place, you can obtain an output with a decimal value. This can be used in place of a <em>printf()<\/em> function.<\/p>\n<p>2) Display result using printf method: The <em>printf()<\/em> function allow you to format output. It should in the following order: <em>%[flags][width][.precision]conversion-character<\/em>. You can learn more about Java <em>printf()<\/em> on <a href=\"https:\/\/docs.oracle.com\/javase\/tutorial\/java\/data\/numberformat.html\" target=\"_blank\" rel=\"noopener noreferrer\">Oracle Java docs<\/a>. <\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class futureInvestment {\r\n\tpublic static void main(String&#x5B;] args) {\r\n\t\tScanner input = new Scanner(System.in);\r\n\r\n\t\t\/\/ Prompt the user to enter the investment amount,\r\n\t\t\/\/ annual interest rate and number of years.\r\n\t\tSystem.out.print(&quot;Enter investment amount: &quot;);\r\n\t\tdouble amount = input.nextDouble();\r\n\t\tSystem.out.print(&quot;Enter annual interest rate in percentage: &quot;);\r\n\t\tdouble monthlyInterestRate = input.nextDouble();\r\n\t\tmonthlyInterestRate \/= 1200;\r\n\t\tSystem.out.print(&quot;Enter number of years: &quot;);\r\n\t\tint years = input.nextInt();\r\n\r\n\t\t\/\/ Calculate future investment value\r\n\t\tdouble futureInvestmentValue = \r\n\t\t\tamount * Math.pow(1 + monthlyInterestRate, years * 12);\r\n\t\t\r\n\t\t\/\/ 1) Display result by division method\r\n\t\tSystem.out.println(&quot;Future value is $&quot; + (int)(futureInvestmentValue * 100) \/ 100.0);\r\n\t\t\/\/ 2) Display result using printf method\r\n\t\tSystem.out.printf(&quot;Future value is $%.2f&quot;, futureInvestmentValue);\t\t\r\n\t}\r\n}\r\n<\/pre>\n<p>I would recommend that you use <em>printf()<\/em> method and get familiar with the format specifiers.<\/p>\n<p><strong>Output:<\/strong><br \/>For example, if you enter amount 1000, annual interest rate 3.25%, and number of years 1, the future investment value is 1032.98<\/p>\n<p>Enter investment amount: 1000.56<br \/>\nEnter annual interest rate in percentage: 4.25<br \/>\nEnter number of years: 1<br \/>\nFuture value is $1043.92<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Question: Write a program that reads in investment amount, annual interest rate, and number of years, and displays the future investment value using the following formula: futureInvestmentValue = investmentAmount * (1 + monthlyInterestRate)^(numberOfYears * 12)<\/p>\n","protected":false},"author":2,"featured_media":5055,"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-11607","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\/04\/vba_script.jpg","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/posts\/11607","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=11607"}],"version-history":[{"count":0,"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/posts\/11607\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/media\/5055"}],"wp:attachment":[{"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/media?parent=11607"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/categories?post=11607"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/tags?post=11607"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}