CSC 241- Exam 1 (October 10, 2000)          Name:


Consider the following Balance and bankAccount classes for the first five questions:


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.

  1. if (x.amt == 10.0)

  2. if (x.balance() == y.currentBalance())

  3. y.deposit(x);

  4. y.b.add(10.0);

  5. System.out.println("The current amount in Account is: " + y.b);

  6. System.out.println("The current amount in Account is: " + y.currentBalance());

  7. y.name="Smith";

  8. try
    {y.withdrawal(10.0);}
    catch (Exception e){}

  9. try
    {Balance.subtract(10.0);}
    catch (Exception e){}

  10. x.amt +=10;


(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:

  1. the qualifiers private and protected mean the same thing in Java.
  2. If a method you call throws an exception, you must always call the method in a try/catch.
  3. java.util is the package in Java API providing you with the String and other language components.
  4. An application and an applet are the same thing in Java.
  5. static variables belong to the class rather than each object of the class.

(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) {