CSC 241- Exam 1 (October 4, 2002)                       Name:


Consider the following Balance and bankAccount class skeletons for the first five questions:

public class Balance {
 private double amt;
 public Balance() {…}
 public void add(double a) {…}
 public void subtract(double a) throws negativeBalanceException {…}
 public double balance() {…}
}
 
public class bankAccount {
 private int accountNumber;
 private String name;
 private Balance b;
 public bankAccount(int an, String n) {…}
 public void deposit (double am) {…}
 public void withdrawal (double am) throws negativeBalanceException {…}
public double currentBalance() {…}
}
 

 (20 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.amt = = y.currentBalance())
  3. bankAccount t = new bankAccount(1,"Jones");
  4. y.deposit(100.0);
  5. y = new Balance( );
  6. System.out.println("The current amount in Account is: " + y.currentBalance());
  7. System.out.println("The name of the owner of the account is: " + y.name);
  8. y.setAmount(100.0);
  9. try {y.withdrawal(x);} catch (Exception e){}
  10. try {x.subtract(10.0);} catch (Exception e){}

(10 Points) 2. True/False:

a.       String is a class in Java API.

b.      int is a primitive type in Java like float or char.

c.       java.awt is the package in Java API providing you with the graphical user interface components.

d.      Applets run in a web browser.

e.       The qualifier private put on a field or method allows it to be accessed outside of that class.

Here is the skeleton of the parkingLot class:

 

 

public class parkingLot {

 

  public static final int capacity=100;

  protected int carCount_;

  protected String name_;

 

  public parkingLot(String n) {…}

  public int  carCount () {…}

  public String  name () {…}

  public void enter () throws parkingLotOverFlowException {…}

  public void exit () throws parkingLotUnderFlowException {…}

  public class parkingLotUnderFlowException extends Exception {…}

  public class parkingLotOverFlowException extends Exception {…}

}

 

(30 Points) 3. INSIDE

I want you to add two methods to the parkingLot class.  In each case think about the following before writing any code:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

(30 Points) 4. OUTSIDE

Assume that we are writing an application; you have no access to the protected components of a parkingLot.  Answer the following:

 

a.       Write a code segment that declares a parkingLot variable p_ and binds it with a new parkingLot named "SNYGG".

 

 

 

 

 

 

 

 

 

b.      Write a code segment that increases the car count of p_ by 5.  Think about how you can increase car count in this context.

 

 

 

 

 

 

 

 

 

c.       Complete the method below that fills up a parkingLot to capacity.  Use a while loop and as long as the parkingLot is not filled to capacity perform enter operations on it.

 

                        public static void fillUp (parkingLot p) {

 

 

 

 

 

 

 

 

 

 

 

                        }