CS 5 Fall 2009
Short Assignment #10
Due Monday, October 26

The SetOfRects class (in the SetOfRects.java file) that we saw in lecture has a particular behavior when we try to add a rectangle to a full set: it simply doesn't add the rectangle.

Several other behaviors are possible. In this assignment, you will change this class to implement a different behavior. In particular, when you try to add a rectangle to a full set, the rectangle is supposed to replace the "least recently added" rectangle, i.e., the new rectangle replaces the rectangle that has been in the set for the longest time. The rectangles do not have to be drawn in the order in which they are added to set; you may draw them in the order in which they appear in the array.

You can use this different implementation of SetOfRects with the same programs (Rect.java and DrawRects.java) that we used in lecture. For example, here's a set of rectangles that has just become full:

The rectangles are added in "row-major" order, i.e., top to bottom, and then left to right within each row. So the upper left rectangle is the first one added. When I drag out another rectangle, here's what we get:

And after adding another rectangle:

Your mission is to edit the SetOfRects.java file to produce this behavior. The changes that you need to make are few.

Hand in a printout of your SetOfRects class, along with at least three screenshots that show that your implementation works in the context of the DrawRects applet. Your printouts do not have to be in color. The only source code you are to hand in is your SetOfRects class. Do not hand in the Rect class or the DrawRects class. Just use the ones from lecture.


Back to Short Assignments
Thomas H. Cormen <thc@cs.dartmouth.edu>
Last modified: Mon Oct 19 22:45:26 2009