Ryerson University: School of Continuing Education

CCPS209, Midterm Test 2, 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 ___________________________ {
    
                public void mousePressed(MouseEvent e)
                {
                    ____________________
    
                    ____________________
                }
                public void mouseReleased(MouseEvent e)
                {
                    ____________________
    
                    ____________________
    
                    ____________________
                }
                public void mouseEntered(MouseEvent e){}
                public void mouseExited(MouseEvent e){}
                public void mouseClicked(MouseEvent e){}
            }
            addMouseListener(____________________________) ;
        }
        /**
           Makes and draws and ellipse.
        */
        public void paintComponent(Graphics g)
        {
            super.paintComponent(g) ;
            Graphics2D g2 = (Graphics2D) g ;
    
            _______________________________________________
    
            _______________________________________________
        }
    }
    /**
         Subclass of Ellipse2D.Double.  Ellipse has a different
         constructor and a special draw method described above.
    */
    public class Ellipse __________________________________
    {
        /**
           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)
        {
            ______________________________________________
    
            ______________________________________________
    
            ______________________________________________
    
            ______________________________________________
        }
    
        /**
           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)
        {
            ______________________________________________
    
            ______________________________________________
    
            ______________________________________________
    
            ______________________________________________
        }
    }
    
    
    
  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.
    
    ___________________________________________________________
    
    Consider this top-level and inner class.  Which variables can the 
    
    f method access?  ____________________________(answer goes here).
    
    
    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?
    
    ______________________________________________________________
    
    In the following pairs of classes, circle which of each pair 
    should be the superclass (while the other would be the subclass).
    
    Employee, Manager
    Polygon, Triangle
    GraduateStudent, Student
    Person, Student
    Employee, GraduateStudent
    BankAccount, CheckingAccount
    Vehicle, Car
    Vehicle, Minivan
    Car, Minivan
    Truck, Vehicle
    
    
    
    What is the difference between radio buttons and check boxes?
    
    ___________________________________________________________________
    
    What is wrong with the following loop?
    
    ___________________________________________________________________
    
      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.
    ___________________________________________________________________
    
    ___________________________________________________________________
    
    ___________________________________________________________________
    
    ___________________________________________________________________
    
    ___________________________________________________________________
    
    
    
  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 void getBalance(int account)
    public void deposit(int account, double amount)
    
    public class Bank
    {