Ryerson University: School of Continuing Education

CCPS209, Midterm Test 2 Solutions, Spring 2004


Last Name                                 
First Name                                
Student Number                                 


Instructions
Question Out of Mark
1 12          
2 8          
3 10          
Total 30          
  1. Fill in the following application so that the user can draw an ellipse on the panel using a mouse. Here are some details of how it should work:
    /**
        A specialized JPanel with a mouse listener that allows the user
        to draw an ellipse with press and release events.  
    */
    public class EllipsePanel extends JPanel
    {
        /**
           (x1,y1) and (x2,y2) are diagonal corners of the bounding box
           of the ellipse.
        */
        private int x1, x2, y1, y2 ;
    
        public EllipsePanel()
        {
            setPreferredSize(new Dimension(300, 500)) ;
            /**
                Setting up some initial values for a default ellipse.
            */
            x1 = getWidth() / 2 ;
            x1 = 200 ;
            x2 = x1 + 20 ;
            y1 = getHeight() / 2 ;
            y1 = 100 ;
            y2 = y1 + 30 ;
    
            /**
                Inner class for a mouse listener (described above).
            */
            class MyListener implements MouseListener {
    
                public void mousePressed(MouseEvent e)
                {
    
                    x1 = e.getX() ;
                    y1 = e.getY() ;
    
                }
                public void mouseReleased(MouseEvent e)
                {
    
                    x2 = e.getX() ;
                    y2 = e.getY() ;
                    repaint() ;
    
                }
                public void mouseEntered(MouseEvent e){}
                public void mouseExited(MouseEvent e){}
                public void mouseClicked(MouseEvent e){}
            }
            addMouseListener(new MyListener()) ;
        }
        /**
           Makes and draws and ellipse.
        */
        public void paintComponent(Graphics g)
        {
            super.paintComponent(g) ;
            Graphics2D g2 = (Graphics2D) g ;
    
            Ellipse ellipse = new Ellipse(x1, y1, x2, y2) ;
            ellipse.draw(g2) ;
    
        }
    }
    /**
         Subclass of Ellipse2D.Double.  Ellipse has a different
         constructor and a special draw method described above.
    */
    public class Ellipse extends Ellipse2D.Double
    {
        /**
           Translates the two diagonal corners into instance
           variables of Ellipse2D.Double.  Note: x, y, width,
           height are public in Ellipse2D.Double.
        */
        public Ellipse(int x1, int y1, int x2, int y2)
        {
    
    	x = Math.min(x1, x2) ;
    	y = Math.min(y1, y2) ;
    	width = Math.abs(x1 - x2) ;
    	height = Math.abs(y1 - y2) ;
    
        }
    
        /**
           Uses the graphics context g2 to draw a filled ellipse.
           The fill color is blue or yellow depending on its position.
        */
        public void draw(Graphics2D g2)
        {
    
    	if (x > y)
    	    g2.setColor(Color.blue) ;
    	else
    	    g2.setColor(Color.yellow) ;
    
    	g2.fill(this) ;
    
        }
    }
    
    
    
  2. Short answer questions.
    In the above application, consider the effect of adding 
    following line to the paintComponent method.   
    g2.draw(new Ellipse(0, 0, 100, 100)) ;
    (a) Would it compile without error?  Explain your yes or no answer.
    
    Yes, it would compile without error because Ellipse is a 
    subclass of Ellipse2D.Double which can be used inside g2.draw().
    
    Consider this top-level and inner class.  Which variables can the 
    
    f method access?  x, b, and t
    
    
    public class T
    {
       public void m(final int x, int y)
       {
           int a ;
           final int b ;
    
           class C implements I
           {
              public void f()
              {
                  ...
              }
           }
           final int c ;
       }   
       private int t ;
    }
    
    
    
    Why does the ActionListener interface have only one method, 
    whereas the MouseListener has five methods?
    
    The ActionListener is for simple things like a button press
    or a timer event, whereas a MouseListener can respond to several
    things that a mouse can do.
    
    In the following pairs of classes, circle which of each pair 
    should be the superclass (while the other would be the subclass).
    
    Employee, Manager    Employee
    Polygon, Triangle Polygon
    GraduateStudent, Student Student
    Person, Student Person
    Employee, GraduateStudent   Employee
    BankAccount, CheckingAccount   BankAccount
    Vehicle, Car    Vehicle
    Vehicle, Minivan    Vehicle
    Car, Minivan   Car
    Truck, Vehicle   Vehicle
    
    
    
    What is the difference between radio buttons and check boxes?
    
    Radio buttons can be grouped so that only one can be on at 
    a time.  Check boxes are intended to be used where several boxes
    can be checked at once.
    
    What is wrong with the following loop?
    
    The first index is 0 in an array, so the following loop
    causes an ArrayIndexOutOfBounds exception.
    
      double[] data = new double[10] ;
      for (int i = 1 ; i <= 10 ; i++) data[i] = i * i ;
    
    
    Write an equals method for a class Person to override the one in the Object class. Assume the Person has instance variables name and height (String and int, respectively). Two person objects are equal if they have the same name and the same height.
    
          public boolean equals(Object other)
          {
              if (! (other instanceof Person) ) return false ;
              Person person = (Person) other ;
              return name.equals(person.name) 
                     && height == person.height ;          
          }
    
    
  3. Write a class Bank that contains an array list of BankAccount objects. Support the following methods, where an account number is simply an index into the array list. (If you don't know the interface for the BankAccount class, ask the instructor to put it on the board.)
    public void addAccount(double initialBalance)
    public double getBalance(int account)
    public void deposit(int account, double amount)
    public class Bank
    {
    
         private ArrayList banks ;
    
         public Bank()
         {
             banks = new ArrayList() ;
         }
         public void addAccount(double initialBalance) 
         {
             banks.add(new BankAccount(initialBalance)) ;
         }
         public double getBalance(int account) 
         {
             return ((BankAccount) banks.get(account)).getBalance() ;
         }
         public void deposit(int account, double amount) 
         {
             ((BankAccount) banks.get(account)).deposit(amount) ;
         }
    
    }