In this lab you are asked to write several new classes and modify an existing one.
You are free, if you'd like, to replace ONE of the subclasses with a different
non-trivial
geometric shape (some sort of polygon) of your own choice.
- Modify SimpleClosed2DPolygon
- to add a setColor method (Be sure to test in a client!)
- to add a constructor which takes an array of locations as a parameter
& uses them to build the lines of the polygon
- Test both your new constructor & setColor methods in a client!
- Write a class named Triangle2D which inherits
from SimpleClosed2DPolygon in ways that make sense. This should not
be a very big class what with it inheriting so much.
- Write a client to check out (ie, fully exercise all its methods) Triangle2D.
- Add a method named isValid to your Triangle2D class which will
check the validity of
its vertices -- returning a value of true if they form a "valid" triangle
and a value of false otherwise. To perform the check:
- Modify your client to exercise the new method.
- Modify SimpleClosed2DPolygon
- to add a method named perimeter which returns the perimeter of the polygon
as a real number. Modify your client from class to exercise the new method.
- to add an abstract method named area which returns the area of the polygon
as a real number. You'll have to modify your Triangle2D class as well. (Why?) Do so.
If you've understood the above and completed it all, the next three classes should go relatively quickly.
- Write a class named Rectangle2D which inherits
from SimpleClosed2DPolygon.
- This class will have it's own version of the method isValid
which will perform the quick check that opposite sides are
of equal length. Would an abstract version of isValid be appropriate to add to superclass?
- Write a client to check out (ie, fully exercise all its methods) Rectangle2D
using an array of Rectangles2D.
- Write a class named ApproxCircle2D which inherits
from SimpleClosed2DPolygon.
- One of the constructors for this class has as arguments at least the following:
- X coordinate of center
- Y coordinate of center
- radius
- number of sides
- Write a client to check out (ie, fully exercise all its methods) ApproxCircle2D.
- Write a class named Pentagon2D which inherits
from ApproxCircle2D.
Write a client to check out (ie, fully exercise all its methods) Pentagon2D.
- Write a client which declares and draws an example of all
the polygon classes available -- each with a different
color.