Convert the application created for assignment 1 into a free standing class defi
ID: 3873158 • Letter: C
Question
Convert the application created for assignment 1 into a free standing class definition that can be used by a elient wishing to perform mo rtgage calculations Specifically, you ashould create a class naned HortgageCalcs with the tollowing private instance variables and public methods i private instance variables: double interestRate double term double principa Public Methods: MortgageCalcs (double interestRate, double //constructor Method. Accepts values for interest rate, Eerm, and principal and places //these values into the corresponding inatance vari double pr double calcPayment() //thia method calculates and returns the Monthly Payment double futureValue() //this method calculates and returns the ruture value..ut double intCharge () //this method calculates and returns the Interest Charge. double getinterestRate() double getterm0 double getprincipal) // get methods for each inatance variable //this me thod 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 paynent actually r 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.58 for 5 years, the anortization table for the first 3 months looks like: (monthly interest rate " 7.51/12-10.625 " .00625) Payment Amount Interest Principal Balance 10 70 213.9 27 21.n ke 20,000.00 19, 724.24 19, 446,76 19,167.54 125.00 275.76 277.48 279.22 400.76 400.76 21.54 Your method should take a single integer parameter indicating the number of months to produce the table. he number of-months ie gceater than the term the method does nothing and returns fals9. Otherwise, the method produces the table for the Indicated number of months and returns true. A sample client has been created that will allow you to test your implementation of MortgageCalcsExplanation / Answer
UseMortgageCalcs.java
public class UseMortgageCalcs{
public static void main(String[] args){
int k = Integer.parseInt(args[0]);
MortgageCalcs m = new MortgageCalcs(7.5,5,20000);
System.out.println(m.getprincipal());
System.out.println(m.getterm());
System.out.println(m.getinterestRate());
m.amortize(k);
}
}
MortgageCalcs.java
public class MortgageCalcs{
private double interestRate, term, principal;
public MortgageCalcs(double ir, double t, double p){
interestRate = ir;
term = t;
principal = p;
}
public double calcPayment(){//this creates the calcPayment Mehtod
double r = (interestRate/100.0);//changes the rate to a percent
double x = (1.0+(r/12.0));
double y = (term*12.0);
double temp = (1.0/(Math.pow(x,y)));
double payment = ((principal*(r/12.0))/(1-temp));//gives us the payment
return payment;//returns the payement
}
public double futureValue(){
double future = calcPayment()*term*12;//calculates the future value by calling the calcPayment method and multiplying by 12 and the term in years
return future;//returns the future value
}
public double intCharge(){//calculates the interest charged
double intCharge = futureValue()-principal;
return intCharge;
}
public double getinterestRate(){//gets the interest rate
return interestRate;
}
public double getterm(){//gets the term in years
return term;
}
public double getprincipal(){//gets the starting principal
return principal;
}
public boolean amortize(int months){
double newPrin = principal;//sets starting principal equal to the initial principal
double payment = calcPayment();//gets the payment for each month
double monthlyRate = (interestRate/100.0/12.0);//calculates the monthly interest rate
if (months>=(term*12)){//returns false if outside of the term limit
return false;
}
else {
System.out.printf("%s %s %s %2s %2s%n","Month","Payment Amount","Interest","Principal","Balance");//prints the headers for each column
for (int i = 0;i<=months;i++){//starts a for loop for printing the amortization table
if (i == 0) System.out.printf("%,49.2f%n",newPrin);
else{
System.out.printf("%-10d%-11.2f%-9.2f%-10.2f%,.2f%n",i,payment, (newPrin*monthlyRate), (payment-(newPrin*monthlyRate)),newPrin-(payment-(newPrin*monthlyRate)));//prints the valuse in the table
newPrin = newPrin-(payment-(newPrin*monthlyRate));//sets the new principal for the next month
}
}
}
return true;
}
}//end class definition