The classes Loan and TestLoanClass are posted below. Modify TestLoanClass so tha
ID: 3533622 • Letter: T
Question
The classes Loan and TestLoanClass are posted below. Modify TestLoanClass so that it throws an IllegalArgumentException with an appropriate message for each input variable.
public class Loan
{
private double annualInterestRate;
private int numberOfYears;
private double loanAmount;
private java.util.Date loanDate;
/** Default constructor */
public Loan()
{
annualInterestRate = 2.5;
numberOfYears = 1;
loanAmount = 1000;
}
/** Construct a loan with specified annual interest rate,
number of years and loan amount
*/
public Loan(double annualInterestRate, int numberOfYears,
double loanAmount)
{
this.annualInterestRate = annualInterestRate;
this.numberOfYears = numberOfYears;
this.loanAmount = loanAmount;
loanDate = new java.util.Date();
}
/** Return annualInterestRate */
public double getAnnualInterestRate()
{
return annualInterestRate;
}
/** Set a new annualInterestRate */
public void setAnnualInterestRate(double annualInterestRate)
{
this.annualInterestRate = annualInterestRate;
}
/** Return numberOfYears */
public int getNumberOfYears()
{
return numberOfYears;
}
/** Set a new numberOfYears */
public void setNumberOfYears(int numberOfYears)
{
this.numberOfYears = numberOfYears;
}
/** Return loanAmount */
public double getLoanAmount()
{
return loanAmount;
}
/** Set a newloanAmount */
public void setLoanAmount(double loanAmount)
{
this.loanAmount = loanAmount;
}
/** Find monthly payment */
public double getMonthlyPayment()
{
double monthlyInterestRate = annualInterestRate / 1200;
double monthlyPayment = loanAmount * monthlyInterestRate / (1 -
(Math.pow(1 / (1 + monthlyInterestRate), numberOfYears * 12)));
return monthlyPayment;
}
/** Find total payment */
public double getTotalPayment()
{
double totalPayment = getMonthlyPayment() * numberOfYears * 12;
return totalPayment;
}
/** Return loan date */
public java.util.Date getLoanDate()
{
return loanDate;
}
}
import java.util.Scanner;
public class TestLoanClass {
/** Main method */
public static void main(String[] args) {
// Create a Scanner
Scanner console = new Scanner(System.in);
// Enter yearly interest rate
System.out.print(
"Enter yearly interest rate, for example, 8.25: ");
double annualInterestRate = console.nextDouble();
// Enter number of years
System.out.print("Enter number of years as an integer: ");
int numberOfYears = console.nextInt();
// Enter loan amount
System.out.print("Enter loan amount, for example, 120000.95: ");
double loanAmount = console.nextDouble();
// Create Loan object
Loan loan =
new Loan(annualInterestRate, numberOfYears, loanAmount);
// Display loan date, monthly payment, and total payment
System.out.printf("The loan was created on %s " +
"The monthly payment is %.2f The total payment is %.2f ",
loan.getLoanDate().toString(), loan.getMonthlyPayment(),
loan.getTotalPayment());
}
}
Explanation / Answer
import java.util.Scanner;
public class TestLoanClass {
/** Main method */
public static void main(String[] args) {
// Create a Scanner
Scanner console = new Scanner(System.in);
// Enter yearly interest rate
System.out.print(
"Enter yearly interest rate, for example, 8.25: ");
double annualInterestRate = console.nextDouble();
if (annualInterestRate < 0 || annualInterestRate > 100) {
throw new IllegalArgumentException("interest rate out of bound "+Double.toString(annualInterestRate));
}
// Enter number of years
System.out.print("Enter number of years as an integer: ");
int numberOfYears = console.nextInt();
if (numberOfYears < 0 ) {
throw new IllegalArgumentException("number of year out of bound "+Double.toString(numberOfYears));
}
// Enter loan amount
System.out.print("Enter loan amount, for example, 120000.95: ");
double loanAmount = console.nextDouble();
if (loanAmount < 0 ) {
throw new IllegalArgumentException("loan Amount out of bound "+Double.toString(loanAmount));
}
// Create Loan object
Loan loan =
new Loan(annualInterestRate,numberOfYears,loanAmount);
// Display loan date, monthly payment, and total payment
System.out.printf("The loan was created on %s " +
"The monthly payment is %.2f The total payment is %.2f ",
loan.getLoanDate().toString(), loan.getMonthlyPayment(),
loan.getTotalPayment());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.