1 // This example extends the Account class to have a user-defined 2 // Exception thrown by the withdraw method. 3 // 4 5 public class AccountException 6 { // instance variables 7 String name; 8 double balance; 9 static double interestrate = .04; 10 11 // constructor method is used to initialize instance variables 12 public AccountException (String n, double b) 13 { name = n; 14 balance = b; 15 } 16 17 // accessor methods 18 public String getname() 19 { return name; } 20 21 public double getbalance() 22 { return balance; } 23 24 public int getbalancedollars() 25 { return (int)Math.floor(balance); } 26 27 public int getbalancecents() 28 { return (int)(balance - Math.floor(balance))*100 ; } 29 30 // other methods 31 public void withdraw (double amount) throws Exception 32 { 33 if ((balance - amount) > 0) 34 { balance -= amount; 35 } 36 else throw new Exception ("Insufficient Funds"); 37 } 38 39 public void deposit (double amount) 40 { balance += amount; } 41 42 // returns monthly interest and updates balance 43 public double monthactivity() 44 { double interest = (balance * interestrate)/12; 45 balance += interest; 46 return interest; 47 } 48 } 49 50