CS 10 Assignment 4, Fall 2008
Implementation notes
Part 1 - class FigurePrinter
- A user constructs a FigurePrinter object by specifying
a character symbol to use for printing the figure, and an integer
specifying the number of spaces that should precede the figure when it is printed. Of course
your class must provide ways to remember this information.
- A default FigurePrinter object uses
'*'
as the symbol, and with an offset of 0.
- There are "get" and "set" methods for both of these variables.
print()
prints a single symbol, preceded by the offset number of spaces.
And printAt(int lines)
advances the specified number of lines before printing.
equals(Object other)
overrides the equals method of class Object (see the text).
compareTo(FigurePrinter other)
is the method that implements
interface Comparable<E>
(where E
is FigurePrinter in this
case). Look up that interface to know what it means. Make the objects comparable based on
the offset value.
- Our solution also includes two utility methods,
shift()
and spaces(int n)
.
Their access is protected
instead of private
, allowing subclasses to use them.
These methods are not required.
Part 2a - class RectanglePrinter extends FigurePrinter
- Store a width and a height. The no-argument constructor sets both of those values to 2, and
sets both superclass variables to their default values. An alternative constructor sets
all four of these variables, and insures that width and height are never less than 2.
- Include "set" and "get" methods for the width and the height. Set them to 2 if a smaller
value is passed to the set methods.
- Override
print()
to print rectangles as outlines formed by the print symbol.
See the sample outputs to know how the rectangles should look.
- Override
equals(Object other)
to account for width and height differences.
- Simply inherit the other FigurePrinter methods.
Part 2b - class TrianglePrinter extends FigurePrinter
- Store a width. The no-argument constructor sets it to 3, and
sets both superclass variables to their default values. An alternative constructor sets
all three of these variables. This alternative constructor insures that width is never less than 3, and
that it is always an even multiple of 3 - it rounds the width to the nearest even
multiple of 3 if a different value is passed to it.
- Include "set" and "get" methods for the width, and have
setWidth
insure
even multiples of 3 (like the constructor does).
- Override
print()
to print triangle outlines.
- Override
equals(Object other)
as appropriate, and inherit the other FigurePrinter
methods like you did for RectanglePrinter.
- Notice in the sample runs that triangles absolutely depend on widths that are multiples of 3,
but they also depend to a lesser degree on multiples of 6. That is, notice that sometimes it is
necessary to draw a single symbol on the bottom, but sometimes it is not necessary.