CSC 241- Exam 1 (February 26, 1998) Name:


(10 Points) 1. Provide brief responses to each of the following:

  1. This question deals with your understanding of the role of problem specification in class design. The following sentence is from the specification for the trafficLight class:"A traffic light has three lights where only one of the lights may be on at any point in time". What state variables are warranted for trafficLight based on this sentence.





  2. Describe the difference between components of a class that are identified as public, protected, or private.






(9 Points) 2. Fill in the words:

  1. String objects hold ___________ and we can manipulate them using methods like ___________ and __________.
  2. we can ________ using Graphics objects with methods like ___________ and ___________.
  3. Java ___________ can be embedded in HTML files and accessed on the __________.

(10 Points) 3. Here is the trafficLight class without the protected components:


public class trafficLight extends Canvas {
  public trafficLight ();
  public trafficLight (int h);
  public Color status ();
  public void change ();
  public void paint(Graphics g);
}

Assume that we have four trafficLight objects( northT_,southT_, westT_, and eastT_) as the state of a StreetCorner class. The North and South bound traffic lights change the same, as do the west and east bound traffic lights. Assume that North and South bound traffic lights are red to start and west and east bound traffic lights are green. Write the method changeAll for StreetCorner that would change the four traffic lights harmoniously. Understand that either pair of lights when green need to change to yellow when this method is called, but the other pair should stay as red, any other case all four lights must change.










(10 Points) 4. Here is the class Tank:


public class Tank {
  public static final int capacity=5000;
  protected int content_;
  public Tank()
  public Tank(int init_cont)  throws
    TankOverFlowException, IllegalArgumentException
  public int content () 
  public void add(int amount) throws
    TankOverFlowException, IllegalArgumentException 
  public void remove(int amount) throws
    TankUnderFlowException, IllegalArgumentException 

Write a method ratio for the Tank class that returns the content over capacity ratio of a tank. The return value would have to be a float.










(10 Points) 5. Write a code segment for an application with two Tank objects (t1_ and t2_). In this code segment, compare their content over capacity ratios using their ratio methods and output an appropriate massege indicating "t1 has the larger ratio" or "t2 has the larger ratio" or "t1 and t2 have the same ratio".










(12 Points) 6. Here is the code for the sequential representation of a stack:


public class Stack {
  private int size;
  private int top=-1;
  private int l[];
  public Stack (int init_size) {
    size = init_size;
    l=new int[size];
  }
  public boolean empty() {
    return top == -1;
  }
  public boolean full() {
    return top == size-1;
  }
  public int pop() {
    top--;
    return l[top+1];
  }
  public void push(int val) {
    l[++top]=val;
  }
}