Roger L. Kraft

CS 59000-03 - Programming Assignment 2

This assignment uses ideas from Sections 4.3-4.5 and 5-1-5.2. This assignment is a combination of Exercises 4.8 and 4.10 from the end of Chapter 4 in our textbook. This assignment is due Tuesday, November 12.

Download this zip file.

First, write a class RectangleComparator.java that implements the Comparator<T> interface and defines a total ordering on Rectangle2D.Double objects. See Exercise 4.10, on page 171 of the textbook, for details. In the zip file there is a program TestRectangleComparator.java that tests your comparator and a file TestRectangleComparator.output.txt that shows what the results of the test should look like.

For the second part of this assignment, you will write a comparator as an anonymous inner class. Open the file TestRectangleComparator_ver2.java from the zip file. In this file you should define an anonymous inner class that implements another comparator for Rectangle2D.Double objects. The new comparator should compare Rectangle2D.Double objects by their "total dimension", i.e., compare rectangles by comparing their x + y + width + height. When you have the comparator written, the TestRectangleComparator_ver2.java program should compile and run and produce output equal to the file TestRectangleComparator_ver2.output.txt.

After you have the comparators written and tested, define the following interface type in the file Filter.java.

      public interface Filter<T>
      {
         boolean accept(T e);
      }

The accept() method in this interface is a function that "decides" whether to keep (filter in) or reject (filter out) objects of type T. A Filter object is used to remove (filter out) items from some collection (of objects of type T).

Write a class FilterRectanglesBySize.java that implements the Filter<T> interface. The class should have a constructor that takes two Rectangle2D.Double parameters

      public FilterRectanglesBySize(Rectangle2D.Double low, Rectangle2D.Double high)

The filter created by this constructor should return true when the object to filter is between the low and high rectangles, as determined by an instance of RectangleComparator. (So we are "keeping" rectangles that are between low and high and rejecting all other rectangles.) In the zip file there is a program TestFilterRectanglesBySize.java that will test your implementation of FilterRectanglesBySize.java. The output of the test program should agree with the file TestFilterRectanglesBySize.output.txt

Turn in a zip file called CS507Hw2Surname.zip (where Surname is your last name) containing your source code files RectangleComparator.java, TestRectangleComparator_ver2.java, Filter.java, and FilterRectanglesBySize.java.

This assignment is due Tuesday, November 12.