©2005 Felleisen, Proulx, et. al.

# 4 Methods and Equality for Self Referential Data

## 4.1 Problem

Recall the Homework 3, Problem 3.7 that dealt with the items in the
grocery store.
Design the following methods:

`same`

, which determines whether two grocery items are
the same;

`cheaperItem`

, which produces the grocery item that is
cheaper, given in terms of the unit cost. This method consumes only
one argument.

## 4.2 Problem

Recall the Homework 3, Problem 3.8 that dealt with reading lists.

Design the following
methods for these classes:

`same`

, which determines whether two lists
of books contain the same books in the same order.

`sort`

, which sorts the list of books by year.

`oldBooks`

, which produces a list of books published
before 1950.

`contains`

, which determines whether a list of books
contains a given book.

## 4.3 Problem

Recall the Homework 3, Problem 3.3 that dealt with shapes. Extend the class
hierarchy with a new subclass that represents a combination of two
shapes, layered on top of each other.

The locations of the two shapes
in the combinations are given relative to the location of the combined
shape. For instance, if the location of the dot is given as (20, 40),
and the location of the combo shape that has the dot as one of its two
layers is (30, 50), then if the combined shape is drawn, the location
of the dot relative to the graphics window origin will be (50, 90).

Design the following methods for these classes:

`same`

, which determines whether two shapes
are the same.

`move`

, which produces a new shape moved by the specified
distance (the change in the x and y coordinates).

*[Go to first, previous, next page]*