CSC 241- Exam 1 (September 29th+30th, 1999)          Name:


Here is the class Tank:

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


(9 Points) 1. Consider the following from the Tank class:

public static final double capacity=5000.0;

Which of the following statements would be valid and which are invalid in an application program that has tanks. Clearly mark each as valid or invalid.

  1. if (Tank.capacity == 5000.0) ...
  2. tank.capacity=10.0;
  3. System.out.println("Tanks capacity is: " + Tank.capacity);

(9 Points) 2. Consider the following from the Tank class:

protected double content_;

Which of the following statements would be valid and which are invalid in an application program that has tanks. Assume, we have already constructed t via Tank t = new Tank(). Clearly mark each as valid or invalid.

  1. if (t.content_ == 5000.0) ...
  2. t.content_=10.0;
  3. System.out.println("Content of t is: " + t.content_);

(20 Points) 3. Add a method removeHalf to Tank that would remove half of the content of the tank. If the tank is empty, nothing should happen when this method is called.

  1. Is there a need for a parameter in this case? Why?

  2. Is there a circumstance where a exception should be thrown by this method? Explain.

  3. write the complete method here:






(10 Points) 4. We are developing an application that has a tank; assume that we already have a Tank object and t_ that references it. Write a code segment with a while loop and the instruction(s) that would repeatedly use the removeHalf method of t_ to reduce its content. The while loop must stop when t_'s content is less than 1.0.




(12 Points) 5. True/False:

  1. Integer is a simple type in Java.
  2. int is a simple type in Java.
  3. java.awt is a package in Java API providing you with Graphical user Interface components.
  4. Applets can run using the java command from the unix prompt like a regular application program.
  5. static final qualifies an identifer as a constant in Java.
  6. class Object is the super class in Java's class hierarchy.

(10 Points) 6. Here is the trafficLight class:


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

You should know that upon construction, a trafficLight has its red light on. 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. Answer the following two questions:

  1. Assuming 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). write the statement that would construct a trafficLight and make t reference it.

  2. Write the instruction(s) in the init method of the applet that ensure that our trafficLight starts as yellow. Remeber that this is being done in the applet, not the trafficLight class.



(10 Points) 7. Here is the code for the sequential representation of a stack:


 public class Stack {
    private int size_;
    private int top_=-1;
    private Object l_[];
    public Stack (int size) {
      size_ = size;
      l_=new Object[size];
    }
    public boolean empty() {
      return top_ == -1;
    }
    public boolean full(){ 
      return top_ == size_-1;
    }
    public Object pop() throws Exception {
      if (empty()) throw new Exception();
      top_--;
      return l_[top_+1];
    }
    public void push(Object val) throws Exception {
      if (full()) throw new Exception();
      l_[++top_]=val;
    }
  }

  1. what is the pre-condition for push?
  2. what is the pre-condition for pop?

(10 Points) 8. Consider the following code segment what will be stored in s1 when the code segment is completed:


                  String s1=new String('hello,');
                  String s2=s1;
                  s1.concat(' how are ');
                  s2.concat('you');