Q:

(IllegalArgumentException) Modify the Loan class in Listing 10.2 to throw IllegalArgumentException if the loan amount, interest rate, or number of years is less than or equal to zero

0

(IllegalArgumentException) Modify the Loan class in Listing 10.2 to throw IllegalArgumentException if the loan amount, interest rate, or number of years is less than or equal to zero.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

/*********************************************************************************
* (IllegalArgumentException) Modify the Loan class in Listing 10.2 to throw      *
* IllegalArgumentException if the loan amount, interest rate, or number of years *
* is less than or equal to zero.                                                 *
*********************************************************************************/
import java.util.Scanner;

public class TestLoanClass {
	/** Main method */
	public static void main(String[] args) {
		// Create a Scanner
		Scanner input = new Scanner(System.in);
		boolean continueInput = true;

		do {
			// Enter annual interest rate
			System.out.print(
				"Enter annual interest rate, for example, 8.25: ");
			double annualInterestRate = input.nextDouble();
	
			// Enter number of years
			System.out.print("Enter number of years as an integer: ");
			int numberOfYeras = input.nextInt();
	
			// Enter loan amount
			System.out.print("Enter loan amount, for example, 120000.95: ");
			double loanAmount = input.nextDouble();
	

			try {
				// Create a Loan object
				Loan loan =
					new Loan(annualInterestRate, numberOfYeras, loanAmount);	
				continueInput = false;

				// Display loan date, monthly payment, and total payment
				System.out.printf("The loan was created on %s\n" + 
					"The monthly payment is %.2f\nTne total payment is %.2f\n",
					loan.getLoanDate().toString(), loan.getMonthlyPayment(),
					loan.getTotalPayment());
			}
			catch (IllegalArgumentException ex) {
				System.out.println(ex.getMessage());
			}
		} while (continueInput);
	}
}
/**************************************
*            Loan                     *
*-----------------------------------  *
* -annualInterestRate: double         *
* -numberOfYears: int                 *
* -loanAmount: double                 *
* -loanDate: java.util.Date           *
* +Loan()                             *
* +Loan(annualInterestRate: double,   *
*   numberOfYears: int,loanAmount:    *
*   double)                           *
* +getAnnualInterestRate(): double    *
* +getNumberOfYears(): int            *
* +getLoanAmount(): double            *
* +getLoanDate(): java.util.Date      *
* +setAnnualInterestRate(             *
*   annualInterestRate: double): void *
* +setNumberOfYears(                  *
*   numberOfYears: int): void         *
* +setLoanAmount(                     *
* loanAmount: double): void           *
* +getMonthlyPayment(): double        *
* +getTotalPayment(): double          *
**************************************/

public class Loan {
	private double annualInterestRate;
	private int numberOfYears;
	private double loanAmount;
	private java.util.Date loanDate;

	/** Default constructor */
	public Loan() {
		this(2.5, 1, 1000);
	}

	/** Construct a loan with specified annual interest rate, 
	    number of years, and loan amount
	 */
	public Loan(double annualInterestRate, int numberOfYears,
		double loanAmount) {
		setAnnualInterestRate(annualInterestRate);
		setNumberOfYears(numberOfYears);
		setLoanAmount(loanAmount);
		loanDate = new java.util.Date();
	}

	/** Return annualInterestRate */
	public double getAnnualInterestRate() {
		return annualInterestRate;
	}

	/** Set an new annualInterestRate */
	public void setAnnualInterestRate(double annualInterestRate) 
			throws IllegalArgumentException {
		if (annualInterestRate <= 0) {
			throw new IllegalArgumentException(
				"Annual interest rate must be greater than 0");
		}
		this.annualInterestRate = annualInterestRate;
	}

	/** Return numberOfYears */
	public int getNumberOfYears() {
		return numberOfYears;
	}

	/** Set a new numberOfYears */
	public void setNumberOfYears(int numberOfYears) 
			throws IllegalArgumentException {
		if (numberOfYears <= 0) {
			throw new IllegalArgumentException(
				"Number of years must be greater than 0");
		}
		this.numberOfYears = numberOfYears;
	}

	/** Return loanAmount */
	public double getLoanAmount() {
		return loanAmount;
	}

	/** Set a new loanAmount */
	public void setLoanAmount(double loanAmount) 
			throws IllegalArgumentException {
		if (loanAmount <= 0) {
			throw new IllegalArgumentException(
				"Loan amount must be greater than 0");
		}
		this.loanAmount = loanAmount;
	} 

	/** Find monthly payment */
	public double getMonthlyPayment() {
		double monthlyInterestRate = annualInterestRate / 1200;
		double monthlyPayment = loanAmount * monthlyInterestRate / (1 -
			(1 / Math.pow(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;
	}
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now