Convert the application into a free standing class definition that can be used b
ID: 3591350 • Letter: C
Question
Convert the application into a free standing class definition that can be used by a client wishing to perform mortgage calculations.
Create a java program that does the following: Specifically, you should create a class named MortgageCalcs with the following private instance variables and public methods: private instance variables: double interestRate double term double principal Public Methods: MortgageCalcs(double interestRate, double term, double principal) //Constructor Method. Accepts values for interest rate, term, and principal and places //these values into the corresponding instance variable double calcPayment() //this method calculates and returns the Monthly Payment. double futureValue() //this method calculates and returns the Future Value. double intCharge() //this method calculates and returns the Interest Charge. double getinterestRate() double getterm() double getprincipal() // get methods for each instance variable boolean amortize(int months) //this method produces an amortization table for the loan. An amortization table details every monthly payment, showing how much of that payment covers interest costs, and how much of the payment actually reduces the loan balance. The Interest portion of the payment is calculated as the Monthly Interest Rate (Interest Rate/12) times the previous balance. The Principal portion of the payment is calculated as Monthly Payment - Interest. The new Balance is calculated by subtracting the Principal from the previous balance. For a $20,000 loan at 7.5% for 5 years, the amortization table for the first 3 months looks like: (monthly interest rate = 7.5%/12= %0.625 = .00625) Payment Amount Interest Principal Balance 20,000.00 1 400.76 125.00 275.76 19,724.24 2 400.76 123.28 277.48 19,446.76 3 400.76 121.54 279.22 19,167.54 Your method should take a single integer parameter indicating the number of months to produce the table. If the number of months is greater then the term, the method does nothing and returns false. Otherwise, the method produces the table for the indicated number of months and returns true.
Explanation / Answer
MortgageCalcs.java
package mortgage;
import java.text.DecimalFormat;
public class MortgageCalcs {
// private instance variables
private double interestRate;
private double term;
private double principal;
DecimalFormat df = new DecimalFormat("0.00");
// Constructor Method. Accepts values for interest rate, term, and principal
// and places
// these values into the corresponding instance variable
public MortgageCalcs(double interestRate, double term, double principal) {
this.interestRate = interestRate;
this.term = term;
this.principal = principal;
}
// this method calculates and returns the Monthly Payment.
public double calcPayment() {
double monthlyPayment = (principal * intCharge() * Math.pow(1 + intCharge(), term))
/ (Math.pow(1 + intCharge(), term) - 1);
return monthlyPayment;
}
// this method calculates and returns the Future Value.
public double futureValue() {
double monthlyInterest = principal * intCharge();
double debtPaid = calcPayment() - monthlyInterest;
principal = principal - debtPaid;
return principal;
}
// this method calculates and returns the Interest Charge.
public double intCharge() {
return interestRate / (12 * 100);
}
// get methods for each instance variable
public double getInterestRate() {
return interestRate;
}
public double getTerm() {
return term;
}
public double getPrincipal() {
return principal;
}
// this method produces an amortization table for the loan.
public boolean amortize(int months) {
double monthlyPayment = calcPayment();
double interestCharge = intCharge();
int i = 1;
//Checks for the number of months entered and principal remaining is not less than monthly emi
while (principal >= monthlyPayment && i <= months) {
double monthlyInterest = principal * interestCharge;
double debtPaid = monthlyPayment - monthlyInterest;
principal = principal - debtPaid;
System.out.println(i + " $" + df.format(monthlyPayment) + " $" + df.format(monthlyInterest) + " $"
+ df.format(debtPaid) + " $" + df.format(principal));
i++;
}
//If principal amount is less than monthly payment
if (i <= months)
System.out.println(i + " $" + df.format(principal) + " $" + df.format(principal) + " $"
+ df.format(principal) + " $0.00");
return true;
}
}
MainTest.java
package mortgage;
public class MainTest {
//Main method to create Mortgage class and use amortize method.
public static void main(String[] args) {
double principal = 20000;
double interestRate = 7.5;
int term = 60;
MortgageCalcs mortgage = new MortgageCalcs(interestRate, term, principal);
System.out.println("Month# Amount interest Principal Balance");
System.out.println("====== ============= ============= ========= ============");
mortgage.amortize(10);
}
}
Sample Output
Month# Amount interest Principal Balance
====== ============= ============= ========= ============
1 $400.76 $125.00 $275.76 $19724.24
2 $400.76 $123.28 $277.48 $19446.76
3 $400.76 $121.54 $279.22 $19167.54
4 $400.76 $119.80 $280.96 $18886.58
5 $400.76 $118.04 $282.72 $18603.86
6 $400.76 $116.27 $284.48 $18319.38
7 $400.76 $114.50 $286.26 $18033.11
8 $400.76 $112.71 $288.05 $17745.06
9 $400.76 $110.91 $289.85 $17455.21
10 $400.76 $109.10 $291.66 $17163.55