MortgageCalcs.class package mortgage; import java.text.DecimalFormat; public cla
ID: 3591463 • Letter: M
Question
MortgageCalcs.class
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
Explanation / Answer
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;
}
}
package mortgage;
import java.util.Scanner;
public class MainTest {
// Main method to create Mortgage class and use amortize method.
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double result;
System.out.println("Enter principal amount between 1 and 500000");
result=input(sc, 1, 500000);
/*Loop till user enters a correct value*/
while(result==-1) {
System.out.println("Invalid principal input");
System.out.println("Enter principal amount between 1 to 500000");
result=input(sc, 1, 500000);
}
double principal = result;
System.out.println("Enter the term between 1 and 30");
result=input(sc, 1, 30);
/*Loop till user enters a correct value*/
while(result==-1) {
System.out.println("Invalid term input");
System.out.println("Enter the term between 1 and 30");
result=input(sc, 1, 30);
}
int term = (int)result;
System.out.println("Enter an interest rate between 1 and 15 percent");
result=input(sc, 1, 15);
/*Loop till user enters a correct value*/
while(result==-1) {
System.out.println("Invalid interest input");
System.out.println("Enter an interest rate between 1 and 15 percent");
result=input(sc, 1, 15);
}
double interestRate = result;
MortgageCalcs mortgage = new MortgageCalcs(interestRate, term, principal);
System.out.println("Principal: " + mortgage.getPrincipal());
System.out.println("Future Value: " + mortgage.futureValue());
System.out.println("Term of loan: " + mortgage.getTerm());
System.out.println("Interest Rate: "+ mortgage.getInterestRate());
System.out.println("Interest Charge: "+ mortgage.intCharge());
System.out.println("Payment: "+ mortgage.calcPayment());
System.out.println(" Enter number of months");
result=inputAmortize(sc);
/*Loop till month input = -1 */
while(result!=-1) {
System.out.println("Month# Amount interest Principal Balance");
System.out.println("====== ============= ============= ========= ============");
if(mortgage.amortize((int)result) == false) {
System.out.println("Number of Months too big");
}
System.out.println(" Enter number of months");
result=inputAmortize(sc);
}
}
/*Input for principal, term , and rate of interest*/
public static double input(Scanner sc, int min, int max) {
double n = sc.nextDouble();
if(n >= min && n<=max)
return n;
else
return -1;
}
/*Input for Amortize till user enters -1*/
public static int inputAmortize(Scanner sc) {
int n = sc.nextInt();
return n;
}
}
*******************************************************************************************************************************
Run the MainTest.java file
The output is as explained above. Comments are provided alog with the code.
I hope it helps you.
Kindly rate the answer.
**********************************************************************************************************************************
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.