(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.
(18 Points) 2. True/False:
(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:
(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]; } }