Can anyone help me see where I am missing this? I am trying to get the loan class called to the main method and check to see if my exception handling is right. I am very new to this, week six to be exact, and can use all the constructive help possible. Thanks ahead of time!
package loan;
import java.util.Scanner;
public class Loan {
public static void main(String[] args) {
try {
Loan(2.5, 0, 1000.00);
}
catch (IllegalArgumentException ex) {
ex.getMessage();
}
}
Scanner input = new Scanner(System.in);
double annualInterestRate;
int numberOfYears;
double loanAmount;
private java.util.Date loanDate;
public Loan() {
this(2.5, 0, 1000);
}
public Loan(double annualInterestRate, int numberOfYears, double loanAmount) {
this.annualInterestRate = annualInterestRate;
this.numberOfYears = numberOfYears;
this.loanAmount = loanAmount;
loanDate = new java.util.Date();
}
public double getAnnualInterestRate() {
if (annualInterestRate > 0)
return annualInterestRate;
else
throw new IllegalArgumentException("Interest rate cannot be zero or negative.");
}
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
public int getNumberOfYears() {
if (numberOfYears > 0)
return numberOfYears;
else
throw new IllegalArgumentException("Number of years cannot be zero");
}
public void setNumberOfYears(int numberOfYears) {
this.numberOfYears = numberOfYears;
}
public double getLoanAmount() {
if (loanAmount > 0)
return loanAmount;
else
throw new IllegalArgumentException("Loan amount cannot be zero");
}
public void setLoanAmount(double loanAmount) {
this.loanAmount = loanAmount;
}
public double getMonthlyPayment() {
double monthlyInterestRate = annualInterestRate/1200;
double monthlyPayment = loanAmount * monthlyInterestRate / (1 - (Math.pow(1/(1 + monthlyInterestRate), numberOfYears *12)));
return monthlyPayment;
}
public double getTotalPayment() {
double totalPayment = getMonthlyPayment() * numberOfYears * 12;
return totalPayment;
}
public java.util.Date getLoanDate() {
return loanDate;
}
}
Your code tries to call a static method called Loan, but it looks like you want to create a new Loan object. You do that by using the keyword new and giving the correct parameters to the constructor: