Lab 5: Inheritance
- Review the inheritance example files in the
account/ and
shape/ directories.
- Create an empty directory named
javalab5/ in your
directory.
- Copy all files (including the
cs1/ directory) from
the shape/ directory
into your javalab5/ directory.
- Create a class named
SquareShape that extends
Shape.
- Copy from
RectangleShape.java to SquareShape.java.
- Use search and replace to change
Rectangle to Square
and height to side.
- Remove private variable
width, its accessor method, and all
references to it.
- There should be four constructors (like
CircleShape.java):
with or without the base point, and with or without side.
- The
perimeter() method should return four times a side.
- The
area() method should return the square of a side.
- Create a class named
RightTriangleShape that extends
RectangleShape.
- Copy from
RectangleShape.java to RightTriangleShape.java.
- Use search and replace to change
Rectangle to
RightTriangle.
- Make sure
RightTriangleShape extends RectangleShape
and not Shape.
- Remove the private instance variables and accessors and the
toString() method.
- The constructors should simply call
super() with the same
arguments.
- Override the
area() method either by dividing super.area()
by two, or by using w * h / 2.
- Override the
perimeter() method by using w + h +
Math.sqrt(w * w + h * h).
- Open
ShapeTest.java and modify as follows, adding to
the existing tests of other shapes:
- Prompt the user to input some numbers for at least one right triangle
and one square.
- Display the output by printing the right triangle and square objects
using
System.out.println().
- Compile the programs by typing
javac *.java.
- Run by typing
java ShapeTest.
- Test, debug and document the programs.
- You can quickly improve the alignment of your code as follows:
- Open each of your files in the emacs editor by typing
emacs
filename.java.
- To mark the beginning of the region, press Ctrl-space.
- To go to the end of the file, Press Esc, press >.
- To line up tabs, press Esc, press x, type tabify, press Tab, press
Enter.
- To save and exit, press Ctrl-x, press Ctrl-c.
- Note: for more information about emacs, see p. 5 of the QuickStudy
UNIX guide, available at the copy centre.
- Open the Ark archiving program.
- Click the K menu, move to Utilities, and click Ark.
- Create a new archive named javalab5.zip.
- Add your folder containing all the shape files.
- Send the javalab5.zip file to the instructor as an attachment in a
message with subject java lab 5.