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.
(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.
(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.
(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:
(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:
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.
(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; } }
(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');