Assignment 2 (due 7 March)


In this assigment you will write an object oriented program using Java that will consist of a class hierarchy of shapes (e.g., square, triangle, circle) that implement a shape interface.  The shape interface requires that a shape provides instance methods to reveal its name, and its perimeter and area measurements.  

To be handed in:  a diskette labeled with your name, containing the subdirectories comprising your JBuilder project subtree (your .jpr project file should be in the root of the subtree) and printouts of your Java source files (*.java).  Please place your diskette and printouts in a large envelope also labeled with your name.

The project will consist of the following interface and classes:


Shape

by definition, interfaces don't have instance attributes

by definition, interfaces don't have constructors

String name()

returns the name of the shape (e.g., "Triangle")

double area()

returns the area measurement of the shape

double perimeter()

returns the perimeter measurement of the shape


Circle implements Shape

double radius;  // the length of the circle's radius

Circle(double r)

constructs a new Circle object with the specified radius

Sample invocation: 

Circle aCircle = new Circle(3.0);

must implement the three methods specified by interface Shape.  Recall the formulae to calculate the area and circumference of a circle:

area = 2 * p * r

circumference = p * r * r


Triangle implements Shape

double sideA, sideB, sideC;  // the lengths of the three sides of the triangle

Triangle(double a, double b, double c)

constructs a new Triangle object with the three specified side lengths

Sample invocation: 

Triangle aTriangle = new Triangle(3.0, 4.0, 5.0);

must implement the three methods specified by interface Shape.  Use Heron's formula to calculate the area of a triangle given the lengths of its three sides:

area = s * (s - sideA) * (s - sideB) * (s - sideC),
where
      s =  (sideA + sideB + sideC) / 2


(abstract) AbstractRectangle implements Shape

double length, width;  // the length and width of a rectangle

by definition, abstract classes don't have constructors

must implement the three methods specified by interface Shape.  Recall the formulae to calculate the area and perimeter of a rectangle:

area = length * width
perimeter = (2 * length) + (2 * width)


MyRectangle extends AbstractRectangle

// inherited from AbstractRectangle

MyRectangle(double l, double w)
// constructor takes two parameters to initialize corresponding inherited
// instance attributes

Sample invocation: 

MyRectangle aRectangle = new MyRectangle(4.0, 6.0);

// inherits area and perimeter instance methods from abstract class AbstractRectangle

// overload the interface method for shape name to return string "Rectangle"

String name();


Square extends AbstractRectangle

// inherited from AbstractRectangle

Square(double l)
// constructor takes one parameter (length) with which to initialize both inherited
// instance attributes

Sample invocation: 

Square aSquare = new Square(5.0);

// inherits area and perimeter instance methods from abstract class AbstractRectangle

// overload the interface method for shape name to return string "Square"

String name();


ShapeApplet extends JApplet   // once again, your user interface Is-a JApplet!

Vector shapes;  // a collection of shapes

Notice how your ShapeApplet JApplet uses (contains) shape objects instantiated from the previous classes.

You'll define methods corresponding to the instances of JButtons on your JApplet panel.

The buttons will add circles, triangles, rectangles, and squares to the shapes vector.

The parameters for these new shapes will be taken from the corresponding JTextFields on the applet panel.  Similar to the previous programming assignment, you'll need to parse the double values from the strings returned by getText().  You'll find the following static (class) method helpful:  Double.parseDouble(s) -- returns a double value parsed from its String parameter.

A "List Shapes" button will iterate through the shapes vector, and use System.out.println() to list the stored shapes.  Unlike the previous assignment, the objects stored in the shapes vector can be instances of different shape classes -- but all the shape classes implement the Shape interface.  This means we can cast references our iterator provides from the shapes vector to be (Shape) references.  For each shape object in the vector, print the shape's name, and its area and perimeter.


(!) Note: All constructors and all instance methods should be public, and all attributes should be private (or protected).

The GUI (Graphical User Interface) should look something like this:

The output from the System.out.println()'s should look something like this:

Shape Rectangle with area = 20.0 and perimeter = 18.0
Shape Square with area = 36.0 and perimeter = 24.0
Shape Triangle with area = 6.0 and perimeter = 12.0
Shape Circle with area = 113.09733552923255 and perimeter = 37.69911184307752