Note: Most of these problems require conditionals.
Points
Build ComparablePoint, a subclass of the
Point class that includes equals(Point
other) and lessThan(Point other) methods. The
equals method should return true if the
other point is equal to the current point. The lessThan
method should return true if the current point is closer
to the origin than the other point.
Write a small test program that uses your ComparablePoints.
Points on a grid
Build GridPoint, a subclass of the Point class
that puts points on a grid, rather than anywhere on the plane. For example,
if the grid has a spacing of 1, then the point (0.7,2.3) is placed at
(1,2). If the grid has a spacing of 0.5, then the point (1.2, 2.3) is
placed at (0.5, 2.5). The constructor for your class should take the
spacing as a parameter.
Write an appropriate program to test your new class.
Squares
One of the drawbacks to the Square and
Rectangle classes we developed in the experiments is that
they do not verify that their width and height are reasonable. In
particular, they will admit negative or zero-valued widths and heights,
something we traditionally do not accept for squares and rectangles.
a. Develop a new SafeSquare class that extends the
Square class.
b. Develop a new SafeRectangle class that extends the
Rectangle class. Extend SafeRectangle to
build a NewSafeSquare class.
c. Which of a and b is a better way of building a safe square class?
Often, we use subclassing to build hierarchies of objects. For example,
we might begin with Books, subclass them to
Textbooks, Novels, and
Collections (obviously not a comprehensive list of types of
books). We could then subclass Textbooks to
Introductory and Advanced or perhaps by
discipline. Similarly, we might subclass Collections into
ShortStories and Essays or perhaps into
single author and multiple author collections. And so on and so forth.
Design a hierarchy of shapes. That is, begin with a generic
Shape class including notes as to which methods it
might provide, such as area. Then list the subclasses
and sub-subclasses of the Shape class. At each stage,
note which methods you may need to override.
[Front Door] [Introduction] [Code]
Copyright (c) 1998 Samuel A. Rebelsky. All rights reserved.
Source text last modified Tue Oct 26 11:04:35 1999.
This page generated on Tue Oct 26 15:36:49 1999 by Siteweaver.
Contact our webmaster at rebelsky@math.grin.edu