Create a form that allows you to enter in interest rate, monthly payment, princi
ID: 3927722 • Letter: C
Question
Create a form that allows you to enter in interest rate, monthly payment, principal , and number of years. Your Java Servlet program then generates a table containing a row of output for each month of a mortgage. Your table should show Month number, New Principal, Interest Paid during the month. If the loan is paid off, your table needs to properly stop. You will want to have a loop for the number of 12 * (Number of years). On each iteration of the loop, you will want to calculate the interest paid which is: interestPaid = (newPrincipal * interest)/(12*100) The 12 is because of 12 months in a year. The 100 is due to the fact that interest rates (like 6) are computed as 0.06 in interest calculations. newPrincipal = newPrincipal + interestPaid - monthlyPayment To get rid of the large number of decimal places that show up when you print a double, there is a format method in the String class that can help. Consider the following code (similar to the printf stuff in System.out): String sPrinciple = String.format("%.2f", principal);
The use of Netbeans IDE is suggested for this question
Explanation / Answer
import java.util.Scanner;
class AmortizationProgram{
public static void main(String[] args){
double p,iy;
int n;
Scanner sc=new Scanner(System.in);
System.out.print("Enter amount of loan:");
p=sc.nextFloat();
System.out.print("Enter interest rate per year:");
iy=sc.nextFloat();
System.out.print("Enter number of years:");
n=sc.nextInt();
calAmort(p,iy,n);
}
public static void calAmort(double p,double iy, int ny){
double newbal;
double im=(iy/12)/100;
int nm=ny*12;
double mp,ip,pp;
int i;
mp=p*im*Math.pow(1+im,(double)nm)/(Math.pow(1+im,(double)nm)-1);
printHeader();
for(i=1;i<nm;i++){
ip=p*im;//interest paid
pp=mp-ip; //princial paid
newbal=p-pp; //new balance
printSch(i,p,mp,ip,pp,newbal);
p=newbal; //update old balance
}
pp=p;
ip=p*im;
mp=pp+ip;
newbal=0.0;
printSch(i,p,mp,ip,pp,newbal);
}
public static void printSch(int i,double p,double mp,double ip,double pp,double newbal){
System.out.format("%-8d%-12.3f%-10.3f%-10.3f%-10.3f%-12.3f ",i,p,mp,ip,pp,newbal);
}
public static void printHeader(){
int i;
System.out.println(" Amortization Schedule for Borrower");
for(i=0;i<62;i++) System.out.print("-");
System.out.format(" %-8s%-12s%-10s%-10s%-10s%-12s"," ","Old","Monthly","Interest","Principle","New","Balance");
System.out.format(" %-8s%-12s%-10s%-10s%-10s%-12s ","Month","Balance","Payment","Paid","Paid","Balance");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.