473,473 Members | 1,977 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

ArrayList Program Help

2 New Member
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.
Feb 3 '10 #1
3 5950
pbrockway2
151 Recognized Expert New Member
peri += ((Point2D.Double)myPolygon.get(i)).distance((Point 2D.Double)myPolygon.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).
Feb 3 '10 #2
pbrockway2
151 Recognized Expert New Member
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. ArrayIndexOutOfBoundsException occurs when an array index is negative or too big.
Feb 3 '10 #3
jeffery123
2 New Member
Thank you so much, that solved the problem, now it runs perfectly.
Feb 3 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Stephen | last post by:
Im trying to carry work out an else if clause in the below method but i'm having a lot of difficulty getting the correct code which allows me to check and see if an arraylist items has text in it...
5
by: Matthias Kwiedor | last post by:
Hi! What i want to do, is to put an ArrayList into a Hashtable. So i can access the ArrayList fast over the key-indexing of the Hashtable. What i did is to put the ArrayList into the...
31
by: Extremest | last post by:
I have a loop that is set to run as long as the arraylist is > 0. at the beginning of this loop I grab the first object and then remove it. I then go into another loop that checks to see if there...
8
by: amazon | last post by:
I have a following structure that I am using with array list: Private Structure arrayliststruct Public Name As String Public value As String Public type As String End Structure and following...
6
by: fniles | last post by:
I am using VB.NET 2003 and a socket control to receive and sending data to clients. As I receive data in 1 thread, I put it into an arraylist, and then I remove the data from arraylist and send it...
6
by: schnag | last post by:
import java.util.*; class Student { private String name; private int score; public Student() { name = ""; score = 0; }
3
by: Christopher H | last post by:
I've been reading about how C# passes ArrayLists as reference and Structs as value, but I still can't get my program to work like I want it to. Simple example: ...
0
by: kinkay716 | last post by:
Hello - Im looking for some clarification on ArrayLists for a project I'm working on. It is basically a program which allows you to create a custom deck of cards. There is a listbox of available...
3
by: =?Utf-8?B?R3JlZw==?= | last post by:
I''ve been working with a CSharp web-site (and am new to it). I have a javascript function that accepts variables from my CSharp code. It all works fine, with the exception of one variable type. I...
5
by: blt51 | last post by:
I need to write a program that handles a bank account and does a number of transactions using an arraylist. However, I'm having trouble getting the arraylist to store all the transactions and then...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.