Java Program To Calculate Mortgage Payment – The following java code has been written in 3 simple ways for mortgage payment calculator. If you have any queries leave a comment here.

The methods used in this article are as follows:

- Using Standard Method
- Using Static Method
- Using Separate Class

Mortgage Payment, as we all know, is the amount of money that you need to return to the person or entity where you have your property mortgaged with.

The frequency of the transaction is monthly and can be calculated with the help of a simple formula.

The formula for Mortgage Payment is as follows:

M = P [{r(1+r)^n}/{(1+r)^n – 1}]

where

M = Monthly payment

P = principal

r = rate

n = number of payments

With the help of this formula, you can easily calculate the amount of money that you need to give back.

Thus, the various methods to calculate mortgage payment in Java Programming are as follows:

## Java Mortgage Payment

One of the basic way every beginner codes is that, they write the entire program in the main method of the program. This is the standard method that people generally follow.

So, initially our inputs are read at run time using the scanner class.

Our problem requires principal amount (principal), rate of interest (rate), time period (time). The input rate that we have taken is in the form of percentage rate per the entire year, but we require this on a monthly basis. So, we make use of the below formula.

1 |
rate =( rate/100)/12; |

Apart from rate, even the time period is taken in years, so we convert years into months using,

1 |
time = time * 12; |

After getting all the desired quantities for performing the calculation for the monthly payment, we make use of the standard formula used i.e.,

double p= (principal * rate) / (1 – Math.pow(1 + rate, -time));

To perform power function in Java, we make use of the Math package which consists of the pow method in it. The math pow function returns a double as is syntactically represented as Math.pow(base,exponent).

The end result stored in p is the payment that is required to be made which can be printed in the console screen.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.Scanner; class MortagePayment { public static void main(String arg[]) { Scanner scan = new Scanner(System.in); System.out.print("Enter Principal Amount : "); double principal = scan.nextDouble(); System.out.print("Enter Rate of Interest : "); double rate = scan.nextDouble(); rate =( rate/100)/12; System.out.print("Enter Time period in years : "); int time = scan.nextInt(); time = time * 12; double p= (principal * rate) / (1 - Math.pow(1 + rate, -time)); // System.out.println("Payment: " + p); } } |

Output:

1 2 3 4 |
Enter Principal Amount : 100000 Enter Rate of Interest : 10 Enter Time period in years : 5 Payment: 2124.704471126833 |

### Mortgage Java – Static Method

Static method is a separate method apart from main which doesn’t need an object to be created to reference it and usually makes use of static data variables.

In the standard method we have observed that, the entire code being written in the same main method.

This is not very efficient when it comes to readability of the code. So, to make it more convenient, we make use of another separate static method.

Here, the main method only comprises of the required inputs taken using the scanner class along with the basic calculations like:

1 2 |
rate =( rate/100)/12; time = time * 12; |

Apart from this, the essential algebraic equation used for getting the mortgage payment is written in the static method (payment) which returns a value of type double which gives the payment to be made.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.util.Scanner; class MortagePayment { public static void main(String arg[]) { Scanner scan = new Scanner(System.in); System.out.print("Enter Principal Amount : "); double a = scan.nextDouble(); System.out.print("Enter Rate of Interest : "); double b = scan.nextDouble(); b =( b/100)/12; System.out.print("Enter Time period in years : "); double c = scan.nextInt(); c= c* 12; double pay=Payment(a,b,c); System.out.println("Payment: " + pay); } static double Payment(double p,double r,double t) { double m= (p * r) / (1 - Math.pow(1 + r, -t)); m= Math.round(m* 100) / 100; return m; } } |

Output:

1 2 3 4 |
Enter Principal Amount : 1000 Enter Rate of Interest : 12.5 Enter Time period in years : 2 Payment: 47.0 |

**Using Separate Class**

In the above scenario of static method we have seen that using another static method we increase the readability of the code. There is another way to enhance it further and that is by making use of a separate class itself for logic.

When the logic is written in a completely different class not just does it increase the readability and scalability but also, each class would not be aware of the contents in another class so it enhances the security of data as well.

So a separate class (Payment) can be created consisting of method which has the logic behind getting the payment i.e.,

1 |
double p= (principal * rate) / (1 - Math.pow(1 + rate, -time)); |

while, the main method takes in input with help of the scanner class at run time and instantiates an object referring to this separate class (Payment) with which the method consisting of the logic (payment) can be invoked.

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 |
import java.util.Scanner; class Payment { double m; Payment(double p,double r,double t) { m= (p * r) / (1 - Math.pow(1 + r, -t)); m= Math.round(m* 100) / 100; } } class MortagePayment { public static void main(String arg[]) { Scanner scan = new Scanner(System.in); System.out.print("Enter Principal Amount : "); double a = scan.nextDouble(); System.out.print("Enter Rate of Interest : "); double b = scan.nextDouble(); b =( b/100)/12; System.out.print("Enter Time period in years : "); double c = scan.nextInt(); c= c* 12; Payment pay=new Payment(a,b,c); System.out.println("Payment: " + pay.m); } } |

Output1:

1 2 3 4 |
Enter Principal Amount : 75000 Enter Rate of Interest : 7.5 Enter Time period in years : 5 Payment: 1502.0 |