Ryerson University: School of Continuing Education

CCPS209, Midterm Test 2B Solutions, Spring 2004


Last Name                                 
First Name                                
Student Number                                 


Instructions
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 implements ActionListener

    
         public void actionPerformed(ActionEvent e)
         {
             box.setLocation(box.x / 2, box.y / 2) ;
             repaint() ;
         }
    
    
  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 implements MouseListener

    
         public void mouseClicked(MouseEvent e)
         {
             int x = e.getX() ;
             int y = e.getY() ;
             // if (box.contains(x, y) )        is simpler!
             if (x >= box.x && x <= box.x + box.width
                    && y >= box.y && y <= box.y + box.height)
             {
                  box.setLocation(2 * box.x, 2 * box.y) ;
                  repaint() ;
             }
         }
         public void mousePressed(MouseEvent e) {}
         public void mouseReleasedMouseEvent e) {}
         public void mouseEntered(MouseEvent e) {}
         public void mouseExited(MouseEvent e) {}
    
    









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

    public class FilledRectangle extends Rectangle

    
          private Color color ;
          public static final int SIDE = 100 ;
    
          public FilledRectangle()
          {
              super(0, 0, SIDE, SIDE) ;
              color = Color.red ;
          }
    
          public FilledRectangle(int x, int y, int w, int h, Color c )
          {
              super(x, y, w, h) ;
              color = c ;
          }
    
          public void draw(Graphics2D g2)
          {
              g2.setColor(color) ;
              g2.fill(this) ;
          }
    
          public void setColor(Color c)
          {
              color = c ;
          }
    
    
  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:
    1. X could be an interface which Y and Z implement.
    2. X could be a superclass and Y, Z could be subclasses.
    3. There could be two other methods: adjust(Y y) and adjust(Z z).
  5. Consider this top-level and inner class. Which variables can the f method access?
    Answer: c, t
    
    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?
    1. Text field has one line whereas text area has many.
    2. Both can allow the user to type text (there is a setEditable(boolean) method).
  7. Which of the following assignments compiles without error? (Deduce the logical subclass-superclass relationship.)
    Answer: 1 and 3
        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.
         public boolean equals(Object other)
         {
             if (! (other instanceof HorseFly) ) return false ;
             HorseFly horseFly = (HorseFly) other ;
    
    	 if (horses.size() != horseFly.horses.size() ) return false ;
    
             for (int i = 0 ; i < horses.size() ; i++)
    	 {
    	     Object horse1 = horses.get(i) ;
    	     Object horse2 = horseFly.horses.get(i) ;
    	     if (! (horse1.equals(horse2)) ) return false ;
    	 }
    
    	 return true ;
         }
    
    Here is a shorter solution, based on the fact that the ArrayList class has a good equals method.
         public boolean equals(Object other)
         {
             if (! (other instanceof HorseFly) ) return false ;
             HorseFly horseFly = (HorseFly) other ;
    
    	 return horses.equal(horseFly.horses) ; 
         }