In: Computer Science
Using Java:
When buying a house or condo (using a mortgage), or when taking out a loan, you’ll usually 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 over-payment 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:
Please print all dollar amounts to 2 decimal places.
All the explanation is in the code comments. Hope it helps!
Code:
import java.util.*;
public class Main
{
public static void calcInterest(float outBal, float annRate, float
monthlyPay, int months, float totalInt)
{
// base condition
if(outBal <= 0)
{
// print the months and total interest
// months - 1 because the current months is not included
System.out.println("The number of months required to pay off a
mortgage or loan: " + (months - 1));
System.out.printf("The total interest paid over the life of the
mortgage or loan: $%.2f\n", totalInt);
return;
}
System.out.println("For month " + months + "
------------------------");
// print Outstanding balance
System.out.printf("Outstanding balance: $%.2f\n", outBal);
// calcuate monthly interest
float monthlyInterest = annRate / (100 * 12) * outBal;
// print monthly interest
System.out.printf("Monthly interest: $%.2f\n",
monthlyInterest);
// check case of negative amortization
if(monthlyPay <= monthlyInterest)
{
System.out.println("Stopping because of negative
amortization");
return;
}
// recursive call with updated Outstanding balance
calcInterest(outBal + monthlyInterest - monthlyPay,
annRate, monthlyPay, months+1, totalInt + monthlyInterest);
}
public static void main(String[] args) {
// input stream
Scanner sc = new
Scanner(System.in);
float initLoan, annRate,
monthlyPay;
// input from user
System.out.println("Enter initial
loan, annual interest and monthly payment");
initLoan =
Float.parseFloat(sc.next());
annRate =
Float.parseFloat(sc.next());
monthlyPay =
Float.parseFloat(sc.next());
// call to the recursive function
with number of months as 1
// and initial total interest as
0
calcInterest(initLoan, annRate,
monthlyPay, 1, 0.0f);
}
}
Sample run:
Please note that the output was very long, therefore, given just a part of it.
#1
#2
#3
Code screenshots: