CSC1110 Homework 12

Inheritance with Shapes

Inheritance comes in many forms. In this assignment, we will create an inheritance hierarchy of Shapes.

UML

The Shape Interface

The Shape interface has only one methods: getArea() which will calculate and return the area of a given Shape. See the sample output for display formatting. You will use your Color enumeration from Week 11 when formatting this output.

Inheritance

You will be responsible for implementing and overriding methods, as needed. Use the UML as a guide for when to do so. You will need to derive some of the instance variable values from the parameters being passed in. In other words, sometimes the values are not explicitly passed in, but can be determined by the values that are given or by the type of shape that it is. As an example, you are able to calculate the angles of a Triangle if you have the lengths of all three sides.

equals()

In order to override the Object class’s equals() method, you will need an Object as an argument, and will need to typecast that argument in the body of the method in order to compare the two Shapes. It is good practice to verify that the Object being passed in to the method is, in fact, the correct type before casting it. To that end, there is an operator in Java, instanceof, that will return true if both operands are of the same declared (or inherited) type:

if(o instanceof Triangle)

Use the attached video to help you implement the methods that calculate values.

Driver class

A test driver class has been provided. The program will create instances of the various Shapes and print them to the console, as shown in the sample output.

Sample Output

SampleOutput

Submission Instructions:

Commit and push your project to your GitHub repository.