{"id":11563,"date":"2020-09-11T20:42:33","date_gmt":"2020-09-12T02:42:33","guid":{"rendered":"https:\/\/sanuja.com\/blog\/?p=11563"},"modified":"2020-09-20T12:10:28","modified_gmt":"2020-09-20T18:10:28","slug":"simple-arithmetic-calculation-using-java","status":"publish","type":"post","link":"https:\/\/sanuja.com\/blog\/simple-arithmetic-calculation-using-java","title":{"rendered":"Simple arithmetic calculation using Java"},"content":{"rendered":"<p><strong>Question:<\/strong> Using Java, write a program to calculate available credit. The program must prompt the user for their credit limit and the amount owing. Based on the user input, print the available credit limit.<\/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 1:<\/strong> Can you determine any improvements without looking at the Solution 2? <\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class AvailableCredit {\r\n    public static void main (String &#x5B;] args) {\r\n        \/\/ Create a Scanner object attached to the keyboard\r\n        Scanner in = new Scanner (System.in);\r\n         \r\n        \/\/ Reading limit\r\n        System.out.print(&quot;Enter the limit: $&quot;);\r\n        int limit = in.nextInt();\r\n\r\n        \/\/ Reading the owing\r\n        System.out.print(&quot;Enter the amount owing: $&quot;);\r\n        int owing = in.nextInt();\r\n\r\n        \/\/ Available credit limit\r\n        int available = limit - owing;\r\n        System.out.print(&quot;The available credit = $&quot; + available);\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Solution 2:<\/strong> This is a more refined solution that achieve the same results as the Solution 1. The variables <em>limit, owing<\/em> and <em>available<\/em> are defined at the top eliminating the need to define the variables as we go down the code. The integer variable <em>int<\/em> is replaced by <em>double<\/em>, which will allow inputs with decimal places (eg. 940.5, 119.15, etc).<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class AvailableCredit {\r\n    public static void main (String &#x5B;] args) {\r\n        \/\/ Declare variable \r\n        double limit, owing, available;\r\n        Scanner m = new Scanner (System.in);   \r\n        \r\n        \/\/ Requesting credit limit\r\n        System.out.print(&quot;Enter the limit: $&quot;);        \r\n        limit = m.nextDouble();\r\n        \r\n        \/\/ Requesting the owing\r\n        System.out.print(&quot;Enter the amount owing: $&quot;);\r\n        owing = m.nextDouble();\r\n        \r\n        \/\/ Available credit limit\r\n        available = limit - owing;\r\n        System.out.print(&quot;The available credit = $&quot; + available);\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Output:<\/strong><br \/>\nEnter the limit: $<em>55.5<\/em><br \/>\nEnter the amount owing: $<em>995.5<\/em><br \/>\nThe available credit = $-940.0<br \/>\nUser inputs are in <em>italics<\/em> are user entered values.<\/p>\n<p>The public class <em>AvailableCredit<\/em> and variable names are arbitrary. This is to be expected in any programming language.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Question: Using Java, write a program to calculate available credit. The program must prompt the user for their credit limit and the amount owing. Based on the user input, print the available credit limit.<\/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":[48],"class_list":["post-11563","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-computer-science","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\/11563","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=11563"}],"version-history":[{"count":0,"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/posts\/11563\/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=11563"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/categories?post=11563"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/tags?post=11563"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}