Lab 5: Inheritance

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