I am new to java and I need to create an irregular polygon class that takes in an array list of Point2D.Double objects, constructs and initializes the points, then draws the polygon and calculates the perimeter and area formed by the polygon.
Here is my irregular polygon class:
import java.awt.geom.*;
import java.util.ArrayList;
import gpdraw.*;
import java.util.Scanner;
public class IrregularPolygon{
private ArrayList <Point2D.Double> myPolygon;
DrawingTool myPencil;
SketchPad myPaper;
double x;
double y;
double peri;
double total;
int numPoints;
int pointsEntered;
int aPoint;
public IrregularPolygon(){
myPolygon = new ArrayList <Point2D.Double>();
x = 1;
y = 1;
peri = 0.0;
total = 0.0;
myPaper = new SketchPad(500,500);
myPencil = new DrawingTool(myPaper);
}
public void input (Point2D.Double aPoint){
Scanner in = new Scanner(System.in);
System.out.print("How many points are in your irregular polygon? ");
int numPoints = in.nextInt();
for (pointsEntered = 1; pointsEntered <= numPoints; pointsEntered++){
System.out.print("enter the x coordinate for your #" + pointsEntered + " point: ");
double x = in.nextDouble();
System.out.print("enter the y coordinate for your #" + pointsEntered + " point: ");
double y = in.nextDouble();
Point2D.Double myPoint = new Point2D.Double(x,y);
myPolygon.add(myPoint);
}
}
public void draws(){
myPencil.up();
myPencil.move(myPolygon.get(0).getX(), myPolygon.get(0).getY());
myPencil.down();
for(int i = 1; i < myPolygon.size(); i++){
myPencil.move(myPolygon.get(i).getX(), myPolygon.get(i).getY());
}
}
public double perimeter(){
for(int i = 0; i < myPolygon.size(); i++){
peri += ((Point2D.Double)myPolygon.get(i)).distance((Point 2D.Double)myPolygon.get(i + 1));
}
return peri;
}
public double area(){
for(int i = 0; i < myPolygon.size()-1; i++){
double myX = (myPolygon.get(i).getX());
double myY = (myPolygon.get(i).getY());
double my1X = (myPolygon.get(i + 1).getX());
double my1Y = (myPolygon.get(i + 1).getY());
total += (myX * my1Y - myY * my1X);
}
return .5 * total;
}
}
Here is my tester:
import java.awt.geom.*;
public class tester{
public static void main(String[] args){
IrregularPolygon app = new IrregularPolygon();
Point2D.Double myShape = new Point2D.Double ();
app.input(myShape);
double are = app.area();
System.out.println("The area of your irregular polygon is " + are + " square units");
double per = app.perimeter();
System.out.println("The perimeter of your irregular polygon is " + per + " units");
app.draws();
}
}
Here is my run output for these sample points:
--------------------Configuration: <Default>--------------------
How many points are in your irregular polygon? 4
enter the x coordinate for your #1 point: 20
enter the y coordinate for your #1 point: 10
enter the x coordinate for your #2 point: 70
enter the y coordinate for your #2 point: 20
enter the x coordinate for your #3 point: 50
enter the y coordinate for your #3 point: 50
enter the x coordinate for your #4 point: 0
enter the y coordinate for your #4 point: 40
The area of your irregular polygon is 2100.0 square units
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 4, Size: 4
at java.util.ArrayList.RangeCheck(ArrayList.java:547)
at java.util.ArrayList.get(ArrayList.java:322)
at IrregularPolygon.perimeter(IrregularPolygon.java:5 6)
at tester.main(tester.java:11)
I get this error and the area of this polygon should be 1700 square units. I am not sure if there is something wrong with my irregular polygon, tester class, or both but thanks for any help.