” 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. “
Monthly Payment Formula, Wikipedia
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.
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 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.
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.
import java.text.NumberFormat; import java.util.Scanner; public class MortgagePaymentCalculator { public static double calculateMonthlyPayment( int loanAmount, int termInYears, double interestRate) { // Convert interest rate into a decimal // eg. 6.5% = 0.065 interestRate /= 100.0; // Monthly interest rate // is the yearly rate divided by 12 double monthlyRate = interestRate / 12.0; // The length of the term in months // is the number of years times 12 int termInMonths = termInYears * 12; // Calculate the monthly payment // Typically this formula is provided so // we won't go into the details // The Math.pow() method is used calculate values raised to a power double monthlyPayment = (loanAmount*monthlyRate) / (1-Math.pow(1+monthlyRate, -termInMonths)); return monthlyPayment; } public static void main(String[] args) { // Scanner is a great class for getting // console input from the user Scanner scanner = new Scanner(System.in); // Prompt user for details of loan System.out.print("Enter loan amount: "); int loanAmount = scanner.nextInt(); System.out.print("Enter loan term (in years): "); int termInYears = scanner.nextInt(); System.out.print("Enter interest rate: "); double interestRate = scanner.nextDouble(); // Display details of loan double monthlyPayment = calculateMonthlyPayment(loanAmount, termInYears, interestRate); // NumberFormat is useful for formatting numbers // In our case we'll use it for // formatting currency and percentage values NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(); NumberFormat interestFormat = NumberFormat.getPercentInstance(); // Display details of the loan System.out.println("Loan Amount: "+ currencyFormat.format(loanAmount)); System.out.println("Loan Term: "+ termInYears+" years"); System.out.println("Interest Rate: "+ interestFormat.format(interestRate)); System.out.println("Monthly Payment: "+ currencyFormat.format(monthlyPayment)); } }
There are many extensions and variations to this problem which we shall cover in future examples.
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.
Hello great code am in my first year and been given this assignment of a loan calculator am not a novice when it comes to programming expect with weeks of java been tought i stilll feel like i know nothing, the tutor is asking for a number of classes and super classes and i feel like i havent got a clue how or where to start , if you have any advice i would very much appreciate, thank you
paul