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
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:
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
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:
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:
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
MyRectangle aRectangle = new
MyRectangle(4.0, 6.0);
// inherits area and perimeter instance methods from
abstract class AbstractRectangle
String name();
Square extends AbstractRectangle
// inherited from AbstractRectangle
Square(double l)
// constructor takes one parameter (length) with which to initialize both
inherited
// instance attributes
Square aSquare = new Square(5.0);
// inherits area and perimeter instance methods from
abstract class AbstractRectangle
String name();
ShapeApplet extends JApplet // once again, your user interface Is-a JApplet!
Vector shapes; //
a collection of shapes
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.
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