©2010 Felleisen, Proulx, et. al.

6  Abstracting with Function Objects

Portfolio Problems

  1. Include the solution of the Lab 6 in your portfolio.

  2. Add a few more examples of tests in the Examples class.

  3. Define a class ImageSmallerThanAndGivenKind that implements the ISelectImageFile interface with a method that selects image files that are small and of the given kind. Allow the user to decide how small should the images be (measuring the size as the number of pixels in the image) and allowing the user to choose the kind of images that should be selected. (All selected images must be of the same kind.)

    Test your class definition on several examples before you use it in your allSuch, anySuch, and filter methods.

  4. Add test cases that will test the methods allSuch, anySuch, and filter with several instances of the ImageSmallerThanAndGivenKind predicate.

Pair Programming Assignment

6.1  Problem

During the lectures we have designed the method filter for a list of Books, selecting books written by the given author and books published in the given year.

  1. Define the class Book and the classes that represent a list of Books. You can start with the classes posted on the lecture notes.

  2. Define the following interface in your project:

    // interface to represent a method compare for books
    public interface ICompareBooks{
      
      // does b1 come before b2 in this ordering?
      public boolean compare(Book b1, Book b2);
    }
    

  3. Define three classes that implement this interface, ordering the books by the length of the book title (class BookOrderByTitleLength), author’s names (class BookOrderByAutor), and one more criterion of your choice.

  4. Design the method sort for the classes that represent a list of Books that uses an instance of a class that implements the ICompareBooks interface to define the appropriate ordering of the books and produces the list in the correctly sorted order.

  5. Design the method isSorted for the classes that represent a list of Books that uses an instance of a class that implements the ICompareBooks interface to determine whether a list of Books is sorted correctly.

    Make sure your tests use all three ways of comparing books.

    Note: Remember the one task one method rule.

    Note: This is just a slight modification of the methods you have already designed.

6.2  Problem

You will work with a binary search tree that represents a collection of Book objects. It should be very similar to the binary search trees that had only integer data.

The class diagram on the next page should help you.

  1. Define the classes that represent a binary search tree of Book objects as shown in the class diagram above.

  2. Define the method insert that produces a new binary search tree by inserting a new Book into the binary search tree, using the ICompareBooks already defined for this tree.

    Last modified: Wednesday, February 17th, 2010 12:28:52pm