CSC 241- Exam 1 (September 26, 2001)                  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 {
 private int accountNumber;
 private String name;
 private 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. y.name=new String("Smith");
  2. if (x.balance() == y.currentBalance())
  3. bankAccount t = new bankAccount(1,"Jones",0.02);
  4. y.deposit(x);
  5. y.b = new Balance( );
  6. System.out.println("The current amount in Account is: " + y.currentBalance());
  7. System.out.println("The current amount in balance is: " + x.currentBalance());
  8. y.setAmount(100.0);
  9. try {y.withdrawal(10.0);} catch (Exception e){}
  10. try {y.subtract(10.0);} catch (Exception e){}

(20 Points) 2. Write the sequence of instructions that would:

a.       Declare two bankAccount variables name them x and y. 

b.      Create a bankAccount object with your name and an account number of your choice as its arguments and bind it to both of these variables (i.e. both x and y need to reference this object).

c.       Deposit 1000 dollars into the account, once using x and a second time using y. 

d.      Withdraw 10 dollars from the account, once using x and a second time using y.  Take care of catching the exception.

e.       Display the balance left in the account with x or y. What do you expect as output?


 

 

 

 

 




(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 the money in his account.

 

 

 

 




 


Here is the skeleton of class Tank that you developed for lab#2:

 

public class Tank {

 protected double capacity_;

  protected double content_;

  public Tank()

  public Tank(double init_cap)

  public double content ()

  public double capacity ()

  public void add(double amount) throws

    TankOverFlowException, IllegalArgumentException

  public void remove(double amount) throws

    TankUnderFlowException, IllegalArgumentException

 

(10 Points) 4. Write a method percentageFull to be added to the list of existing methods of Tank class. This method provides us with the percentage of fullness for a Tank.  A double is returned which is arrived at from content divided by capacity multiplied by 100.0.



 








(10 points) 5. Assuming that we are developing an application that has the following:

Tank t= new Tank(100);

Write the instructions in this application that would loop through and add 1.0 gallon of liquid to the tank during each iteration until it is at least half full.  You must use the percentageFull method developed in the previous question. 




 

 

 

 

 

 

 (10 Points) 6. True/False:

  1. The qualifier public is more restrictive than protected in Java.
  2. If a method you call throws an exception, but you know that the exception can't occur, you don't have to put the call in a try/catch.
  3. Java.awt is the package in Java API providing you with graphics features.
  4. Applets must be embedded in an html file via an Applet tag in order for them to run in a browser.
  5. The static qualifier on a field means that it can't change.

(10 Points) 7. Here is the skeleton of class light used in assignment #1.

 
import java.awt.*;
 
public class light extends Canvas {
  protected Color onColor_=Color.red;
  protected Color offColor_=Color.lightGray;
  protected boolean on_=true;
  protected int size_=360;
 
  public light ()
  public light (int s, boolean on, Color onColor, Color offColor)
  public int lightSize ()
  public boolean on()
  public void change ()
  public void paint(Graphics g)
}

Suppose we are writing an applet.