The consumer loan department of a bank asks a software analyst to write a Java p
ID: 3884276 • Letter: T
Question
The consumer loan department of a bank asks a software analyst to write a Java program that can calculate
the monthly payment of a mortgage
the principal paid in each monthly payment
the interest paid in each monthly payment,
After reading (from the console) necessary data – the loan amount, the loan period (in months), and the annual interest rate, the program processes the data and then prints out (to the console) the following pieces of information as follows:
The loan amount in the 1st line
The loan period (how many months) in the 2nd line
The annual interest rate in the 3rd line
(Leave the next line blank)
Next, the program prints out the following text as shown:
Payment #: Monthly Payment Principal Interest Balance
Andfinally the program prints out the following details of a monthly payment (one line for each payment)
Payment #
Monthly payment amount
Principal paid in the monthly payment
Interest paid in the monthly payment
Balance of the loan after the monthly payment
The inputs must be verified to be sure that they are not negative. All the floating-point numbers of the outputs have 2 digits after the floating point.
Sample of the outputs:
Loan amount: 10000.00
Number of months: 6
Annual interest rate: 2.00
Payment # Monthly Payment Principal Interest Balance
1 1676.40 1659.74 16.67 8340.26
2 1676.40 1662.50 13.90 6677.76
3 1676.40 1665.27 11.13 5012.49
4 1676.40 1668.05 8.35 3344.44
5 1676.40 1670.83 5.57 1673.61
6 1676.40 1673.61 2.79 0.00
Important Notes:
Formula to compute the monthly payment:
monthly payment = loanAmount*monthlyInterestRate/1/1-((1+monthlyInterestRate)^numberOfMonths
Other formulas:
Monthly interest rate = annual interest rate / 12
Interest (in monthly payment) = monthly interest rate * current balance
principal (in monthly payment) = monthly payment - interest
balance (after monthly payment has been paid) = current balance - principal
Explanation / Answer
import java.util.Scanner;
public class Monthlypayment
{
public static void main(String[] args)
{
double loanAmount=0.0;
int loanPeriod;
double annualInterestRate=0.0,monthlyInterestRate=0.0;
Scanner obj=new Scanner(System.in);
System.out.println("Enter the loan amount");
loanAmount=obj.nextDouble();
if(loanAmount<0.0)
{
System.out.println("Invalid Input");
System.exit(0);
}
System.out.println("Enter the loan period(in months)");
loanPeriod=obj.nextInt();
if(loanPeriod<0.0)
{
System.out.println("Invalid Input");
System.exit(0);
}
System.out.println("Enter the annual Interest Rate");
annualInterestRate=obj.nextDouble();
if(annualInterestRate<0.0)
{
System.out.println("Invalid Input");
System.exit(0);
}
monthlyInterestRate=(double)(annualInterestRate/100)/(double)12;
double monthlyPayment=(double)(loanAmount*monthlyInterestRate)/((double)(1-Math.pow((1+monthlyInterestRate),-loanPeriod)));
double principal=0.0,interest=0.0,balance=0.0;
System.out.printf("Loan Amount:%.2f ",loanAmount);
System.out.println("Number of months:"+loanPeriod);
System.out.printf("Annual Interest rate:%.2f ",annualInterestRate);
System.out.print("Payment# Monthly Payment Principal Interest Balance ");
balance=loanAmount;
for(int i=1;i<=loanPeriod;++i)
{
interest=monthlyInterestRate*balance;
principal=monthlyPayment-interest;
balance=balance-principal;
System.out.printf("%d %.2f %.2f %.2f %.2f ",i,monthlyPayment,principal,interest,balance);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.