Coders Packet

Mortgage Calculator in Java using standard Java Libraries

By Pallav Goswami

This is a Java program that calculates the monthly mortgage payment based on the user-entered principal, annual interest rate, and period in years.

INTRODUCTION

This is a Java program that calculates the monthly mortgage payment based on the principal, annual interest rate, and period in years entered by the user. The program prompts the user to enter the required values and performs input validation to ensure that the values entered are within the required range. It then calculates the mortgage payment using the formula for mortgage payments and displays the result to the user in currency format using the NumberFormat class.

OVERVIEW

This Java code for the mortgage calculator uses standard Java libraries, such as java.text.NumberFormat, java.util.Currency and java.util.Scanner, which are included in the Java Development Kit (JDK) by default. The code defines a Java class named MortgageCalculator with a main method that takes user input to calculate and display the monthly mortgage payment for a given loan.

PURPOSE

The purpose of the Mortgage Calculator program is to calculate the monthly mortgage payment for a home loan based on the principal amount borrowed, the interest rate, and the number of years the loan is taken out for.

INPUTS

The user is prompted to enter three inputs:

1. Principal: The amount borrowed, which must be between $1,000 and $1,000,000

2. Annual Interest Rate: The interest rate for the loan, which must be between 1 and 30%

3. Period: The number of years the loan is taken out for, which must be between 1 and 30

CALCULATIONS

Once the user inputs are validated, the program uses the following formula to calculate the monthly mortgage payment:

M = P [ i(1 + i)^n ] / [ (1 + i)^n – 1]

where:

M = monthly mortgage payment

P = the principal amount borrowed

i = the monthly interest rate (calculated as the annual interest rate divided by 12)

n = the total number of monthly payments (calculated as the number of years multiplied by 12)

OUTPUT

The calculated monthly mortgage payment is displayed in US dollars (USD) in currency format using the NumberFormat and Currency classes.

CODE STRUCTURE

The code is structured as follows:

1. The program imports the Scanner, NumberFormat, and Currency classes

2. The program defines constants for the number of months in a year and the percentage value of interest rates

3. The program initializes variables for the principal, monthly interest, and number of payments to 0

4. The program uses a Scanner object to prompt the user for input and validates each input to ensure it is within the required range

5. The program calculates the monthly mortgage payment using the formula above

6. The program uses the NumberFormat and Currency classes to format the output as USD currency

7. The program outputs the formatted mortgage payment to the console.

RESULTS

results

Download Complete Code

Comments

No comments yet

Download Packet

Reviews Report

Submitted by Pallav Goswami (pallavgoswami)

Download packets of source code on Coders Packet