Homework #2: Inheritance & Polymorphism

Problem

One of the classic examples of inheritance uses geometric shapes, such as might be found in a "drawing" application. Rectangles, lines, circles, and other 2-dimensional shapes differ from each other, but they have some things in common. In this homework you will try to "factor out" the similarities of 2-dimensional shapes into a common superclass.

Assignment

Create an abstract base class, Shape, to store instances of 2-dimensional geometric shapes. At a minimum, a Shape should have:

Define subclasses of Shape for rectangles, circles, ellipses, and lines. Each class should have one or more constructors which take a suitable set of parameters. For example, a circle is defined by its center (X & Y) and radius, while a rectangle can be specified by the coordinates of two opposite corners or by the coordinates of one corner along with a width and a height. Each class should override the toString() method of the Shape class.

Write a testbed class, Homework02, to test your classes. Create a Shape array which can store at least a dozen shapes of various types. Populate the array with a mixed set of concrete objects using individual assignment statements. (Or, if you're feeling adventurous, you can write a loop and use the Random class to choose the type and location of Shape to insert into the array.) Using only a simple for loop and a println() statement, print the description of each element of the array.

Other requirements:

Due date: The homework is due at the beginning of class on 23 September.

Turn in: (1) Listings of the source files for all classes in your program, including your testbed class. (2) A printout of the terminal session in which you run your testbed class. (3) A printout of at least one Web page generated by javadoc for your set of classes.

E-mail: A Zip or tar archive file containing all of your source files (including package.html) to grading@bozoid.com. The subject of the e-mail should be "Homework02", and the archive name should be hw02.zip or hw02.tar.gz. In addition to your source files, the archive file should include (a) a text file named output.txt containing the record of your terminal session and (b) a text file named readme.txt containing your name and any other identifying information you think is appropriate. Do not include .class files or javadoc files.

Note: Staple all the pages of your submission together, and make sure you put your name on your homework!