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