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

Using Java: create a loan payment calculator : Prompt the user for an amount and

ID: 3565868 • Letter: U

Question

Using Java: create a loan payment calculator:

Prompt the user for an amount and a number of years. Print the amount and number of years. Call a method loanRate that takes an input loan amount as a parameter and returns an interest rate. If a jumbo loan (a loan amount is greater than or equals to $350,000), use 4.0%. For a large loan (not jumbo but greater than or equal to $100,000), use 3.5%. Otherwise, the interest rate is 3.0%. No print statements should be included in this method. The main method will print the loan rate. Call a method loanMonthlyPayment that will take three parameters: the loan amount, rate, and number of years. It will return a monthly payment amount. This method should not print anything. The main method should print the monthly payment. Call a method loanTotalPayment that will take 4 parameters: the loan amount, monthly payment amount, rate and the number of years. It will return the total payment amount after printing a table like the one in the sample output. The main method should then print the total payment amount.

Details

You will need to use Scanner to obtain input from the keyboard. You should declare a Scanner variable named console at the beginning of your main method. Only the main method will input from the keyboard.

Input Format

Inputting and varify the input data. When writing your program, assume that two numbers will be entered, a double value giving the loan amount and an integer giving the loan duration in years. Use the console.nextDouble() and console.nextInt() to read in the values. Get the rest of the assignment working before coming back to this and improve the input handling of your program. Your program must accept input as 4 tokens and verify that the input is in an appropriate format. The first token will be either "amount" or "years" (without the quotes). If the first token is "amount", the next is the amount of the loan and should be read in using console.nextDouble(). If the first token is "years", the next token is the number of years and should be read in with console.nextInt(). If the first token was "amount", the third token should be "years" and the number of years is read in as the fourth token using console.nextInt(). If the first token was "years", the third token should be "amount" and the amount should be read in with console.nextDouble(). Any other tokens are considered invalid input. If invalid input is detected, the program should print an appropriate message, explaining how the input is invalid. At that point it should exit the program. You can exit the program by calling return. You need to also validate the numeric input before you call console.nextInt() or console.nextDouble(). You can do this using the following Scanner methods: boolean hasNextInt(): returns true if the next token is a valid int. boolean hasNextDouble(): returns true if the next token is a valid double. You have to display the dollar amounts with 2 decimal places by defining the following function:

private static String twoPlaces(double d) {

DecimalFormat df = new DecimalFormat("0.00");

return df.format(d); }

Do not use tabs to line up the columns. Instead, write a method called padOnLeft which takes 2 parameters, a String, s and an int, n. If the length of s is at least n, return s. Otherwise, return a string of length n obtained by putting the appropriate number of blanks at the front of s. You can create this string with a for loop. Use this to create the columns of your table with the decimal points in each column lined up. Given a loan amount, b, an monthly interest rate, r, and a loan period in months, n, you can compute monthly payment, p, using the following formula: p = (b*r)/( 1-(1+r)^-n

Example output:

Enter the loan amount and loan duration in years, for example

amount 3000 years 2

[DrJava Input Box]

Loan amount: $300000.00

Loan period: 2 years

Loan rate: 3.5%

Monthly payment: $12960.82

Month Balance Payment Remaining

1 300875.00 12960.82 287914.18

2 288753.93 12960.82 275793.12

3 276597.51 12960.82 263636.70

4 264405.64 12960.82 251444.82

5 252178.20 12960.82 239217.38

6 239915.10 12960.82 226954.28

7 227616.23 12960.82 214655.42

8 215281.50 12960.82 202320.68

9 202910.78 12960.82 189949.97

10 190503.99 12960.82 177543.17

11 178061.00 12960.82 165100.19

12 165581.73 12960.82 152620.91

13 153066.06 12960.82 140105.24

14 140513.88 12960.82 127553.06

15 127925.09 12960.82 114964.28

16 115299.59 12960.82 102338.77

17 102637.26 12960.82 89676.44

18 89938.00 12960.82 76977.18

19 77201.70 12960.82 64240.88

20 64428.25 12960.82 51467.44

21 51617.55 12960.82 38656.73

22 38769.48 12960.82 25808.67

23 25883.94 12960.82 12923.12

24 12960.82 12960.82 0.00

Total payment amount: $311059.60

Explanation / Answer

// Note: need to input the string "amount" and "years" before numeric data

// For example: amount 300000 years 2

import java.text.DecimalFormat;

import java.util.Scanner;

public class Loancalc
{

public static void main(String Args[])
{
Scanner console = new Scanner(System.in);
double loan = -1, rate = 0;
int duration = -1;
  
System.out.println("Enter the loan amount and loan duration in years, for example");
System.out.println("amount 3000 years 2");
System.out.println();
  
for (int i = 0; i < 2; i++)
{
String cmd = console.next();
if (cmd.equals("amount"))
{
if (!console.hasNextDouble())
{
System.out.println("Invalid amount.");
return;
}
loan = console.nextDouble();
}
else if (cmd.equals("years"))
{
if (!console.hasNextInt())
{
System.out.println("Invalid years.");
return;
}
duration = console.nextInt();
}
else
{
System.out.println("Unknown command.");
return;
}
  
}
  
if (duration < 0)
{
System.out.println("Please enter valid years.");
return;
}
if (loan < 0)
{
System.out.println("Please enter valid loan amount.");
return;
}
  
  
rate = loanRate(loan);
System.out.println("Loan amount: $" + twoPlaces(loan));
System.out.println("Loan period: " + duration + " years");
System.out.println("Loan rate: " + rate + "%");
double monthlypayment = loanMonthlyPayment(loan, rate / 1200, duration * 12);
System.out.println("Monthly payment: $" + twoPlaces(monthlypayment));
double total = loanTotalPayment(loan, monthlypayment, rate, duration);
System.out.println("Total payment amount: $" + twoPlaces(total));
}

public static double loanMonthlyPayment(double loanAmount,
double monthlyRate, int loanDuration)
{
return (loanAmount * monthlyRate)
/ (1 - Math.pow((1 + monthlyRate), -loanDuration));
}

public static double balance(double bal, double loanRate)
{
return (bal + bal * loanRate / 12);
}

public static double loanTotalPayment(double loanAmount,
double loanMonthlyPayment, double loanRate, int years)
{
double totalamount = 0;
System.out.println("Month" + padOnLeft("Balance", 15) +
padOnLeft("Payment", 15) + padOnLeft("Remaining", 15));
  
double mrate = loanRate / 1200;
  
for (int i = 1; i <= years * 12; i++)
{
loanAmount = loanAmount + loanAmount * mrate;
double remain = loanAmount - loanMonthlyPayment;
  
System.out.println(padOnLeft(String.valueOf(i), 5) +
padOnLeft(twoPlaces(loanAmount), 15) +
padOnLeft(twoPlaces(loanMonthlyPayment), 15) +
padOnLeft(twoPlaces(remain), 15)

);
loanAmount = remain;
totalamount += loanMonthlyPayment;
}
return totalamount;
}

public static String twoPlaces(double d)
{
DecimalFormat df = new DecimalFormat("0.00");
return df.format(d);
}

public static String padOnLeft(String s, int n)
{
while (s.length() < n)
{
s = " " + s;
}

return s;
}

public static double loanRate(double loanAmount)
{
if (loanAmount >= 350000)
{
return 4.0;
}
else if (loanAmount >= 100000)
{
return 3.5;
}
else
{
return 3.0;
}
}
}

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