ArrayList Program Help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jeffery123
    New Member
    • Feb 2010
    • 2

    ArrayList Program Help

    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.Array List;
    import gpdraw.*;
    import java.util.Scann er;

    public class IrregularPolygo n{
    private ArrayList <Point2D.Double > myPolygon;
    DrawingTool myPencil;
    SketchPad myPaper;
    double x;
    double y;
    double peri;
    double total;
    int numPoints;
    int pointsEntered;
    int aPoint;

    public IrregularPolygo n(){
    myPolygon = new ArrayList <Point2D.Double >();
    x = 1;
    y = 1;
    peri = 0.0;
    total = 0.0;
    myPaper = new SketchPad(500,5 00);
    myPencil = new DrawingTool(myP aper);


    }
    public void input (Point2D.Double aPoint){
    Scanner in = new Scanner(System. in);
    System.out.prin t("How many points are in your irregular polygon? ");
    int numPoints = in.nextInt();
    for (pointsEntered = 1; pointsEntered <= numPoints; pointsEntered++ ){
    System.out.prin t("enter the x coordinate for your #" + pointsEntered + " point: ");
    double x = in.nextDouble() ;
    System.out.prin t("enter the y coordinate for your #" + pointsEntered + " point: ");
    double y = in.nextDouble() ;
    Point2D.Double myPoint = new Point2D.Double( x,y);
    myPolygon.add(m yPoint);
    }
    }

    public void draws(){
    myPencil.up();
    myPencil.move(m yPolygon.get(0) .getX(), myPolygon.get(0 ).getY());
    myPencil.down() ;

    for(int i = 1; i < myPolygon.size( ); i++){
    myPencil.move(m yPolygon.get(i) .getX(), myPolygon.get(i ).getY());
    }
    }

    public double perimeter(){
    for(int i = 0; i < myPolygon.size( ); i++){
    peri += ((Point2D.Doubl e)myPolygon.get (i)).distance(( Point2D.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){
    IrregularPolygo n app = new IrregularPolygo n();
    Point2D.Double myShape = new Point2D.Double ();
    app.input(mySha pe);
    double are = app.area();
    System.out.prin tln("The area of your irregular polygon is " + are + " square units");
    double per = app.perimeter() ;
    System.out.prin tln("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.Index OutOfBoundsExce ption: Index: 4, Size: 4
    at java.util.Array List.RangeCheck (ArrayList.java :547)
    at java.util.Array List.get(ArrayL ist.java:322)
    at IrregularPolygo n.perimeter(Irr egularPolygon.j ava:56)
    at tester.main(tes ter.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.
  • pbrockway2
    Recognized Expert New Member
    • Nov 2007
    • 151

    #2
    peri += ((Point2D.Doubl e)myPolygon.get (i)).distance(( Point 2D.Double)myPol ygon.get(i + 1));
    The last point doesn't have a "next" point. Well, it does, but it isn't myPolygon.get(i +1) rather it's myPolygon.get(0 ).

    Comment

    • pbrockway2
      Recognized Expert New Member
      • Nov 2007
      • 151

      #3
      I am not sure if there is something wrong with my irregular polygon, tester class
      The runtime stack trace aims to help with this problem by printing the line which caused the error and working backwards (the line that called it, the line that called that etc). Typically the first line of your code that's mentioned is a good place to start looking for the bug.

      Additionally the error message itself suggests the nature of the problem. ArrayIndexOutOf BoundsException occurs when an array index is negative or too big.

      Comment

      • jeffery123
        New Member
        • Feb 2010
        • 2

        #4
        Thank you so much, that solved the problem, now it runs perfectly.

        Comment

        Working...