{"id":56,"date":"2010-06-02T14:37:39","date_gmt":"2010-06-02T04:37:39","guid":{"rendered":"https:\/\/learn-java-by-example.com\/?p=56"},"modified":"2015-11-23T13:01:34","modified_gmt":"2015-11-23T02:01:34","slug":"monthly-payment-calculator","status":"publish","type":"post","link":"https:\/\/learn-java-by-example.com\/java\/monthly-payment-calculator\/","title":{"rendered":"Monthly Payment Calculator"},"content":{"rendered":"
\n

”<\/span> The fixed monthly payment for a fixed rate mortgage is the amount paid by the borrower every month that ensures that the loan is paid off in full with interest at the end of its term. The monthly payment formula is based on the annuity formula. “<\/span><\/p>\n

Monthly Payment Formula, Wikipedia<\/a><\/cite><\/p><\/blockquote>\n

Write a Java program without a graphical user interface that calculates and displays the mortgage payment amount given the amount of the mortgage, the term of the mortgage, and the interest rate of the mortgage.<\/p>\n

This is the simplest form of one of the classic problems given to first year Java students. It aims to get you comfortable with the structure of a simple Java application and how to get input from the user.<\/p>\n

User input in this example is taken from the console (standard input). The Scanner class comes with a number of methods for handling reading various types of input from the user. In this case we use the readInt() method to read an integer from the input, and readDouble() to read a floating point value.<\/p>\n

Once we have the required inputs it simply a case of applying the Monthly Payment Formula to get the monthly payment, and displaying the results to the user.<\/p>\n

<\/div>\n
\r\nimport java.text.NumberFormat;\r\nimport java.util.Scanner;\r\n\r\npublic class MortgagePaymentCalculator {\r\n\r\n\tpublic static double calculateMonthlyPayment(\r\n\t\tint loanAmount, int termInYears, double interestRate) {\r\n\t\t\r\n\t\t\/\/ Convert interest rate into a decimal\r\n\t\t\/\/ eg. 6.5% = 0.065\r\n\t\t\r\n\t\tinterestRate \/= 100.0;\r\n\t\t\r\n\t\t\/\/ Monthly interest rate \r\n\t\t\/\/ is the yearly rate divided by 12\r\n\t\t\r\n\t\tdouble monthlyRate = interestRate \/ 12.0;\r\n\t\t\r\n\t\t\/\/ The length of the term in months \r\n\t\t\/\/ is the number of years times 12\r\n\t\t\r\n\t\tint termInMonths = termInYears * 12;\r\n\t\t\r\n\t\t\/\/ Calculate the monthly payment\r\n\t\t\/\/ Typically this formula is provided so \r\n\t\t\/\/ we won't go into the details\r\n\t\t\r\n\t\t\/\/ The Math.pow() method is used calculate values raised to a power\r\n\t\t\r\n\t\tdouble monthlyPayment = \r\n\t\t\t(loanAmount*monthlyRate) \/ \r\n\t\t\t\t(1-Math.pow(1+monthlyRate, -termInMonths));\r\n\t\t\r\n\t\treturn monthlyPayment;\r\n\t}\r\n\t\r\n\tpublic static void main(String[] args) {\r\n\t\t\r\n\t\t\/\/ Scanner is a great class for getting \r\n\t\t\/\/ console input from the user\r\n\t\t\r\n\t\tScanner scanner = new Scanner(System.in);\r\n\r\n\t\t\/\/ Prompt user for details of loan\r\n\t\t\r\n\t\tSystem.out.print("Enter loan amount: ");\r\n\t\tint loanAmount = scanner.nextInt();\r\n\t\t\r\n\t\tSystem.out.print("Enter loan term (in years): ");\r\n\t\tint termInYears = scanner.nextInt();\r\n\t\t\r\n\t\tSystem.out.print("Enter interest rate: ");\r\n\t\tdouble interestRate = scanner.nextDouble();\r\n\t\t\r\n\t\t\/\/ Display details of loan\r\n\t\t\r\n\t\tdouble monthlyPayment = \r\n\t\t\tcalculateMonthlyPayment(loanAmount, termInYears, interestRate);\r\n\t\t\r\n\r\n\t\t\/\/ NumberFormat is useful for formatting numbers\r\n\t\t\/\/ In our case we'll use it for \r\n\t\t\/\/ formatting currency and percentage values\r\n\t\t\r\n\t\tNumberFormat currencyFormat = \r\n\t\t\tNumberFormat.getCurrencyInstance();\r\n\t\tNumberFormat interestFormat = \r\n\t\t\tNumberFormat.getPercentInstance();\r\n\r\n\t\t\/\/ Display details of the loan\r\n\r\n\t\tSystem.out.println("Loan Amount: "+\r\n\t\t\tcurrencyFormat.format(loanAmount));\r\n\t\tSystem.out.println("Loan Term: "+\r\n\t\t\ttermInYears+" years");\r\n\t\tSystem.out.println("Interest Rate: "+\r\n\t\t\tinterestFormat.format(interestRate));\r\n\t\tSystem.out.println("Monthly Payment: "+\r\n\t\t\tcurrencyFormat.format(monthlyPayment));\r\n\r\n\t}\r\n\r\n}\r\n<\/pre>\n

There are many extensions and variations to this problem which we shall cover in future examples.<\/p>\n

One extension is to calculate the payments over the term of the loan (amortisation table). If you’re interested in how to solve this problem then check out our Simple Mortgage Calculator<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"

This is the simplest form of one of the classic problems given to first year Java students. It aims to get you comfortable with the structure of a simple Java application and how to get input from the user. User input in this example is taken from the console (standard input). The Scanner class comes…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_options":[]},"categories":[4],"tags":[9,10,8,12,11],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p6Yyl2-U","jetpack-related-posts":[{"id":96,"url":"https:\/\/learn-java-by-example.com\/java\/simple-mortgage-calculator\/","url_meta":{"origin":56,"position":0},"title":"Simple Mortgage Calculator","date":"June 5, 2010","format":false,"excerpt":"This example expands on the Monthly Payment Calculator example we posted earlier. As well as calculating the monthly payment for a loan, it then goes on to use that to calculate the balance of the loan after each payment. This is often referred to as an amortisation schedule. The displayMonthlyBalance()\u2026","rel":"","context":"In "Featured"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":339,"url":"https:\/\/learn-java-by-example.com\/java\/scanner-read-words-text-file\/","url_meta":{"origin":56,"position":1},"title":"Using Scanner to read words from text file","date":"November 20, 2014","format":false,"excerpt":"Often you need to read a file line by line. Alternatively sometimes you want to read text word by word (for example to count the occurrence of different words). The Scanner classes next() method can be used for this as shown in the following example. You can find an example\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":98,"url":"https:\/\/learn-java-by-example.com\/java\/calculator-keypad\/","url_meta":{"origin":56,"position":2},"title":"Calculator Keypad","date":"August 14, 2010","format":false,"excerpt":"This problem involves understanding some of the basics of building a Swing application including how individual components are laid out inside a window, and how your application can react to the user interacting with the application. In this case we need to update a text field whenever a button is\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":175,"url":"https:\/\/learn-java-by-example.com\/java\/money-change-breakdown\/","url_meta":{"origin":56,"position":3},"title":"Money Change Breakdown","date":"March 28, 2011","format":false,"excerpt":"What is the smallest number of coins (or notes) that are required to give a specified amount of change? This is the next problem we will address. To solve this problem we use basically the same process a shop keeper would when giving change to customers. That is we look\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":199,"url":"https:\/\/learn-java-by-example.com\/java\/pythagoras-theorem\/","url_meta":{"origin":56,"position":4},"title":"Pythagoras Theorem","date":"December 13, 2010","format":false,"excerpt":"One of our member students was asked to implement the Pythagoras Theorem using Java. This is a good opportunity to introduce the Math class which contains a collection of static methods for various mathematical functions. We need the square root method (sort) to calculate the hypotenuse, and can also use\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":133,"url":"https:\/\/learn-java-by-example.com\/java\/fibonnaci-number\/","url_meta":{"origin":56,"position":5},"title":"Fibonnaci Number","date":"December 3, 2010","format":false,"excerpt":"Generating Fibonacci numbers is an assignment often given to Java students when they are being introduced to recursion. Recursion is programming technique that involves a method calling itself to solve a problem. When implementing a recursive solution we look for two things: A base case that returns a value without\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts\/56"}],"collection":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/comments?post=56"}],"version-history":[{"count":16,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts\/56\/revisions"}],"predecessor-version":[{"id":362,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts\/56\/revisions\/362"}],"wp:attachment":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/media?parent=56"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/categories?post=56"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/tags?post=56"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}