CISC 3115

Exercise 3: (More) Fun with Geometric Objects

Objectives:

A Couple Interfaces

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:

Remember, every class and interface should be in separate files.

A Few Classes

Let'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.

A Main Program

Write a main program that uses these classes:

  1. Make an ArrayList of GeometricObjects,
  2. fill it with some squares and circles,
  3. and then use enhanced for to print out the area and perimeter of all the shapes.
  4. Then make another ArrayList of Resizables,
  5. and fill it with some shapes.
  6. Print out the areas and perimeters, then loop through again, resizing to 50%, and print out the areas and perimeters again. Did the resizing have the correct effect?

Bonus...

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?