Suppose we add the following method definition to our
Bank class.
public class Bank {
// : (definitions of other methods and variables)
public void remove(int i) throws NoSuchElementException {
if(i < 0) {
throw new NoSuchElementException("negative index");
} else if(i >= num_accts) {
throw new NoSuchElementException("index too large");
} else {
num_accts--;
accts[i] = num_accts;
}
}
}
Suppose we have a Bank variable named first. How would
you use this method to remove the third account from first? Be
sure to handle any exceptions that the method might initiate; for full
credit, your code display the message
associated with the exception
(like ``negative index'').
public static void run() {
Bank first = new Bank();
// : (code that inserts accounts into the bank)
}
public static void run() {
Bank first = new Bank();
// : (code that inserts accounts into the bank)
try {
first.remove(2);
} catch(NoSuchElementException e) {
IO.println(e.getMessage());
}
}