In this lab we will practice building classes using abstract classes.
In the file, make a Public class: ShapeTests
and additional
classes in the file (without the public qualifier) named: Octagon
and Triangle
Write a concrete class named Octagon that extends the provided
GeometricObject class and implements the Comparable
interface.
Assume all eight sides of the octagon are of equal length. The area can be
computed using the following formula:
Define the Octagon class to have a private, double
data field
named side
with its getter and setter methods.
Define a no-arg constructor that creates an Octagon
with
side
0, and a constructor to create an Octagon
with a specified side
. Implement the getArea
and
getPerimeter
methods, as is required.
Define the concrete Triangle
class derived from
GeometricObject
to represent a geometric object with three sides.
In a valid triangle, the sum of any two sides is greater than the other side.
The Triangle
class must adhere to this rule. Make the constructor
throw an Exception
if a triangle is created with sides that violate
the rule, as follows:
/** Construct a triangle with the specified sides */
public Triangle(double side1, double side2, double side3) throws Exception {
// Implement it
}
Write a test program that creates an ArrayList<GeometricObject>
shapes containing 3 Octagon
objects and 3 Triangle
objects. Compare two Octagon
objects using the compareTo
method.
Write a loop that sums the area of all the geometric objects in the array by
calling the overridden getArea
method on each object.
When you are done with these, feel free to help others or leave.