Sets and Filters Exercises

COM 1201 – Spring 2003 – Jeff Raab

Introduction

In this exercise set you will answer questions about abstraction topics and write code that uses and implements abstractions.  You must follow the course guidelines for type- or handwritten answers, diagrams, and writing code, as posted on the course website.

Programming exercises

Perform these programming exercises before the written exercises below.

To complete this portion of the exercise set you must download the files from the course website.  These files represent a complete CodeWarrior project that will work as-is in the computer labs in Cullinane Hall.  If you wish to work at home, elsewhere, or using an IDE other than CodeWarrior, it is your responsibility to configure the software.

1.         Write a class named BoundedSetAdapter that implements the given IBoundedSet interface and enforces the invariant that the size of the bounded set must never grow larger than its maximum size.  The maximum size must be part of the data definition for the class, and must be set to a maximum size provided as input to the constructor.  A precondition for the constructor must be that the given maximum size be greater than 0.

            Your class must use the Proxy design pattern to adapt the methods in the IBoundedSet interface to an object of type ISet.  The ISet used to hold the elements in the bounded set must be part of the data definition for the class, and must be assigned to an ISet object provided as input to the constructor. 

            If an invariant is violated in the constructor, you must throw a RuntimeException with an error message describing what went wrong.

            If an invariant will be violated by performing the add method or the union method in its entirety, you must throw a RuntimeException with an appropriate error message and make no changes to the bounded set.  It is not OK to do part of the work of the add or union method, changing the bounded set in some fashion, and then throw an exception.  Invariants must be ensured before the state of the bounded set is changed.

            In the class named SetsAndFiltersTests, write tests for all of the methods of your class, covering as many different cases as you can identify.  Since you will need to pass an ISet object to the constructor for each BoundedSetAdapter you create, a class named JavaSet has been given that implements the ISet interface.

            Section 2.5 of the textbook describes this kind of bounded set in more detail.

The SetsAndFiltersTests class defines an example filter and a method that applies that filter to the shared set of shapes.  Use this example as a guide when performing the remaining programming exercises.

2.         Define a filter that removes all shapes of type Pie from the set.  Write a method that applies this filter to the shared set of shapes.  Run the application and test your filter in all possible cases.  Make changes to the filter as needed until you are sure it is correct.

3.         Define a filter that changes the color for each shape of type IColorable to a random color.  The following method returns a random color and should be used in your filter:

     randomColor()

 

            Write a method that applies this filter to the shared set of shapes.  Run the application and test your filter in all possible cases.  Make changes to the filter as needed until you are sure it is correct.

4.         Define a filter that sets the color of each shape of type Circle to white.  The color white is a constant in the Java language referred to as:

     Color.white

 

            Write a method that applies this filter to the shared set of shapes.  Run the application and test your filter in all possible cases.  Make changes to the filter as needed until you are sure it is correct.

5.         Write a method that that changes the color for each shape whose color can possibly be changed, to a random color.  You must reuse the filter from the exercise above that changes the color of each IColorable shape to a random color, as well as define a new filter that changes the colors of other shapes to random colors.  In other words, you must define a method that uses two filters: one you defined for problem 3, and one you define to do the rest of the work for this problem.

Written exercises

You should only perform these exercises after the programming exercises are completed.

6.         Explain why the code in the SetOfAShape class must typecast in the item method, but not in the methods add and remove.

7.         Explain why the filter you wrote that removes all pies, when applied to a set of shapes, doesn’t remove any squares.

8.         Explain why the filter you wrote that randomly changes the colors of IColorable objects, when applied to a set of shapes, doesn’t change the colors of any circles.

9.         Explain why it’s not possible, using the given classes as they are, to write a filter that randomly changes the colors of all shapes.  Briefly describe what changes would have to be made to what given classes to make such a filter possible.

10.       Describe what you would have to do, without changing any of the given classes, to be able to create a shape of type Pie whose color could be changed randomly by at least one of the filters you wrote. 

            (Note that exercise 9 asks you what changes would have to be made to the given classes, and exercise 10 asks you what would be done without making changes to the given classes.)

Submission

You must submit the following written and/or printed material:

           Type- or handwritten answers to written problems

           Printouts of classes and tests you wrote for programming problems

 

You must submit the following electronic material:

           A folder containing all of the provided files in the exercise

           Classes and tests you wrote for programming problems

 

Details for electronic submission are given on the course website.