We played with the idea of GeometricObject
interface in the application activities. Let's write a complete application using this idea (we'll simplify the interfaces involved, a little). Start by defining two interfaces:
GeometricObject
has a getArea()
and getPerimeter()
method, both of which return double
values.Resizable
has a resize()
method that takes an int
percentageLet's whip up some geometric objects. Write a Circle
and Square
class, each of which implement GeometricObject
. Note you'll need to use protected
instance variables. Write appropriate constructors for these classes. By the way, π in Java is Math.PI
—no import
statement necessary.
Then write subclasses ResizableCircle
and ResizableSquare
which implement the Resizable
interface.
Write a main program that uses these classes:
ArrayList
of GeometricObject
s,for
to print out the area and perimeter of all the shapes. ArrayList
of Resizable
s, If you get done with that quickly, think about incorporating two more shapes: Ellipse
and Rectangle
. Note that each of these require two dimensions. You can find formulas for the ellipse here (heh heh). Where should these shapes go in the "inheritance tree"? Does adding them change anything about your previously-written code?