COM1204 Summer 2002 -- Midterm Exam

Professor Futrelle, Northeastern U., College of Computer & Information Sciences

RED EXAM

This is a closed-book, closed-notes, no-calculators exam. Exam given, Monday, July 29th. Write all answers in your exam book. Do not turn in this question sheet. You should keep it and bring it with you when I go over the exam in class. In the questions below, methods are often written as, for example, boo(). This is a shorthand and does not imply that the methods have no arguments. They may or may not.


1. Write the source code for an interface A that describes two methods. The first is getVal() that returns an int. The second is describe() that returns a String.

Then write the source code for an adapter, an abstract class AAdapter, that implements A, returning 0 for getVal() and the empty string for describe().

Then write the source code for B that subclasses AAdapter and defines getVal() to return the value of an instance variable val.

Why is B allowed to only define getVal()? What is this redefinition process called? What advantages does this kind of OO design have, involving an interface, an adapter and a class?

2. Write the source code for a class that has an int field n which is not directly accessible, but set by invoking setN(int). Arrange your code so that if a user attempts to set n to a negative number it is set to 0. Otherwise, it is set to the value requested.

What OO design principles are involved in this?

3. A factory can produce instances of a number of distinct classes. Assume that the class Sup has subclasses Sub1 and Sub2. Further assume that the Factory class has a procedure produce(int) that returns an instance of either Sub1 or Sub2, depending on its int argument being 1 or 2, respectively. Finally, assume that a programmer attempts to write the following statement using an instance of Factory named factory:

Sup s = factory.produce(2);

The assignment statement above may or may not be correct. In considering the two alternatives below, describe an easy-to-understand example (no source code details needed) that helps to clarify your answer.

4.Write a trivial factory method that returns an instance of a subclass chosen from two subclasses of a class. You needn't define any of the classes, just the method declaration with a body. Make up your own, easy-to-understand names for the classes.

5.Explain the difference between simple overloading and the more object-oriented concept of overriding. Do this in the following way:

  1. For a class ClassOne, show how overloading can occur even within a single class definition. Define two such methods, exampleMeth(), with no bodies.
  2. For a subclass, ClassOneOne, show how exampleMeth() could be overridden (not overloaded).
  3. Using the correct OO terminology, explain how the appropriate code to execute is chosen for the overridden method. Is this done at compile time or run time, and how and why?

6. In a class file, it is often useful to write a main() method that tests some methods defined in that class. Assuming that a class OneClass has a method oneMeth() that returns a String, write out the source code for a main() method that writes to System.out, the string returned by oneMeth(). You only need to write the definition of the main() method, not any other part of the class definition.

7. Assume that three classes, App, Crunch and Munch are designed so that they operate in time as follows:

  1. App calls the method canYouCrunch() on Crunch, which returns a boolean.
  2. Then App calls the method crunch() on Crunch.
  3. After receiving a crunch() request, Crunch calls the method munch() on Munch, which returns a Munch object.
  4. Crunch then does further processing and returns a Crunch object to its caller.

Draw a sequence diagram illustrating the above sequence. Then explain in which class source code the canYouCrunch() method is defined and in which class source code the call to canYouCrunch() appears.


For your entertainment -- the Gulf Stream. For more information and interesting diagrams see:
http://kingfish.coastal.edu/gulfstream/

Return to the COM1204 home page