In: Computer Science
(In Java Using recursive techniques) When buying a house or condo (using a mortgage), or when taking out a loan, you’ll likely wind up with some form of a fixed monthly payment. Mortgages and loans work this way:
The numbers determined at the beginning of every mortgage or loan are:
Interest and new outstanding balance are calculated at the beginning of every month:
If the new outstanding balance goes negative, then the overpayment for the last month is refunded and the mortgage is considered “paid in full”.
Write a program which uses a recursive method to determine and display the number of months required to pay off a mortgage or loan (when the new outstanding balance goes to 0.00) and the total interest paid over the life of the mortgage or loan. You may want to display the outstanding balance and monthly interest calculated at the beginning of each month, but this is not required.
Use the following test cases:
We also need to cover the case where the monthly payment amount doesn’t cover the calculated monthly interest. In this case, the balance on the loan actually goes up, not down! (This is known as “negative amortization”.) If this is the case, your recursive method should show the interest and the monthly payment for the first month, then stop.
Test case for negative amortization:
Remember to use Recursive techniques
Please print all dollar amounts to 2 decimal places.
import java.util.*;
class Main{
public static void main(String ar[]){
Scanner sc=new Scanner(System.in);
float principle,rate,monthlyPayment;
principle=sc.nextFloat();
rate=sc.nextFloat();
monthlyPayment=sc.nextFloat();
Loan l=new Loan(principle,rate,monthlyPayment);
l.print();
l.calculate();
}
}
class Loan{
private float principle;
private float rate;
private float monthlyPayment;
private float totalInterest;
private int time;
Loan(float principle,float rate,float monthlyPayment){
this.principle=principle;
this.rate=rate;
this.monthlyPayment=monthlyPayment;
}
public void print(){
System.out.println(principle);
System.out.println(rate);
System.out.println(monthlyPayment);
}
public void calculate(){
totalInterest=0;
time=0;
float x=principle*rate/1200; //interest of first month
//negative amortization if interest of first month>=monthly
payment
if(x>=monthlyPayment){
System.out.print("Interest of first month is ");
System.out.printf("%.2f",x);
System.out.println();
System.out.print("Monthly payment is ");
System.out.printf("%.2f",monthlyPayment);
System.out.println();
}
// if it is positive amortization then
else{
calc(principle);
System.out.println("Time taken to pay the loan is "+time+"
months");
System.out.print("Total interest paid is ");
System.out.printf("%.2f",totalInterest);
System.out.println();
}
}
private void calc(float amt){
if(amt<=0){ // if amount is <=0 then loan is paid fully
totalInterest=totalInterest-principle+amt; //Interest=paid money -
(principle amount at the begining + overpayment(that will be
refunded))
return;
}
amt=amt+amt*rate/1200-monthlyPayment; //principle amount for next
month
time+=1; //no. of months passed till this point
totalInterest+=monthlyPayment; //paid money till now = money paid
upto last month + payment of current month
calc(amt);
}
}