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!
17 14267 @slapsh0t11
It would be more straight forward to say: -
public static String blah = "Y";
-
and it would be better not to have a public static String variable at all. Since blah is only used in the main() method of IrregularPolygonTester, declare it there. Right before it is used (ie just before the while loop). -
myShape.toAdd(myPoint);//this line is causing problems
-
Perhaps you could describe the problems this line is causing. Does it compile? If not what are the compiler messages?
Does the program behave in an unwanted way when you run it? If so give any exception that is reported, or describe the unwanted behaviour and the input that gives rise to it.
The program does compile, but something seems to be going on with the line I indicated in my code as well as in the toAdd method in the non-tester file.
Here is the error message I keep receiving:
Enter X-Coordinate: 3
Enter Y-Coordinate: 4
Exception in thread "main" java.lang.NullPointerException
at IrregularPolygon.toAdd(IrregularPolygon.java:28)
at IrregularPolygonTester.main(IrregularPolygonTester .java:19)
OK.
So you have a "NullPointerException": that means that something is null when it shouldn't be. You get these if you dereference a variable which is null (eg try and invoke a method on something that is null), or if an array variable is null and you try and access one of its elements.
This exception occurs at line 28 of IrregularPolygon.java
So we have the immediate cause! myPolygon is null. [Of course it might be that add() is causeing the NulPointerException because myPoint is null. But in that case the stack trace would have started with by mentioning add()'s code.]
Now, since myPolygon is null you have two questions to answer:
(1) Where did you try and set the value of myPolygon?
(2) Why did this not happen?
I'm not sure I quite understand the nature of this problem or how to go about resolving it. Why does it matter if the myPolygon ArrayList is null (aka empty) if I add values to it (make it NOT null) before I try to do anything else with it? Have you been able to get this code to work?
Again, I really appreciate all of your help!
No - null does not mean "empty"! It's more like "nonexistent".
There is no actual array list (created with "new") to which myPolygon refers.
Isn't this the ArrayList to which myPolygon refers?
ArrayList <Point2D.Double> myPolygon = new ArrayList <Point2D.Double>();
Yes - that's the answer to question 1. That's where you tried to give myPolygon a value.
Now we need to answer question 2. How come - given that line - that when you later get to toAdd() myPolygon has no value? Hint: there is a difference in that constructor between the way you give myPolygon a value and the way in which you give all of the other member variables their values. (x, y, perim, total, myPaper and myPen)
Sorry, but that didn't help much - even though I've spent the last hour or so working on this problem. I'm still quite new to Java, so a more thorough explanation of what it is I need to do would be much appreciated!
So, I redid the IrregularPolygon Class, and here's the new error message I got:
Enter another point? (Y/N)Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
at java.util.ArrayList.RangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at IrregularPolygon.perimeter(IrregularPolygon.java:4 7)
at IrregularPolygonTester.main(IrregularPolygonTester .java:27)
Here's the slightly revised code: -
import java.awt.geom.*;
-
import java.util.ArrayList;
-
import gpdraw.*;
-
-
public class IrregularPolygon
-
{
-
private ArrayList <Point2D.Double> myPolygon = new ArrayList <Point2D.Double>();//this is what I changed
-
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 aPoint)
-
{
-
myPolygon.add(aPoint);
-
}
-
-
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;
-
}
-
}
Use this: -
public IrregularPolygon()
-
{
-
myPolygon = new ArrayList <Point2D.Double>();
-
x = 1;
-
y = 1;
-
perim = 0.0;
-
total = 0.0;
-
myPaper = new SketchPad(500,500);
-
myPen = new DrawingTool(myPaper);
-
}
-
Do you see the difference? Do you see why the myPolygon used in the toAdd() method will now have a value?
Our posts crossed! Your revised version does much the same thing as what I posted. Make sure you understand why you don't, now, get the error in the toAdd() method.
On to the ArrayIndexOutOfBoundsException...
You will get this one if an array is access at some index value that is too small (ie <0) or too big (ie >= the length of the array).
The stack trace tells you that this is occurring deep inside Java's ArrayList code. What you are really interested in is what in your code triggered this. You find that out by reading the stack trace from the top until you find a reference to your code.
In this case it is "at IrregularPolygon.perimeter(IrregularPolygon.java:4 7)". On this line you are accessing an array (or ArrayList) position that doesn't make sense (ie is too big or too small). -
for(int i = 0; i < myPolygon.size(); i++)
-
{
-
perim += ((Point2D.Double)myPolygon.get(i)).distance((Point 2D.Double)myPolygon.get(i + 1));
-
}
-
i goes from zero (which is the first vertex of the polygon) up to myPolygon.size()-1 which is the last vertex. Now ask yourself: if i is the last vertex of the polygon what is the expression myPolygon.get(i + 1) supposed to mean?
Thank you so much!! The program runs correctly now, with the exception of the area calculation...could you take a look at that algorithm for me?
Here's what it actually is:
0.5(X0*Y1 + X1*Y2 + ... + XN-1*Y0 - Y0*X1 - Y1*X2 - ... - YN-1*X0)
Can you check my area() method to see what might be giving me the incorrect output? I can't quite see what's wrong...maybe another set of eyes will help.
Here's that method in my program: - public double area()
-
{
-
for(int i = 0; i < myPolygon.size()-1; 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;
-
}
A picture might help. The following is a polygon with n sides. It doesn't "join up", but you didn't say it has to and that doesn't affect this problem. -
0 1 2 3 n-1 (array size is n)
-
*---*---*---*---*---*---*---*---*---*
-
How many ___ sides need to be added up to find the perimeter?
Again slow!
But I see in your area method you have i<myPolygon.size()-1, so well done!
I'll have a think about the area...
Here's your formula:
0.5(X0*Y1 + X1*Y2 + ... + XN-1*Y0 - Y0*X1 - Y1*X2 - ... - YN-1*X0)
Basically you have forgotten the bits in bold which depend on applying the "multiply and subtract" business to the last vertex and the first one. Maybe you can add this on after the for loop.
(and maybe you should do something analogous for the perimeter. come to think of it polygons are defined as being "closed up". So a polygon with n vertices does have n sides. And there will be n little triangles to consider when figuring out the area. don't worry about this last remark unless you see where the formula came from.)
@OP Please use code tags whenever you post code like pb-rock-way-2 was doing in his posts. His code is more readable than yours. This makes it easier for people trying to help you. It's also part of the site's posting guidelines.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Shamli |
last post by:
I am looking for an algorithm that enlarge a 2D polygon.
cheers,
|
by: BSOB |
last post by:
im gone for a long weekend so plenty of time for anyone to answer.
if i have 4 points representing a polygon and each point is represented by an x and a y coordinate, is there an easy (or slightly...
|
by: shihaoran |
last post by:
Thx for the help for my other two programs.
Here is a hard one; it is about makeing irregular polygon; i could not figure it out, help pls....
1. Implement a class IrregularPolygon that...
|
by: jojojjose |
last post by:
function draw()
{
var s='';
s+='<v:polygon points="';
s+= //points from server
s+="> </v:polygon>'";
document.wrie(s);
}
|
by: jojo41300000 |
last post by:
Hi,
Is anyone know that how to get the x and y points inside the polygon using C++ program?
I have the given polygon data to draw the polygon, but i don't know how to get all the points...
|
by: friendkitty |
last post by:
Hi All,
I m a new member here.I am now writing a program that render Vertex Normals.I m now trying to implement a data structure that will best suit computing vertex normals.For that computation ,...
|
by: =?Utf-8?B?U2ViYXN0aWFuIEZyw6Ruaw==?= |
last post by:
In several locations in the MSDN Mappoint Web Service SDK it says:
"Creating Polygons
There are several ways you can create and prepare polygon data for upload to
the Customer Service Site:
1....
|
by: moondaddy |
last post by:
I need to be able to make polygons with rounded corners. This will be to
draw group outlines around shapes in a diagramming tool in wpf. all angles
in the polygon will be 90 degrees, but somehow...
|
by: jpatchak |
last post by:
Hello,
I am having a problem using intersects method of a class I made that extends Polygon. Basically, I am trying to write code that allows the user to drag these polygons. I create a 1x1...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
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...
| |