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

Operation The Data Entry section prompts the user to enter values for the loan a

ID: 3799437 • Letter: O

Question

Operation
The Data Entry section prompts the user to enter values for the loan amount, yearly interest rate, and number of years. If the user doesn’t enter data that’s valid, this section displays an appropriate error message and prompts the user again.
The Formatted Results section displays a formatted version of the user’s entries as well as the formatted result of the calculation.
The application prompts the user to continue. Save your program as LoanCalculator.java.

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

Loan amount: 0 1,000,000

yearly interest rate: 0 20

years: 0 100


The application should only accept a value of “y” or “n” at the Continue prompt.
If the user enters invalid data, the application should display an appropriate error message and prompt the user again until the user enters valid data.
The code that’s used to validate data should be stored in separate methods. For example:
public static double getDoubleWithinRange(Scanner sc, String prompt,    double min, double max)
public static int getIntWithinRange(Scanner sc, String prompt,    int min, int max)

Needed for JAVA

Welcome to the Loan Calculator DATA ENTRY Enter loan amount: ten Error Invalid decimal value Try again Enter loan amount: Error Number must be greater than 0.0 100000000000 Enter loan amount: Error Number must be less than 1000000.0 500000 Enter loan amount: Enter yearly interest rate 5.6 Enter number of years: thirty Error Invalid integer value Try again. Enter number of years: Error Number must be greater than 0 Enter number of years: 100 Error Number must be less than 100 Enter number of years: 30 FORMATTED RESULTS $500,000.00 Loan amount: Yearly interest rate 5.6s Number of years 30 Monthly payment $2,870.39 Continue (y/n) Error This entry is required Try again. Continue (y/n) x Error Entry must be 'y' or 'n' Try again Continue (y/n) n

Explanation / Answer

import java.util.*;
import java.io.*;
import java.lang.String;
class LoanCalc
{
   public static void main(String[] args)
{
System.out.println("Welcome to the Loan Calculator ");
Scanner sc = new Scanner(System.in);
String chck = "y" ;
while (chck.equalsIgnoreCase("y"))
{
System.out.println("DATA ENTRY ");
double amount =getDoubleWithinRange(sc,"Enter loan amount: ", 0, 1000000);
double intrstRate =getDoubleWithinRange(sc,"Enter yearly interest rate: ", 0, 20);
int years =getIntWithinRange(sc,"Enter number of years: ", 0, 100);  
double mnthlyIntrstRate = intrstRate/12/100;
int months = years * 12;
double payment = calMnthlypymnt(amount, months, mnthlyIntrstRate);

System.out.println("FORMATTED RESULTS ");
System.out.println("Loan amount: $" + amount);
System.out.println("Yearly interest rate: " + intrstRate+"%");
System.out.println("Number of years: " + years);
System.out.println("Monthly payment: $" + payment);

chck = getChoiceString(sc, "Continue? (y/n): ", "y", "n");
System.out.println();
}
}
   public static double getDoubleWithinRange(Scanner sc, String prompt,
   double min, double max)
   {
   double d = 0.0;
   boolean isValid = false;
   while (isValid == false)
   {
   d = getDouble(sc, prompt); // call the getDouble method
   if (d <= min)
   {
   System.out.println(
   "Error! Number must be greater than " + min + ".");
   }
   else if (d >= max)
   {
   System.out.println(
   "Error! Number must be less than " + max + ".");
   }
   else
   isValid = true;
   }
   return d;
   }

public static double calMnthlypymnt(double loanAmount,int months, double mnthlyIntrstRate)
{
double monthlyPayment =
loanAmount * mnthlyIntrstRate/
(1 - 1/Math.pow(1 + mnthlyIntrstRate, months));

return monthlyPayment;
}
public static double getDouble(Scanner sc, String prompt)
{
double d = 0.0;
boolean isValid = false;
while (isValid == false)
{
System.out.print(prompt);
if (sc.hasNextDouble())
{
d = sc.nextDouble();
isValid = true;
}
else
{
System.out.println("Error! Invalid number. Try again.");
}
sc.nextLine();
// discard any other data entered on the line
}
return d;
}
public static int getInt(Scanner sc, String prompt)
{
int i = 0;
boolean isValid = false;
while (isValid == false)
{
System.out.print(prompt);
if (sc.hasNextInt())
{
i = sc.nextInt();
isValid = true;
}
else
{
System.out.println("Error! Invalid integer value. Try again.");
}
sc.nextLine();
}
return i;
}

public static int getIntWithinRange(Scanner sc, String prompt,int min, int max)
{
int i = 0;
boolean isValid = false;
while (isValid == false)
{
i = getInt(sc, prompt);
if (i <= min)
System.out.println(
"Error! Number must be greater than " + min);
else if (i == max)
System.out.println(
"Error! Number must be less than " + max);
else
isValid = true;
}
return i;
}

  
private static String getRequiredString(Scanner sc, String prompt)
{
String s = "";
boolean isValid = false;
while (isValid == false)
{
System.out.print(prompt);
s = sc.nextLine();
if (s.equalsIgnoreCase(""))
{
System.out.println("Error! This entry is required. Try again.");
}
else
{
isValid = true;
}
}
return s;
}

private static String getChoiceString(Scanner sc, String prompt, String s1, String s2)
{
String s = "";
boolean isValid = false;
while (isValid == false)
{
s = getRequiredString(sc, prompt);
if (
!s.equalsIgnoreCase(s1) &&
!s.equalsIgnoreCase(s2)
)
{
System.out.println("Error! Entry must be '"+
s1 +"' or '"+ s2 +"'. Try again.");
}
else
{
isValid = true;
}
}
return s;
}
  
}

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