Inheritance and Hierarchies in Java
To get some background on Inheritance in Java, take a look at
these notes.
Geometric Shapes
To start, create a lab5 subdirectory
in your labs directory, and make it your working directory.
Download the files in the /home/cs230/download/Lab5 directory, in puma.
Introduction
- Inheritance is one of the principle ways in which software can be reused.
Code of classes can be reused in other situations by modifying a base class.
- The proper designations of base classes can provided a useful hierarchy
of classes.
- In this exercise, the abstract Shape class will provide a
base for a hierarchy of classes that define the properties and areas of
various geometric figures.
- For example, the Circle class will inherit from the Shape
class and add the radius property and the area calculation
definition. Other classes for closed polygons will be inherited from the
same base class
The Shape Class
The Shape class provides the base for the hierarchy of geometric shapes.
The definition of this abstract class is given to you. Study that definition. In particular, pay
attention to the property of the class, and its methods. There are some defined methods
(toString()
and compareTo(), as well as an abstract one (area()).
To Do: Write The Circle Class
- The
Circle class extends the Shape class.
This means that the Circle class has the properties and methods of
the Shapes class.
- The
Circle class implements the area() method.
Remember, area() was declared abstract in the Shape
class.
- Add a private property
radius to your Circle class.
- Add a new constructor to assign the
name and the
radius properties.
- Notice that the
Circle constructor invokes the Shape
constructor with the keyword super.
To Test: Run The TestShape Class In the Lab5
download directory, there is a class called TestShape.
This class asks the user for shape selection and size, and adds each
Shape object to a Shape array. The array is then sorted by area of
each Shape, and then the contents of the sorted array are printed out.
Right now, the user can only specify circles, but this will soon evolve...
Add other classes
Write more classes to build a hierarchy of shapes. For each class you add to the
Shape hierarchy, think about the following points:
- what class the newly added one should inherit from?
- what private properties should you add to the class?
- add a new constructor (which most probably should call the constructor
in the parent class).
- add a
main() method to include some initial, basic testing
for your class. Also, augment the TestShape() method, to thorough test your
methods.
Here are some ideas of classes to add to the hierarchy:
Rectangle
Square
Triangle
Isosceles Triangle
regular Pentagon
regular Hexagon
Perimeter of Shape objects
Add the method perimeter() to your classes. Think where, in the Shapes hierarchy,
the method should be introduced and where and how it should be defined.
Reference: Area of shapes
- Triangle:
You can use Heron's formula to calculate the area of a triangle when you know the
lengths of all three sides, a, b and c.
The area, S, is given by:
S = sqrt(p * (p-a)*(p-b)*(p-c) )
where p is half the perimeter, or p = (a+b+c)/2
- Regular Hexagon:
The area, S, of a regular Hexagon of side length t is given by the formula:
S = (3*sqrt(3)/2) * t^2
- Regular Pentagon:
The area, S, of a regular Pentagon of side length t is given by the formula:
S = 5/4 * t^2 * cot(pi/5)
|