Here is my assignment that I need help with:
1. Implement a class IrregularPolygon that contains an array list of Point2D.Double objects.
2. The Point2D.Double class defines a point specified in double precision 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() { }
-
}
As far as I can tell, I am ALMOST there, but there seems to be a technicality in my main method that prevents my program from working. Here is the tester file:
-
import java.awt.geom.*;
-
import java.util.Scanner;
-
-
public class IrregularPolygonTester
-
{
-
public static String blah = new String("Y");//is this the proper way to do this?
-
-
public static void main(String[] args)
-
{
-
IrregularPolygon myShape = new IrregularPolygon();
-
Scanner in = new Scanner(System.in);
-
while(blah.equals("Y"))//test if user wants to keep adding points
-
{
-
System.out.print("Enter X-Coordinate: ");
-
double x = in.nextDouble();
-
System.out.print("Enter Y-Coordinate: ");
-
double y = in.nextDouble();
-
Point2D.Double myPoint = new Point2D.Double(x, y);
-
myShape.toAdd(myPoint);//this line is causing problems
-
System.out.print("Enter another point? (Y/N)");
-
blah = in.nextLine();
-
blah.toUpperCase();
-
blah.trim();
-
}
-
-
myShape.draw();
-
System.out.println("Perimeter of figure = " + myShape.perimeter());
-
System.out.println("Area of figure = " + myShape.area());
-
}
-
-
}
And here is the regular class file:
-
import java.awt.geom.*;
-
import java.util.ArrayList;
-
import gpdraw.*;
-
-
public class IrregularPolygon
-
{
-
private ArrayList <Point2D.Double> myPolygon;
-
private double x;
-
private double y;
-
private double perim;
-
private double total;
-
private DrawingTool myPen;
-
private SketchPad myPaper;
-
-
public IrregularPolygon()
-
{
-
ArrayList <Point2D.Double> myPolygon = new ArrayList <Point2D.Double>();
-
x = 1;
-
y = 1;
-
perim = 0.0;
-
total = 0.0;
-
myPaper = new SketchPad(500,500);
-
myPen = new DrawingTool(myPaper);
-
}
-
-
public void toAdd(Point2D.Double myPoint)
-
{
-
myPolygon.add(myPoint);
-
}
-
-
public void draw()
-
{
-
myPen.up();
-
myPen.move(myPolygon.get(0).getX(), myPolygon.get(0).getY());
-
myPen.down();
-
-
for(int i = 1; i < myPolygon.size(); i++)
-
{
-
myPen.move(myPolygon.get(i).getX(), myPolygon.get(i).getY());
-
}
-
}
-
-
public double perimeter()
-
{
-
for(int i = 0; i < myPolygon.size(); i++)
-
{
-
perim += ((Point2D.Double)myPolygon.get(i)).distance((Point2D.Double)myPolygon.get(i + 1));
-
}
-
return perim;
-
}
-
-
public double area()
-
{
-
for(int i = 0; i < myPolygon.size(); i++)
-
{
-
double X1 = (myPolygon.get(i).getX());
-
double Y1 = (myPolygon.get(i).getY());
-
double X2 = (myPolygon.get(i + 1).getX());
-
double Y2 = (myPolygon.get(i + 1).getY());
-
total += (X1 * Y2 - Y1 * X2);
-
}
-
return 0.5 * total;
-
}
-
}
ANY help would be greatly appreciated! If you need any additional info, please let me know! Thanks in advance!