Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java - Calculate the monthly payment on a loan Specifications ? The formula for

ID: 3560401 • Letter: J

Question

Java - Calculate the monthly payment on a loan

Specifications
? The formula for calculating monthly payment is:
double monthlyPayment =
loanAmount * monthlyInterestRate/
(1 - 1/Math.pow(1 + monthlyInterestRate, months));
? The application should accept decimal entries for the loan amount and interest rate entries.
? The application should only accept integer values for the years field.
? The application should only accept integer and decimal values within the following ranges:
Greater Less
Than Than
Loan amount: 0 1,000,000
Yearly interest rate: 0 20
Years: 0 100
? The application should only accept a value of

Explanation / Answer

import java.text.DecimalFormat;

import java.util.Scanner;

?

public class MonthlyPayment {

public static double loanAmount;

public static int year;

public static double yearlyInterestRate;

public static double monthlyPayment;

public static void main(String[] args)

{

Scanner scan=new Scanner(System.in);

char choice='n';

System.out.println("Welcome to the loan calculator ");

do

{

System.out.println("Data Entry");

loanAmount=getDoubleWithinRange(scan,"Enter loan amount :",0,1000000);

yearlyInterestRate=getDoubleWithinRange(scan,"Enter Yearly interest rate :",0,20);

year=getIntWithinRange(scan,"Enter number of Years :",0,100);

monthlyPayment=getMonthlyPayment();

printResult();

System.out.println("Continue? (y/n) :");

choice=scan.nextLine().charAt(0);

}while(choice=='y');

}

public static double getDoubleWithinRange(Scanner sc, String prompt,

double min, double max)

{

int error=0;

double value=0;

do

{

error=0;

System.out.println(prompt);

try

{

value=Double.parseDouble(sc.nextLine());

}

catch(Exception e)

{

error=1;

System.out.println("Invalid Decimal Value. Try Again");

}

if(value<min)

{

error=1;

System.out.println("Error ! Number must be greater than "+min);

}

if(value>max)

{

error=1;

System.out.println("Error ! Number must be less than "+max);

}

}while(error==1);

return value;

}

public static int getIntWithinRange(Scanner sc, String prompt,

int min, int max)

{

int error=0;

int value=0;

do

{

System.out.println(prompt);

try

{

value=Integer.parseInt(sc.nextLine());

}

catch(Exception e)

{

error=1;

System.out.println("Invalid Integer Value. Try Again");

}

if(value<min)

{

error=1;

System.out.println("Error ! Number must be greater than "+min);

}

if(value>max)

{

error=1;

System.out.println("Error ! Number must be less than "+max);

}

}while(error==1);

return value;

}

public static double getMonthlyPayment()

{

int months=year*12;

double monthlyInterestRate=yearlyInterestRate/12;

double monthlyPayment =

loanAmount * monthlyInterestRate/

(1 - 1/Math.pow(1 + monthlyInterestRate, months));

return monthlyPayment;

}

public static void printResult()

{

System.out.println("Loan Amount : "+loanAmount);

System.out.println("Yearly Interest rate : "+yearlyInterestRate);

System.out.println("Number of years : "+year);

System.out.println("Monthly Payment : "+ new DecimalFormat("##.##").format(monthlyPayment));

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote