POLYGONS
POLYGONS

Work the pre-lab, FIRST.

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.

  1. Modify SimpleClosed2DPolygon
    1. to add a setColor method   (Be sure to test in a client!)
    2. to add a constructor which takes an array of locations as a parameter & uses them to build the lines of the polygon
    3. Test both your new constructor & setColor methods in a client!

  2. 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.

    1. Write a client to check out (ie, fully exercise all its methods) Triangle2D.

    2. 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:
      • compute the length of each side as a double ( Euclidean distance from vertex to vertex)
      • any three points will define a non-degenerate triangle as long as they do not lie on the same line. a simple way to check would be to notice that the length of any one side cannot equal the sum of the other two sides so
                  lenA = computed length of side a
                  lenB = computed length of side b
                  lenC = computed length of side c
        
                  if ( lenA < lenB+lenC )
                           everything's fine
                  otherwise
                           you've got a degenerate triangle (pts are colinear)
         
        Should you be checking all combinations? Is one enough?

    3. Modify your client to exercise the new method.

  3. Modify SimpleClosed2DPolygon
    1. 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.
    2. 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.

  1. Write a class named Rectangle2D which inherits from SimpleClosed2DPolygon.
    1. 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?
    2. Write a client to check out (ie, fully exercise all its methods) Rectangle2D using an array of Rectangles2D.

  2. Write a class named ApproxCircle2D which inherits from SimpleClosed2DPolygon.
    1. 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
    2. Write a client to check out (ie, fully exercise all its methods) ApproxCircle2D.

  3. Write a class named Pentagon2D which inherits from ApproxCircle2D.
    Write a client to check out (ie, fully exercise all its methods) Pentagon2D.

  4. Write a client which declares and draws an example of all the polygon classes available -- each with a different color.