Create a file named mortgagePayment eate a file named mortgagePaymentTable.html
ID: 673978 • Letter: C
Question
Create a file named mortgagePayment eate a file named mortgagePaymentTable.html that lets an user enter the loan nt, loan period in number of years, the beginning interest rate and ending interest rate, then displays the monthly and total payments for each interest rate beginning interest rate to the ending interest rat starting from the e, with an increment of 1/8. Please use the following formula to compute the monthly payment: loanAmountx monthlyInerestRate monthlyPayment = loandmountx monthly InarestRate (1+monthlyInerestRate)o For total payment, you should calculate it as: tota!Payment = monthlyPayment * num0fYears * 12 ·The beginning interest rate is a dropdown list with the selection of 1%, 2%, 3%, 4% and 5% ·The ending interest rte is a dropdown list with the selection of 6%, 7% 8%, 9% and 10% The following is how the program looks like:Explanation / Answer
import java.util.Scanner;
import java.lang.Math;
public class AmortizationTable
{
public static void main(String[] args)
{
//int ctrl;
Scanner input = new Scanner(System.in);//Creates Scanner input
//Prompt the user for Loan Amount
System.out.print("Enter the loan Amount: ");
double LoanAmt = input.nextDouble();
//Prompt for Number of Years
System.out.print("Enter the number of years for the loan: ");
int numOfYears = input.nextInt();
//Prompt for APR
System.out.print("Enter Annual Percentage Rate(APR) like 8.25: ");
double APR = input.nextDouble();
//Prompt for Monthly Payments
System.out.print("Enter your monthly payments: ");
double MonthlyPayment = input.nextDouble();
37
//Create the monthly interest rate(APR is a yearly rate)
38
double Mrate = (APR / 1200);
39
System.out.println("Mrate "+Mrate);
40
41
//Give Balance the value of the Loan Amount
double principal = LoanAmt;
double TotInterest = 0;//Creates the total interest
double principalPay;//The principal loan that changes
double interest;//The interest of the loan
//The header display
System.out.println("Payment # Principal Payment APR MR Interest Payment Principal Payment");
int i;//a counter for the loop
for(i = 0; i <= numOfYears * 12; i++)
{
interest = (int) (Mrate * principal * 100) / 100;
principalPay = (int) ((MonthlyPayment - interest) * 100) / 100;
principal = (int) ((principal - principalPay) * 100) / 100;
TotInterest += interest;
System.out.println(i + " " + principal
+ " " + MonthlyPayment + " " + APR + " " + Mrate + " " +TotInterest
+ " " + principalPay);
}
System.out.println("The total interest over the life of the loan is: "+TotInterest);
}
}
Try this code also
import java.text.NumberFormat;
import java.util.Scanner;
/**
* Write a Java program without a graphical user interface that
* calculates and displays the mortgage payment amount given the
* amount of the mortgage, the term of the mortgage, and the
* interest rate of the mortgage.
*
* Then display the balance over the term of the loan.
*
* @author http://learn-java-by-example.com
*
*/
public class MortgageCalculator {
/**
* Display monthly balance for the term of a loan
*
* @param loanAmount total amount of loan
* @param termInYears term of loan in years
* @param interestRate loan interest rate, 5.6% = 5.6
* @param monthlyPayment monthly payment
*/
public static void displayMonthlyBalance(int loanAmount,
int termInYears, double interestRate, double monthlyPayment) {
NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();
interestRate /= 100.0;
double monthlyRate = interestRate / 12.0;
int termInMonths = termInYears * 12;
// Loop through the term of the loan tracking the balance
double balance = loanAmount;
for (int i=0; i<termInMonths; i++) {
// Add interest to the balance
balance += (balance * monthlyRate);
// Subtract the monthly payment
balance -= monthlyPayment;
// Display running balance
System.out.println("Balance after payment "+(i+1)+": "+
currencyFormat.format(balance));
}
}
/**
* Calculates the monthly payment for a specified loan
*
* @param loanAmount total amount of loan
* @param termInYears term of loan in years
* @param interestRate loan interest rate, 5.6% = 5.6
* @return monthly payment
*/
public static double calculateMonthlyPayment(
int loanAmount, int termInYears, double interestRate) {
// Convert interest rate into a decimal
// eg. 6.5% = 0.065
interestRate /= 100.0;
// Monthly interest rate
// is the yearly rate divided by 12
double monthlyRate = interestRate / 12.0;
// The length of the term in months
// is the number of years times 12
int termInMonths = termInYears * 12;
// Calculate the monthly payment
// Typically this formula is provided so
// we won't go into the details
// The Math.pow() method is used
// to calculate values raised to a power
double monthlyPayment =
(loanAmount*monthlyRate) /
(1-Math.pow(1+monthlyRate, -termInMonths));
return monthlyPayment;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Prompt user for details of loan
System.out.print("Enter loan amount: ");
int loanAmount = scanner.nextInt();
System.out.print("Enter loan term (in years): ");
int termInYears = scanner.nextInt();
System.out.print("Enter interest rate: ");
double interestRate = scanner.nextDouble();
// Calculate the monthly payment
double monthlyPayment = calculateMonthlyPayment(
loanAmount, termInYears, interestRate);
// NumberFormat is useful for formatting numbers
// In our case we'll use it for
// formatting currency and percentage values
NumberFormat currencyFormat =
NumberFormat.getCurrencyInstance();
NumberFormat interestFormat =
NumberFormat.getPercentInstance();
// Display details of the loan
System.out.println("Loan Amount: "+
currencyFormat.format(loanAmount));
System.out.println("Loan Term: "+
termInYears+" years");
System.out.println("Interest Rate: "+
interestFormat.format(interestRate));
System.out.println("Monthly Payment: "+
currencyFormat.format(monthlyPayment));
// Now display the monthly balance for
// the term of the loan
displayMonthlyBalance(
loanAmount, termInYears, interestRate, monthlyPayment);
}
}
import java.util.Scanner;
import java.lang.Math;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.