Cover Data Structures and Algorithms with Object-Oriented Design Patterns in Java
next up previous contents index

Exercises

  1. Specify the set of values and the set of operations provided by each of the following Java primitive data types:
    1. char,
    2. int,
    3. double, and
    4. String.
  2. What are the features of Java that facilitate the creation of user-defined data types.
  3. Explain how each of the following Java features supports polymorphism:
    1. interfaces,
    2. abstract classes, and
    3. inheritance.
  4. Suppose we define two concrete classes, A and B, both of which are derived from the AbstractObject class declared in Program gif. Furthermore, let a and b be instances of classes A and B (respectively) declared as follows:
    public class A extends AbstractObject { ... };
    public class B extends AbstractObject { ... };
    Comparable a = new A();
    Comparable b = new B();
    Give the sequence of methods called in order to evaluate a comparison such as ``a.isLT(b)''. Is the result of the comparison true or false? Explain.
  5. Consider the Int wrapper class defined in Program gif. Explain the operation of the following program fragment:
    Comparable i = new Int (5);
    Comparable j = new Int (7);
    boolean result = i.isLT (j);
  6. Let c be an instance of some concrete class derived from the AbstractContainer class given in Program gif. Explain how the statement
    System.out.println (c);
    prints the contents of the container on the standard output stream.
  7. Suppose we have a container c (i.e., an instance of some concrete class derived from the AbstractContainer class defined in Program gif) which among other things happens to contain itself. What happens when we invoke the toString method on c?
  8. Enumerations and visitors provide two ways to do the same thing--to visit one-by-one all the objects in a container. Give an implementation for the accept method of the AbstractContainer class that uses an enumeration.
  9. Is it possible to implement an enumeration using a visitor? Explain.
  10. Suppose we have a container which we know contains only instances of the Int class defined in Program gif. Design a Visitor which computes the sum of all the integers in the container.
  11. Consider the following pair of Associations:
    Comparable a = new Association (new Int (3), new Integer (4));
    Comparable b = new Association (new Int (3));
    Give the sequence of methods called in order to evaluate a comparison such as ``a.isEQ(b)''. Is the result of the comparison true or false? Explain.

next up previous contents index

Bruno Copyright © 1998 by Bruno R. Preiss, P.Eng. All rights reserved.