public class Balance { private double amt; public Balance() {amt=0;} public void add(double a) {amt+=a;} public void subtract(double a) throws negativeBalanceException { if (a>amt) throw new negativeBalanceException (); amt -=a; } public double balance() {return amt;} } public class bankAccount { protected int accountNumber; protected String name; protected Balance b; public bankAccount(int an, String n) { accountNumber=an; name=n; b= new Balance(); } public void deposit (double am) { b.add(am); } public void withdrawal (double am) throws negativeBalanceException { b.subtract(am); } public double currentBalance() {return b.balance();} }
(30 Points) 1.
Examine the following code segments for legal and illegal uses
of the classes Balance and bankAccount, their objects, and their components
in an application that imports them.
Assume that Balance x= new Balance();
and bankAccount y= new bankAccount(1234,"Jones");
already exist in an application.
Clearly mark each as valid or invalid, when invalid, give a brief explanation.
(10 Points) 2. Write the sequence of instructions that would create
an bankAccount object for Mohammadi with the account number 4444; deposit
1000 dollars into the account and then withdraw 10 dollars from it; and finally, display the balance left in the account (don't just print 990, get the
balance from the object).
(10 Points) 3. Assume that bankAccount x = new bankAccount(1234,"Jones");
already exists in an application and we have already
put some money in Jones's bank account object (x). Write the
instruction(s) that would remove all of this money from his account.
(20 Points) 4. Write the class interestBearingBankAccount that extends bankAccount. This new class will have:
(10 points) 5. What is the pre/post conditions for withdrawal
method of the bankAccount class.
(10 Points) 6. True/False:
(10 Points) 7. Here is the interface for Queue:
public interface Queue { public boolean empty(); public boolean full(); public Queue enqueue(Object x) throws Exception; public Queue dequeue() throws Exception; public Object peek() throws Exception; }
Suppose we are writing an application that has a queue object.
write the method emptyTheQueue() in that application that would remove
all elements of the queue (emptying it out). This method doesn't need to
display anything.
static void emptyTheQueue (Queue q) {