C212 Fall 1999

Exam 2 Solutions and Comments

Under construction: more soon.

Q1:

(a) An object of a subclass may be treated as an object of the
superclass.
True. An object of a subclass "is-a" object of a superclass.

(b) An object of a superclass may be treated as an object of the
subclass.
False. An object of a subclass can contain additional information
apart from that in the superclass.

(c) A integer can be cast to an double.
True.

(d) A boolean can be cast to an integer.
False. Casting for or to a boolean from any type is not allowed.

Q2:

interface Shape
{   
    int getX();
    int getY();
    double area();
}
class Triangle implements Shape
{   private int x, y;
    private double base, height;
    
    public Triangle(double b, double h, int xx, int yy)
    {   x = xx; y = yy; base = b; height = h;
    }
    public int getX() { return x; }
    
    public int getY() { return y; }
    
    public double area() { return .5 * base * height; }

    /* Most common errors:
    * - instead of ".5" or equivalent, such as "1/2.0",
    *   using 1/2, which is 0 (integer division).
    * - also using .5(base*height) instead of .5*(base*height).
    * - including field declarations in the interface.
    * - including non-abstract method declarations in the interface.
    * - starting the interface name with a lower-case letter (poor style).
    * - failure to define a constructor for Triangle.
    */
}
Q3:
The output numbers will respectively be 3, 3, 5, 5.

A common error was to write 1, 3, 5, 4. For this type of
problems, it is easier to draw a picture and manipulate
references according to the program. Here,

a2 = a1;  // now a1 and a2 are both pointing to the same object
a2.i = 3; // now that object, pointed to by a1 and a2, has 3.
a3 = a4;  // now a3 and a4 both refer to the same object
a3.i = 5; // now that object has 5. 

4. [20 points]

(a) Write a static void method, nullArray that takes an Object array as its argument, checks through it, and throws a PollutedException if the array contains a null element. You may assume that this exception is already defined.
ANSWER: public static void nullArray(Object[] objects) throws UnfilledException { { for (int i = 0; i< objects.length; i++) { if ( objects[i] != null) { throw new UnfilledException(); }
<
} NOTES:
Since we didn't make clear whether UnfilledExceptions are RunTimeExceptions or not, it isn't clear whether or not the method has to throw an UnfilledException. So no marks were deducted if your method did not throw an UnfilledException.

(b) Write an interface that requires such a method, to be used whenever a class must be sensitive to unfilled arrays.
Warning! This question contains an error. We asked you to write a static method, and then an interface which requires that method. This is impossible: interfaces cannot contain static methods. Oops. Anyone who wrote down that interfaces cannot contain static methods should get full marks: come and see us if you did not.

If you thought that UnFilledExceptions were RunTimeExceptions your answer 
would look like this:
public interface EmptyChecker { public static void nullArray(Object[] objects); }
<
Otherwise it will look like this:
public interface FillChecker { public static void nullArray(Object[] objects) throws PollutedException; }