| Author |
interface, exception help
|
Felipe Walker
Ranch Hand
Joined: Sep 23, 2003
Posts: 41
|
|
below is my complete code for a bank account class and the measurable interface and my InsufficientFundsException class. My bankAccount class doesnt' recognize the interface or the exception class I designed. I keep getting the error below. The interface and the InsufficientFundsException class compiles with no problem. C:\Documents and Settings\felipewalker\Desktop\BankAccount\BankAccount.java:5: cannot resolve symbol symbol : class Measurable location: class BankAccount public class BankAccount implements Measurable{ ^ C:\Documents and Settings\felipewalker\Desktop\BankAccount\BankAccount.java:49: cannot resolve symbol symbol : class InsufficientFundsException location: class BankAccount throw new InsufficientFundsException("withdrawal of " + amount + " exceeds balance of " + balance); ^ 2 errors Tool completed with exit code 1 the Code ------------------ public interface Measurable { double getMeasure(); } public class InsufficientFundsException extends RuntimeException{ public InsufficientFundsException(){} public InsufficientFundsException(String reason){ super(reason); } } import javax.swing.JOptionPane; /** A bank account has a balance that can be changed by deposits and withdrawals. */ public class BankAccount implements Measurable{ /** Constructs a bank account with a zero balance. */ public BankAccount(){ balance = 0; } /** Constructs a bank account with a given balance. @param initialBalance the initial balance */ public BankAccount(double initialBalance){ if(initialBalance >= 0){ balance = initialBalance; } else JOptionPane.showMessageDialog(null,"Invalid Amount","Initial Balance can not be less than $0.00",JOptionPane.WARNING_MESSAGE); } /** Deposits money into a bank account. @param amount the amount to deposit */ public void deposit(double amount){ if (amount >= 0){ double newBalance = balance + amount; balance = newBalance; } else JOptionPane.showMessageDialog(null,"Invalid Amount","Deposit can not be less than $0.00",JOptionPane.WARNING_MESSAGE); } /** Withdraws money from a bank account. @param amount the amount to withdraw */ public void withdraw(double amount){ if (amount <= balance){ double newBalance = balance - amount; balance = newBalance; } else { balance = balance - OVERDRAFT_PENALTY; throw new InsufficientFundsException("withdrawal of " + amount + " exceeds balance of " + balance); } } /** Gets the current balance of the bank account. @return the curent balance */ public double getBalance(){ return balance; } /** Adds intrest to the balance. @param rate the current intrest rate */ public void transfer(BankAccount other, double amount){ withdraw(amount); other.deposit(amount); } public double getMeasure(){ return balance; } private double balance; final double OVERDRAFT_PENALTY = 35.00; }
|
 |
Julian Kennedy
Ranch Hand
Joined: Aug 02, 2004
Posts: 823
|
|
You don't say how you're compiling but, regardless, the problem is because your interface and Exception classes are not available on the classpath. All you probably need to do is add "." (the current directory) to the classpath. That way the compiler will be able to find the existing class files. Jules
|
 |
Nigel Browne
Ranch Hand
Joined: May 15, 2001
Posts: 673
|
|
Firstly an answer to your question. Make sure your classpath is set up correctly and includes the directory where your .java files are stored. Secondly a little tip on your bank account class. Don't use double or float to store decimal amounts. You can use this example to see what I mean The expected answer here would be 0.9 but the answer obtained is 0.8999999999999999. What should you use instead ? BigDecimal would be my recommendation.
|
 |
Julian Kennedy
Ranch Hand
Joined: Aug 02, 2004
Posts: 823
|
|
Actually long or even int should be fine unless you're dealing with huge amounts or lots of decimal places (e.g. currency conversion). Just divide by 100 to display cents or whatever. Jules
|
 |
Felipe Walker
Ranch Hand
Joined: Sep 23, 2003
Posts: 41
|
|
Thanks for all the help, that solved my issue
|
 |
 |
|
|
subject: interface, exception help
|
|
|