Assignment #2: Inheritance and Interfaces
In this assignment you need to write a host of classes, and place them
in a reasonable hierarchy.
Some classes might be abstract, some might be interfaces, etc.
You have to make all the decisions. Then you will write methods to manipulate classes that are
in the hierarchy.
Basic classes
Design four classes:
Circle,
Rectangle,
Square, and
Ellipse.
All classes should provide constructors that set the dimensions and also
toString, getArea, getPerimeter.
The classes should provide accessors to get the dimensions.
Any attempts to set dimensions to be a negative or zero value should throw an appropriate
runtime exception (such as IllegalArgumentException). You do not have to
catch the exception in any part of the program.
This part is basically the same as stuff I have done in class.
Also provide a basic Shape class.
Embellishments
Some shapes are Comparable and should provide a compareTo method
(but only if it deciding which shape is larger is absolutely beyond question).
Almost all shapes are Expandable, meaning all dimensions can be increased by a
(multiplicative) factor such as 2 (which would make the shape twice as large in all directions)
by invoking expand.
Some shapes are Strechable which means that the longer dimension can be changed to
a specified new dimension by invoking stretch.
Test Class
Write a test class, Assign2, which will have several static methods.
- Write a main that creates an array and populates it with several different shapes. Then have
main invoke the additional methods below.
- Write a method that takes an array with several shapes and expands all of them.
- Write a method that takes an array with several shapes and stretches the ones that
are Stretchable (you can use instanceof).
- Write a method that takes an array and finds the maximum item in the array.
- Write a method that takes an array and a comparator and finds the maximum item in the array.
Avoiding Generics
In this assignment you can either use the existing generic interfaces Comparable<T>
and java.util.Comparator<T> or you can simply provide your own non-generic interfaces:
interface ComparableObject
{
int compareTo( Object other );
}
interface Comparator
{
int compare( Object lhs, Object rhs );
}
If you provide your own non-generic interfaces, you will have to do some extra type-casting.