Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Desperately need help with ArrayLists....

807591Feb 28 2008 — edited Feb 28 2008
So I need to finish this "IrregularPolygon" for my CS2 class...and I'm having a lot of trouble with it. Any help with my problems would be greatly appreciated.
I'm having trouble closing my polygon in the void draw. For example if my polygon is just a square, I don't know how to connect point D to point A without the user inputing a point E to overlap A.
I also have no idea what the void add is suppose to be used for and I have no idea how to go about solving for perimeter.
I know it's a lot...but any help with any of these problems would be appreciated. Thanks.
/**
 * Daniel Varela
 * ArrayList... >_<
 */
import java.awt.geom.*;  // for Point2D.Double
import gpdraw.*;        // for DrawingTool 
import java.util.*;     // for ArrayList
public class IrregularPolygon
{
    //variables
    char ch;
    int ptNum, xPos, yPos;
    double area, perim, myX, myY, total;
    private DrawingTool myPencil;
    private SketchPad myPaper;

    //Universal Scanner and ArrayList.
    Scanner input = new Scanner(System.in);
    private ArrayList <Point2D.Double> myPolygon;
    
   // Input for USER ONLY.
   public void input(ArrayList myPolygon)
   {
       System.out.println("We need vertices to create a polygon.");
       System.out.println("The SketchPad is (100, 100) in size.");
       System.out.println("Please provide the vertices in consecutive order.");
       System.out.println("When you want to stop entering values, type 'done'.");
       
       try //try is used to exit the for loop w/o crashing the program.
       {
           for (char ch = 'A'; ch <= 'Z'; ch++) 
           //Pre-Condition: Points (Xn, Yn) such that n <= 26.
           {
               System.out.print("Enter the X coordinate for point " + ch + ": ");
               double x = input.nextDouble();
               System.out.print("Enter the Y coordinate for point " + ch + ": ");
               double y = input.nextDouble();
               Point2D.Double myPoint = new Point2D.Double(x,y);
               myPolygon.add(myPoint);
            }
        }catch(InputMismatchException e) //By typing anything other than a double, this will print.
        {
            System.out.println("\nDone with user input.");
        }
    }
    //Output from Input values.
   public void output(ArrayList myPolygon)
   {
       try //try used to exit for loop the instance user input no longer exists.
       {
           for (char ch = 'A'; ch <= 'Z'; ch++)
           {
               Point2D.Double pt = (Point2D.Double)myPolygon.get(ch - 65); 
               System.out.println("Point " + ch + " is (" + pt.getX() + "," + pt.getY() + ")");
            }
       }catch(IndexOutOfBoundsException e)
       {
           System.out.println("");
       }
   }
   
   public void add(Point2D.Double aPoint) 
   {
   }

   public void draw(ArrayList myPolygon) 
   {   
       myPaper = new SketchPad(100, 100);
       myPencil = new DrawingTool(myPaper);
       try
       {
           for (char ch = 'A'; ch <= 'Z'; ch++)
           {
               String s = " ";
               s = String.valueOf(ch); //This will get the value of every char. ex: A, B, C, etc.
               Point2D.Double pt = (Point2D.Double)myPolygon.get(ch - 65);
               myPencil.move((pt.getX()),(pt.getY()));
               myPencil.down();
               myPencil.drawString(s); //Prints the corresponding ch value. ex: A, B, C, etc.
           }

        }catch(IndexOutOfBoundsException e)
        {
            System.out.println("");
        }
    }

   public void perimeter(ArrayList myPolygon)
   { 
   }

   public double calculateArea(ArrayList myPolygon)
   { 
       return 0;
   }
   
   public static void main(String[] args)
   {
       IrregularPolygon algorithm = new IrregularPolygon();
       ArrayList myPolygon = new ArrayList();
       algorithm.input(myPolygon);
       algorithm.output(myPolygon);
       algorithm.draw(myPolygon);
       algorithm.perimeter(myPolygon);
       double area = algorithm.calculateArea(myPolygon);
       System.out.println("The area of the Polygon is: " + area);
   }
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 27 2008
Added on Feb 28 2008
1 comment
191 views