CSC 241- Exam 1 (October 1, 1997) Name:


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

  1. In Object Oriented Design, we are concerned with objects and their behavior. Describe a technique where the specification is used in identifying objects, their states and methods.





  2. Why would we not permit an application program to just change the state variables of an object? For example, why shouldn't an applet that creates a trafficLight object be able to change the three light objects in it, may be, turning all three on?





(25 Points) 2. Provide brief responses to each of the following:

  1. What is the difference between int and Integer in Java?


  2. What role does the paint method play in an applet?


  3. What role does the action method play in an applet.


  4. What is the purpose of an applet tag in an html file.


  5. What are exceptions and what is their purpose?



(20 Points) 3. Assume the following class as an alternative to our trafficLight. In this version, we don't care what the current status of the traffic light is, we can just make it red, or make it green, or make it yellow. The class is no longer designed to have its objects control their own state changes, however, they still can only have one of their three lights on at a time. Fill in the three methods, makeGreen, makeYellow, and makeRed. Keep in mind, you can't have more than one light on at a time. Note that switchLight method in Light turns a light on, if its off, or turns it off, if its on.


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 ()
    return current_.color();
  public void makeGreen () {  /**Fill in**/





    repaint();
  }
  public void makeYellow () {  /**Fill in**/






    repaint();
  }
  public void makeRed () {  /**Fill in**/






    repaint();
  }

  public void paint(Graphics g) /** don't fill in **/
}

(20 Points) 4. Given the alternative trafficLight in 2), complete the action method for an applet that changes the traffic light whenever a button is hit (the same way as the applet for your assignment 1 did). Also, add on any new variables that you may need in order to accomplish this. Hint, now its the applet's job to control how we change the traffic light. The light sequence should be red, green, yellow, and then back to red again.


   public class traffic1 extends Applet {
	 protected Button light_switch;
     protected trafficLight t_;

     public traffic1 () {
       light_switch = new Button("Switch");
       light_switch.setForeground(Color.black);
       light_switch.setBackground(Color.lightGray);

       t_ = new trafficLight();
     }
     public void init() {     
       add(light_switch);
       add(t_);
     }

   	public boolean action(Event event, Object arg) {
      // if the Switch button is pressed, change light
      if (event.target == light_switch) {
      /** fill in **/







        return true;
      }
      return(super.action(event,arg));
   	}
   }

(20 Points) 5. 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 

Assume that t_ is a Tank object in an applet. Your task is to write the paint method for this applet. In the paint method, draw a rectangle graphically representing the tank, coloring the empty portion of the tank as red and the full portion as green. Make use of fillRect and setColor. How do you decide what portion should be filled as red and what portion as green?

  // Draw a traffic light
  public void paint(Graphics g) {
    g.setColor(Color.black);
    g.drawRect(1,1,100,100);
    /** fill in the rest of the code **/





  }