Mortgage Calculator
Simple mortgage calculator in Java, exploring fundamental programming concepts and practical financial calculations.
This mortgage calculator project is based on the Java Tutorial for Beginners by Programming with Mosh. It’s a practical way to learn Java programming, helping you understand how to handle user input, perform calculations, and format output in a useful application.
Sample Output
1
2
3
4
Principal: 100000
Annual Interest Rate: 3.92
Period (Years): 30
Mortgage: $472.81
Formula
Source: WikiHow
Constants and Scanner Initialization
1
2
3
Scanner scanner = new Scanner(System.in);
final int YEAR = 12;
final int PERCENT = 100;
- Scanner Initialization: This line initializes a
Scannerobject namedscannerto read input from the console (System.in). - Constants (YEAR and PERCENT): These are declared as
final int, making them constants in Java.YEARis set to 12, representing the number of months in a year.PERCENTis set to 100, used for converting percentage values to decimal form.
User Input and Calculation Initialization
1
2
3
4
5
6
7
8
9
10
System.out.print("Principal: ");
int principal = scanner.nextInt();
System.out.print("Annual Interest Rate: ");
double annualInterest = scanner.nextDouble();
double monthlyInterest = annualInterest / PERCENT / YEAR;
System.out.print("Period (Years): ");
int period = scanner.nextInt();
int numPayment = period * YEAR;
- User Input:
principal: Reads and stores the principal amount borrowed from the user.annualInterest: Reads and stores the annual interest rate provided by the user.monthlyInterest: Calculates the monthly interest rate by dividing the annual interest rate by 12 (number of months in a year) and by 100 to convert it from percentage to decimal.period: Reads and stores the number of years over which the loan will be repaid.numPayment: Calculates the total number of monthly payments by multiplyingperiod(years) byYEAR(months in a year).
Mortgage Calculation
1
2
3
double mortgage = principal
* (monthlyInterest * Math.pow(1 + monthlyInterest, numPayment))
/ (Math.pow(1 + monthlyInterest, numPayment) - 1);
Calculates the monthly mortgage payment using the formula where:
- M is
mortgage, the monthly payment. - P is
principal, the loan amount. - r is
monthlyInterest, the monthly interest rate. - n is
numPayment, the total number of monthly payments.
Math.pow()is a Java method used to raise a number to a specified power, returning the result as a double.
Output Formatting and Display
1
2
String mortgageFormat = NumberFormat.getCurrencyInstance(Locale.US).format(mortgage);
System.out.print("Mortgage: " + mortgageFormat);
Uses NumberFormat.getCurrencyInstance(Locale.US) to format the mortgage amount into a currency format suitable for the US locale.
Complete Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.text.NumberFormat;
import java.util.Locale;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
final int YEAR = 12;
final int PERCENT = 100;
System.out.print("Principal: ");
int principal = scanner.nextInt();
System.out.print("Annual Interest Rate: ");
double annualInterest = scanner.nextDouble();
double monthlyInterest = annualInterest / PERCENT / YEAR;
System.out.print("Period (Years): ");
int period = scanner.nextInt();
int numPayment = period * YEAR;
double mortgage = principal
* (monthlyInterest * Math.pow(1 + monthlyInterest, numPayment))
/ (Math.pow(1 + monthlyInterest, numPayment) - 1);
String mortgageFormat = NumberFormat.getCurrencyInstance(Locale.US).format(mortgage);
System.out.print("Mortgage: " + mortgageFormat);
scanner.close();
}
}
This post is licensed under CC BY 4.0 by the author.