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

Exercises

  1. Specify the set of values and the set of operations provided by each of the following C# types:
    1. char,
    2. int,
    3. double, and
    4. string.
  2. What are the features of C# that facilitate the creation of user-defined data types.
  3. Explain how each of the following C# features supports polymorphism:
    1. interfaces,
    2. abstract classes,
    3. inheritance, and
    4. operator overloading.
  4. Suppose we define two concrete classes, A and B, both of which are derived from the ComparableObject class declared in Program gif. Furthermore, let a and b be instances of classes A and B (respectively) declared as follows:
    public class A : ComparableObject { ... };
    public class B : ComparableObject { ... };
    ComparableObject a = new A();
    ComparableObject b = new B();
    Give the sequence of methods called in order to evaluate a comparison such as ``a < b''. Is the result of the comparison true or false? Explain.
  5. Consider the ComparableInt32 wrapper class defined in Program gif. Explain the operation of the following program fragment:
    ComparableObject i = 5;
    ComparableObject j = 7;
    bool result = i < j;
  6. Let c be an instance of some concrete class derived from the AbstractContainer class given in Program gif. Explain how the statement
    Console.WriteLine(c);
    prints the contents of the container on the console.
  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. Enumerators 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 enumerator.
  9. Is it possible to implement an enumerator using a visitor? Explain.
  10. Suppose we have a container which we know contains only instances of the ComparableInt32 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:
    ComparableObject a = new Association(3, 4);
    ComparableObject b = new Association(3);
    Give the sequence of methods called in order to evaluate a comparison such as ``a == b''. Is the result of the comparison true or false? Explain.

next up previous contents index

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