{"id":5624,"date":"2022-08-29T19:32:02","date_gmt":"2022-08-29T14:02:02","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-java-issue-using-classes-and-accessing-variables\/"},"modified":"2022-08-29T19:32:02","modified_gmt":"2022-08-29T14:02:02","slug":"solved-java-issue-using-classes-and-accessing-variables","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-java-issue-using-classes-and-accessing-variables\/","title":{"rendered":"[Solved] Java issue using classes and accessing variables"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-42782345\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"42782345\" data-parentid=\"42779286\" data-score=\"0\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>Not sure, if this is what you want, but you would need to update your <code>total<\/code> within the loop to update the value in the <code>Total Compensation<\/code> column.<\/p>\n<p>Let&#8217;s start with the little things you should change first. The <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/www.oracle.com\/technetwork\/java\/codeconventions-135099.html\">naming convention<\/a> in Java is camel-case. You should change that accordingly. For example, rather than <code>getsalesDouble<\/code> you should name your function <code>getSalesDouble<\/code> &#8211; similar to your <code>numFormat<\/code>.<\/p>\n<p>Next, you should also change the names of your methods and parameters\/variables. In order to do that, I like to ask myself <em>what is this method going to do?<\/em>. Once I know that I&#8217;d come up with an appropriate verb for it and that&#8217;s my method&#8217;s name. For example, your <code>Calculator<\/code> should be named <code>calculate<\/code> in order to have a verb. But this wouldn&#8217;t be self-explanatory, thus, another name has to take its place &#8212; ask youself <em>what does it calculate?<\/em> (e.g. <code>calculateSalary<\/code>).<\/p>\n<p>Next, your <code>utilsCalculate<\/code> class should start with an uppercase letter (see link above). I&#8217;d prefer to name it <code>Utils<\/code>, since there is only one utils class anyway. Additionally, the class should have only static methods, so you do not need to create an instance (see code below). You might want to create a <em>private default constructor<\/em>, to avoid other programmers in your team accidentally creating an instance of your <code>Utils<\/code> class.<\/p>\n<p>Now, that being said, you should avoid commenting like you do. It only clutters your code. For example, the end of a method is indicated by <code>}<\/code>. So, there is no need to state that the end of the method is at the line where the <code>}<\/code> occurs. If you find it hard to see where the closing <code>}<\/code> of a method is, you might consider properly formatting your code. Usually your IDE can do that for you by pressing a shortcut or using the menu bar.<\/p>\n<p>Last but not least, I take it, your code works for you, but the <code>total<\/code> does not update (within your <code>while loop<\/code>). This is, because you only calculate the total <em>once<\/em> and never re-calculate it with the new value of <code>salesDouble<\/code>. You&#8217;ll have to call <code>total = salesObject.Calculator();<\/code> after updating the <code>salesDouble<\/code> value. Of course, this would require you to create a new <code>utilsCalculate<\/code> instance. Otherwise, the value would always be the same, as you would never update the <code>salesDouble<\/code> within your utils class. That is nothing I&#8217;d recommend you to do, but would solve your issue for the code as is.<\/p>\n<p>Have this example, on how to do it <em>properly<\/em>*.<\/p>\n<p><strong>Utils<\/strong><\/p>\n<pre><code>package utils;\n\nimport java.text.DecimalFormat;\n\npublic class Utils {\n    public final static double FIXED_SALARY = 30000;\n\n    \/**\n     * Formats the given decimal number.\n     * @param dec The number to format\n     * @return The formatted number as string.\n     *\/\n    public static String numFormat(double dec) {\n        return new DecimalFormat(\"##,##0.00\").format(dec);\n    }\n\n    \/**\n     * Calculates the salary based on the given sales.\n     * @param sales The annual sales\n     * @return The calculated salary.\n     *\/\n    public static double calculateSalary(double sales) {\n        double commissionRate = 0.10d;\n\n        if (sales &lt; 320000) {\n            commissionRate = 0.00d;\n        } else if (sales &gt;= 320000 &amp;&amp; sales &lt; 400000) {\n            commissionRate = 0.08d;\n        }\n\n        \/\/ System.out.println(\"The current commission is \" + (int)(commissionRate * 100) + \"% of total sales.\");\n\n        return FIXED_SALARY + (sales * commissionRate);\n    }\n}\n<\/code><\/pre>\n<p><strong>Main<\/strong><\/p>\n<pre><code>package sales;\n\nimport java.util.Scanner;\n\nimport utils.Utils;\n\npublic class Person {\n    public static void main(String[] args) {\n        \/\/ Initialise scanner\n        Scanner input = new Scanner(System.in);\n        System.out.print(\"Please enter your annual sales:\");\n\n        \/\/ Read sales from input &amp; calculate salary\n        double annualSales = input.nextDouble();\n        double salary = Utils.calculateSalary(annualSales);\n\n        \/\/ Calculate commission bonus\n        double commissionBonus = 1.5 * annualSales;\n\n        \/\/ Print information for user\n        System.out.println(\"The total yearly salary is: \" + Utils.numFormat(salary));\n        System.out.println(\"Total Sales \\t\\t Total Compensation\");\n\n        while (annualSales &lt;= commissionBonus) {\n            System.out.println(annualSales + \" \\t\\t \" + salary);\n            annualSales += 5000;\n\n            \/\/ Update salary according to annual sales\n            salary = Utils.calculateSalary(annualSales);\n        }\n\n        \/\/ Close scanner\n        input.close();\n    }\n}\n<\/code><\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre><code>Please enter your annual sales:320000\nThe total yearly salary is: 55.600,00\nTotal Sales          Total Compensation\n320000.0         55600.0\n325000.0         56000.0\n330000.0         56400.0\n335000.0         56800.0\n340000.0         57200.0\n345000.0         57600.0\n350000.0         58000.0\n355000.0         58400.0\n360000.0         58800.0\n365000.0         59200.0\n370000.0         59600.0\n375000.0         60000.0\n380000.0         60400.0\n385000.0         60800.0\n390000.0         61200.0\n395000.0         61600.0\n400000.0         70000.0\n405000.0         70500.0\n410000.0         71000.0\n415000.0         71500.0\n420000.0         72000.0\n425000.0         72500.0\n430000.0         73000.0\n435000.0         73500.0\n440000.0         74000.0\n445000.0         74500.0\n450000.0         75000.0\n455000.0         75500.0\n460000.0         76000.0\n465000.0         76500.0\n470000.0         77000.0\n475000.0         77500.0\n480000.0         78000.0\n<\/code><\/pre>\n<p>* Of course, there is still room for enhancement, but I think it should be sufficient for your problem.<\/p>\n<p><strong>Note<\/strong> that I did not check your equations, as I do not know how to calculate this &#8211; I assumed your calculations are correct.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">10<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Java issue using classes and accessing variables <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Not sure, if this is what you want, but you would need to update your total within the loop to update the value in the Total Compensation column. Let&#8217;s start with the little things you should change first. The naming convention in Java is camel-case. You should change that accordingly. For example, rather than &#8230; <a title=\"[Solved] Java issue using classes and accessing variables\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-java-issue-using-classes-and-accessing-variables\/\" aria-label=\"More on [Solved] Java issue using classes and accessing variables\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[519,323,419,459],"class_list":["post-5624","post","type-post","status-publish","format-standard","hentry","category-solved","tag-class","tag-java","tag-methods","tag-while-loop"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Java issue using classes and accessing variables - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-java-issue-using-classes-and-accessing-variables\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Java issue using classes and accessing variables - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Not sure, if this is what you want, but you would need to update your total within the loop to update the value in the Total Compensation column. Let&#8217;s start with the little things you should change first. The naming convention in Java is camel-case. You should change that accordingly. For example, rather than ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-java-issue-using-classes-and-accessing-variables\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-08-29T14:02:02+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-java-issue-using-classes-and-accessing-variables\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-java-issue-using-classes-and-accessing-variables\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Java issue using classes and accessing variables\",\"datePublished\":\"2022-08-29T14:02:02+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-java-issue-using-classes-and-accessing-variables\/\"},\"wordCount\":466,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"class\",\"java\",\"methods\",\"while-loop\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-java-issue-using-classes-and-accessing-variables\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-java-issue-using-classes-and-accessing-variables\/\",\"name\":\"[Solved] Java issue using classes and accessing variables - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-08-29T14:02:02+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-java-issue-using-classes-and-accessing-variables\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-java-issue-using-classes-and-accessing-variables\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-java-issue-using-classes-and-accessing-variables\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Java issue using classes and accessing variables\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Java issue using classes and accessing variables - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-java-issue-using-classes-and-accessing-variables\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Java issue using classes and accessing variables - JassWeb","og_description":"[ad_1] Not sure, if this is what you want, but you would need to update your total within the loop to update the value in the Total Compensation column. Let&#8217;s start with the little things you should change first. The naming convention in Java is camel-case. You should change that accordingly. For example, rather than ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-java-issue-using-classes-and-accessing-variables\/","og_site_name":"JassWeb","article_published_time":"2022-08-29T14:02:02+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-java-issue-using-classes-and-accessing-variables\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-java-issue-using-classes-and-accessing-variables\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Java issue using classes and accessing variables","datePublished":"2022-08-29T14:02:02+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-java-issue-using-classes-and-accessing-variables\/"},"wordCount":466,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["class","java","methods","while-loop"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-java-issue-using-classes-and-accessing-variables\/","url":"https:\/\/jassweb.com\/solved\/solved-java-issue-using-classes-and-accessing-variables\/","name":"[Solved] Java issue using classes and accessing variables - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-08-29T14:02:02+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-java-issue-using-classes-and-accessing-variables\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-java-issue-using-classes-and-accessing-variables\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-java-issue-using-classes-and-accessing-variables\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Java issue using classes and accessing variables"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/5624","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=5624"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/5624\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=5624"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=5624"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=5624"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}