Ryerson University
School of Computer Science
CPS209 Term Test #1(E) : Winter 2006
Worth = 15%; Time limit = 50 minutes; No aids

Name:____________________________________ Student #: ______________________

Note: there are several meanings of the word interface, but in this exam the only meaning that applies is the java construct of which Comparable is a typical example.
A. (1 point each)
  1. Polymorphism. Consider the DataSet class that we studied as an example, where it has an instance variable of type Measurable, an interface with the method double getMeasure(). Modify the following class (in any way that works) such that we can add objects of that class to a Dataset object.
    public class Porcupine
    {
       private int quills ;
       public Porcupine(int initialQuills) { quills = initialQuills ; }
    
    
    
    }
    
  2. Inner class. Consider this top-level and inner class. Circle which variables can the method function access:
    a    b    c    d    e    f    g    h
     public class Outer
    {
       private int a ;
       public void method(final int b, int c)
       {
          int d ;
          final int e ;
          class Inner implements I
          {
              public void function()
              { 
                 ...
              }
          }
          int f ;
          final int g ;
          ...
       }
       private int h ;
    }
    
  3. Interface. Suppose classes A and B both implement interfaces X and Y. Indicate whether each of the following blocks considered in isolation will (a) work without error, (b) have a compilation error or (c) have a runtime error.
  4. Inner class: Give two reasons why one might write a class as an inner class rather than a top-level class?
  5. drawing: Write a panel's paintComponent which draws a yellow-filled rectangle (width 30, height 20) with a vertical black line (length 20) projecting from its bottom middle.
              public void paintComponent(Graphics g)
              {
                  Graphics2D g2 = (Graphics) g ;
    
                  ______________________________________
    
                  ______________________________________
    
                  ______________________________________
              }
    

B. (2 points) Write an interface Sortable that has one boolean method lessThan whose only parameter is of type Object.

C. (3 points) Write a method public static Sortable getSmallest(Sortable[] x) which returns the smallest item of the array parameter.

D. (5 points) Write a class which could be used to make a listener object for a JButton. Assume the class is an inner class which has access to outer class instance variables Porcupine porcupine and Random random which are already initialized. (See the Porcupine class defined above). Every time the button is pressed, porcupine is made to refer to a new Porcupine object with a random number of quills (from 100 to 1000, inclusive).