CSC 241- Exam 1 (February 25th, 2000)          Name:


(30 Points) 1. 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 

Examine the following code segements for legal and illegal uses of the class Tank, its objects, and their components. Assume that Tank t=new Tank(); already exists in an application. Clearly mark each as valid or invalid.

  1. if (t.capacity() == 5000.0) ...
  2. t.capacity()=10.0;
  3. t.content()=10.0;
  4. t.content()=t.content() + 1;
  5. t.content_=10.0;
  6. System.out.println("Tanks capacity is: " + Tank.capacity);
  7. Tank.content_=10.0;
  8. try
    {t.add(10.0);}
    catch (IllegalArgumentException e){}
    catch (TankOverFlowException e){}
  9. try
    {Tank.add(10.0);}
    catch (IllegalArgumentException e){}
    catch (TankOverFlowException e){}
  10. t.add()=10.0;

(18 Points) 2. True/False:

  1. String is a simple type in Java like float or char.
  2. int is a simple type in Java like float or char.
  3. java.awt is the package in Java API providing you with the String and other language components.
  4. An applet needs to be embedded in a .html file via an < applet > tag in order for it to run in a web browser.
  5. The qualifier public qualifies an identifer as a constant in Java.
  6. class Object is the super class for only the class Tank.

(20 Points) 3. Here is the skeleton of the trafficLight class:


public class trafficLight extends Canvas {
  protected Light red_ = new Light (true,Color.red,Color.lightGray);
  protected Light green_ = new Light (false,Color.green,Color.lightGray);;
  protected Light yellow_ = new Light (false,Color.yellow,Color.lightGray);;
  protected Light current_ = red_;
  protected int height_;
  public trafficLight ();
  public trafficLight (int h);
  public Color status ();
  public void change ();
  public void paint(Graphics g);
}

Remember that the calls to the change method turn different lights on; the sequence is from red to green, from green to yellow, and finally, from yellow to red.

Suppose, we want to write an applet that has a trafficLight. Assume that the field t is declared as a trafficLight (i.e. The declaration trafficLight t; is in the list of field declarations for the applet).

Answer the following two questions:

  1. Write the statement that would construct a trafficLight with the height 200 pixels and make t reference it.


  2. Assuming t) is red (its red light is on). What instruction(s) will change it to yellow in our applet. Hint: think about using the change method.






(17 Points) 4. Add a new method to our trafficLight class, shown above, that would force the trafficLight to become green, no matter what its current state is. Keep in mind that this method is "inside" the trafficLight class and not just using it, so you have access to all protected components. Also, understand that you don't know which of the three lights is on when this method is called and this method's job is to make sure that the green light is on and nothing else when its complete.
public void makeGreen() {








(15 Points) 5. Here is the code for the sequential representation of a Queue:


public class Queue {
  private int size_;
  private int last_=-1;
  private Object q_[];
  public Queue (int size) {
    size_ = size;
    q_=new Object[size_];
  }
  public boolean empty() {
    return last_ == -1;
  }
  public boolean full() {
    return last_ == size_-1;
  }
  public void enqueue(Object x) throws Exception {
    if (last_ == size_ - 1) throw new Exception();
    q_[++last_]=x;
  }

  public void dequeue() throws Exception {
    if (last_ == -1) throw new Exception();
    for (int i=0;i<last_;i++) q_[i]=q_[i+1]; //shift elements back by 1 cell.
  }

  public Object peek() throws Exception {
    if (last_ == -1) throw new Exception();
    return q_[0];
  }
}
  1. In English, simply write the pre-condition for enqueue.

  2. In English, simply write the pre-condition for dequeue.

  3. In English, simply write the pre-condition for peek.