I have to write Java application program that displays a table that indicates th
ID: 640558 • Letter: I
Question
I have to write Java application program that displays a table that indicates the payoff for a car after each payment. User has to use (Scanner) to enter the (Monthly Payment),(annual interest), and (number of required months required for paying completely for the car). program should use this >> Balance = Payment * (1-(1+MonthlyInterest)^(PaymentNumber - Total Number of Payments)) / MonthlyInterest. For Example:<< balance=payment*(1-Math.pow(1+MonthlyInterest,PaymentNumber-Total Number of Payments))/MonthlyInterest >>. format table like this:
<<After payment Number>> <<payoff>>
1 >> 1000, 2 >> 955, 3 >> until zero
The following is given to allow you to know if you have written the formula correctly. if the monthly payment = ($165.25), the total number of payments =(36), and the annual interest =(9%). the balance after the first payment is ($5070.31).
please Help me with this program.
Thank you
Explanation / Answer
import java.text.NumberFormat;
import java.util.Scanner;
* Then display the balance over the term of the loan.
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);
}
}