Data Structures and Algorithms with Object-Oriented Design Patterns in Ruby
next up previous index

Exercises

  1. Specify the set of values and the set of operations provided by each of the following Ruby types:
    1. Fixnum,
    2. Float, and
    3. String.
  2. What are the features of Ruby that facilitate the creation of user-defined data types.
  3. Explain how each of the following Ruby features supports polymorphism:
    1. classes,
    2. inheritance, and
    3. mix-in  modules like ::Comparable and ::Enumerable.
  4. Suppose we define two concrete classes, A and B, both of which are derived from the abstract AbstractObject class declared in Program gif. Furthermore, let a and b be instances of classes A and B (respectively) declared as follows:
    class A < Abstract Object
        ...
    end
    class B < AbstractObject
        ...
    end
    a = A.new
    b = B.new
    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. Let c be an instance of some concrete class derived from the Container class given in Program gif. Explain how the statement
    print c.to_s, "\n"
    prints the contents of the container on the console.
  6. Suppose we have a container c (i.e., an instance of some concrete class derived from the Container class defined in Program gif) which among other things happens to contain itself. What happens when we invoke the to_s method on c?
  7. Iterators 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 Container class that uses an iterator.
  8. Is it possible to implement an iterator using a visitor? Explain.
  9. Suppose we have a container which we know contains only Fixnums. Design a Visitor which computes the sum of all the Fixnums in the container.
  10. Consider the following pair of Associations:
    a = Association.new(3, 4)
    b = Association.new(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 index

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