Ryerson University: School of Continuing Education

CCPS209, Midterm Test 2B, Spring 2004

Last Name                                 
First Name                                
Student Number                                 

Question Out of Mark
1 2          
2 4          
3 8          
4 4          
5 2          
6 2          
7 2          
8 6          
Total 30          
  1. Write a listener for a button. When the button is pressed, then the Rectangle object box should be moved halfway closer to the origin. That is, if it is currently at (100, 200), then it will move to (50, 100). Assume that the listener is an inner class of a JPanel which has instance variable box. Redraw the picture containing the rectangle on each press of the button.

    class MyButtonListener __________________________________________

  2. Assume the same application as in the previous question. Write a listener for a mouse. When the mouse is clicked inside the rectangle then the rectangle is redrawn twice as far away from the origin.

    class MyButtonListener _______________________________________

  3. Write a subclass of java.awt.Rectangle called FilledRectangle. FilledRectangle should have the following features.

    public class FilledRectangle_______________________________________

  4. You are told there is a method adjust(X x) which does something to object x. Later you see that the call adjust(y) works fine whether y is of type X, Y or Z. List three ways in which this is possible:



  5. Consider this top-level and inner class. Which variables can the f method access?
    Answer: _________
    public class T
       public void m(final int x, final int y)
           int a ;
           final int b ;
       class C implements I
           public void f()
        final int c ;
       private int t ;
  6. (a)What is the difference between a text field and a text area? (b) Which of these can the user type text into?

  7. Which of the following assignments compiles without error? (Deduce the logical subclass-superclass relationship.)
    Answer: _________
        Fly fly = new Fly() ;
        HorseFly horseFly = new HorseFly() ;
        fly = horseFly ;          // 1
        horseFly = fly ;          // 2
        fly = new HorseFly() ;    // 3
        horseFly = new Fly() ;    // 4
  8. Write an equals method for a class HorseFly to override the one in the Object class. Assume the HorseFly has an instance variable horses of type ArrayList, which holds a list of the horses the fly visits (in order). Two horsefly objects are equal if they visit the same horses in the same order.