Skip to main content
ICT
Lesson A15 - ArrayList
 
Main   Previous Next
 

LAB ASSIGNMENT A15.1 page 9 of 11

IrregularPolygon

Background:

Polygons are closed two-dimensional shapes bounded by line segments. The segments meet in pairs at corners called vertices. A polygon is irregular if not all its sides are equal in length. The figure below shows examples of irregular polygons:


Assignment:

  1. Implement a class IrregularPolygon that contains an array list of Point2D.Double objects.

  2. The Point2D.Double class defines a point specified as a double representing a location in (x, y) coordinate space. For example, Point2D.Double(2.5, 3.1) constructs and initializes a point at coordinates (2.5, 3.1).

  3. Use the following declarations as a starting point for your lab work.

    import java.awt.geom.*;     // for Point2D.Double
    import java.util.ArrayList; // for ArrayList
    import gpdraw.*;            // for DrawingTool

    public class IrregularPolygon
    {
       private ArrayList <Point2D.Double> myPolygon;

       // constructors
       public IrregularPolygon() { }

       // public methods
       public void add(Point2D.Double aPoint) { }

       public void draw() { }

       public double perimeter() { }

       public double area() { }
    }

    The constructor should initialize myPolygon into an empty ArrayList. The add method should add the point into the ArrayList. The draw() method should draw the polygon specified by the points in the ArrayList, and the perimeter() method and area() method should calculate and return the polygon's perimeter and area.
  4. The program should use the Drawing Tool to draw the polygon by joining adjacent points with a line segment, and then closing it up by joining the end and start points. Use the Drawing tool's Pen methods to move and draw the polygon.

  5. Write methods that compute the perimeter and the area of a polygon. To compute the perimeter, compute the distance between adjacent points, and total up the distances. The area of a polygon with corners is the absolute value of:

    Note: add n products, then subtract n products, then divide by 2. The result will be negative or positive depending on the order in which the products are taken, i.e., which products are subtracted and which are added.

  6. The parallelogram formed by the following coordinates (20, 10), (70, 20), (50, 50), (0, 40) has a perimeter of 174.1 units and an area of 1700 square units. You can use this as a test case to verify that your program is working correctly.
  7. Note: For illegal polygons with less than 3 vertices, both the add and the perimeter methods should return 0.

 

Web-CAT Submission:

    Before submitting the lab to Web-CAT you must edit the IrregularPolygon.java file and remove the DrawingTool instantiation statement (near line 18) as follows:

        private DrawingTool pen = new DrawingTool(new SketchPad( 300, 300, 0));

    change to:

        private DrawingTool pen; // = new DrawingTool(new SketchPad( 300, 300, 0));

     


Main   Previous Next
Contact
 © ICT 2006, All Rights Reserved.