Hey guys, I'm stuck at a problem. Check out the code:

import java.util.*;

class testAll
{
	public static void main(String[] args)
	{
		Scanner input = new Scanner(System.in);
		ArrayList pointsArray = new ArrayList();
		ArrayList linesArray = new ArrayList();
		System.out.println("Enter amount of sides: ");
		int sides = input.nextInt();
		if (sides <= 2 || sides > 6)
		{
			System.out.println("Error in creating polygram");
		} 
		else
		{
			for (int i = 0; i < sides + 1; i++)
			{
				System.out.println("Enter the X coordinate: ");
				int a = input.nextInt();
				System.out.println("Enter the Y coordinate: ");
				int b = input.nextInt();

				Point P = new Point();
				P.create(a, b);
				pointsArray.add(P);
			}
			for (int i = 0; i < pointsArray.size() - 1; i++)
			{
				Point P1 = (Point) pointsArray.get(i);
				Point P2 = (Point) pointsArray.get(i + 1);
				Line L1 = new Line();
				L1.create(P1, P2);
				linesArray.add(L1);
				L1.display();
				L1.length();
				System.out.println();
			}
			linesArray.size();
		}
	}

}

I have to allow the user to enter the amount of sides they want to enter (no less than 2 and no more than 6), and then I have to calculate the perimeter of the created polygon.
So far I have been able to get the following output which looks a bit like this:

Enter amount of sides: 
3
Enter the X coordinate: 
1
Enter the Y coordinate: 
2
Enter the X coordinate: 
3
Enter the Y coordinate: 
2
Enter the X coordinate: 
2
Enter the Y coordinate: 
23
Enter the X coordinate: 
23
Enter the Y coordinate: 

1
Point 1 = (1,2)
Point 2 = (3,2)
The length of the line is: 2.0

Point 1 = (3,2)
Point 2 = (2,23)
The length of the line is: 21.02379604162864

Point 1 = (2,23)
Point 2 = (23,1)
The length of the line is: 30.4138126514911

I am now stuck at trying to add the results of the lines lengths and add them all together, to output the perimeter of the polygon. Any help would be appreciated ;-)

Since you start the index from 0 the first loop would be this:

for (int i = 0; i < sides; i++)

If sides = 3, then i = 0,1,2. 3 sides.
Or:

for (int i = 1; i <= sides; i++)

If sides = 3, then i = 1,2,3. 3 sides.

As you have noticed you entered 3 and the program asked for 4 points.

Also it is not a good idea to have the methods of an object (Point, Line) to call the System.out.println. What if you want to use it in desktop gui, or a web page. You will have to make additional changes.
If you had methods that return values, you could use them anywhere you want:

class Point {
   int a = 0;
   int b = 0;

   public String toString() {
       return "("+a+","+b+")";
   }
}
class Line {
   Point p1,p2;

   public Point getP1() {
      return p1;
  }

   public double length() {
       // calculate length;
       return 123.45;
   }
}
System.out.println("Point 1: "+ L1.getP1());
System.out.println("Point 2: "+ L1.getP2());
System.out.println("The length of the line is: "+ L1.length());

Now that you have the length, you can add all the lengths in the for loop:

[B]double sum = 0;[/B]
for (int i = 0; i < pointsArray.size() - 1; i++)
{
// YOUR CODE
   Point P1 = (Point) pointsArray.get(i);
   ...
   System.out.println("The length of the line is: "+ L1.length());

[B]sum = sum + L1.length();[/B]
}
[B]System.out.println("Perimeter: "+sum);[/B]

Hey, thanks for the tip. It was so obvious, but I obviosuly didn't see that :p . Last request, how would i be able to take the first point (x,y) and make it the last as well, so that the polygon is a closed polygon, as the first and last points are the same.
Kind of stuck on that..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.