The goal of this assignment is to practice writing classes to implement an interface. There are two parts. You may be able to finish part 1 during the lab block.
As usual, make a new directory for this project and cd into it.
In the Set class you wrote for the previous assignment, you had choice--you could implement it using a character array or using a String. All of you chose to use a character array. To demonstrate that either would work, you will now re-implement set using a String instead. Do the following:
Set.java
and SetDriver.java
from the previous assignment's directory into your current directory.
Rename Set.java
as SetCharArray
(and change the name of the class in the file).
SetCharArray
private and all the methods public.
Set
containing signatures
of all the public methods of SetCharArray
.
Add a method elementNumber(int)
that will
return a certain character from the set (assuming that each element
is ordered, from 0 to the size minus 1).
SetCharArray
:
Set
elementNumber(int)
.
other
has
a char[]
instance variable, so replace such references
with calls to elementNumber
SetString
which implements
the interface Set
but uses a String inside.
Hints: The String methods length()
, charAt(int)
,
and indexOf(String s)
. The last of these will
tell you the location of the first occurrence of s
in the
receiver string, or -1 if it doesn't occur at all.
Copy the files Shape.java
and ShapeDriver.java
from the class public directory to your directory for this assignment,
and look at them in Xemacs.
In this part of the assignment, you will write classes for
three types of shapes: Rectangles, right triangles, and
isosceles triangles. They all should implement the
Shape
interface, that is, have methods for
computing their area, computing their perimeter, and
drawing a picture on the screen.
Write classes Rectangle
, RightTriangle
and IsoTriangle
. Their draw methods should produce
something like
********** ********** ********** * *** ****** ********* * *** ***** ******* ********* ***********
Figuring out how to draw the triangles is the tricky part. You may discuss this with your classmates, but your code must be your own.
The constructors should query the user to get information about the dimensions for the shape.
As you go, update the driver so that it give the user the option to instantiate each shape class.
You also are responsible for documenting Shape.java
.
Create the script file as before (cat, rm, compile, and run)
> a2ps -P sp (the name of the script file)
(This will print "two up", meaning two pages shown next to each other
on one pice of paper. If you use a2ps
on a Java file, it
will format it nicely like in the handouts I've given in class.
The command lpr
works similarly except it does no formatting
and doesn't print two up by default.)
Then turn in the hard copy by 5:00, Wed, Apr 6.