473,396 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Irregular Polygon Program

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.
Expand|Select|Wrap|Line Numbers
  1. import java.awt.geom.*; // for Point2D.Double
  2. import java.util.ArrayList; // for ArrayList
  3. import gpdraw.*; // for DrawingTool
  4.  
  5. public class IrregularPolygon{
  6. private ArrayList <Point2D.Double> myPolygon;
  7.  
  8. // constructors
  9. public IrregularPolygon() { }
  10.  
  11. // public methods
  12. public void add(Point2D.Double aPoint) { }
  13.  
  14. public void draw() { }
  15.  
  16. public double perimeter() { }
  17.  
  18. 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:
Expand|Select|Wrap|Line Numbers
  1. import java.awt.geom.*;
  2. import java.util.Scanner;
  3.  
  4. public class IrregularPolygonTester
  5. {
  6.     public static String blah = new String("Y");//is this the proper way to do this?
  7.  
  8.     public static void main(String[] args)
  9.     {
  10.         IrregularPolygon myShape = new IrregularPolygon();
  11.         Scanner in = new Scanner(System.in);
  12.         while(blah.equals("Y"))//test if user wants to keep adding points
  13.         {
  14.             System.out.print("Enter X-Coordinate: ");
  15.             double x = in.nextDouble();
  16.             System.out.print("Enter Y-Coordinate: ");
  17.             double y = in.nextDouble();
  18.             Point2D.Double myPoint = new Point2D.Double(x, y);
  19.             myShape.toAdd(myPoint);//this line is causing problems
  20.             System.out.print("Enter another point? (Y/N)");
  21.             blah = in.nextLine();
  22.             blah.toUpperCase();
  23.             blah.trim();
  24.         }
  25.  
  26.         myShape.draw();
  27.         System.out.println("Perimeter of figure = " + myShape.perimeter());
  28.         System.out.println("Area of figure = " + myShape.area());
  29.     }
  30.  
  31. }

And here is the regular class file:
Expand|Select|Wrap|Line Numbers
  1. import java.awt.geom.*;
  2. import java.util.ArrayList;
  3. import gpdraw.*;
  4.  
  5. public class IrregularPolygon
  6. {
  7.     private ArrayList <Point2D.Double> myPolygon;
  8.     private double x;
  9.     private double y;
  10.     private double perim;
  11.     private double total;
  12.     private DrawingTool myPen;
  13.     private SketchPad myPaper;
  14.  
  15.     public IrregularPolygon()
  16.     {    
  17.         ArrayList <Point2D.Double> myPolygon = new ArrayList <Point2D.Double>();
  18.         x = 1;
  19.         y = 1;
  20.         perim = 0.0;
  21.         total = 0.0;
  22.         myPaper = new SketchPad(500,500);
  23.         myPen = new DrawingTool(myPaper);
  24.     }
  25.  
  26.     public void toAdd(Point2D.Double myPoint)
  27.     {
  28.         myPolygon.add(myPoint);
  29.     }
  30.  
  31.     public void draw()
  32.     {
  33.         myPen.up();
  34.         myPen.move(myPolygon.get(0).getX(), myPolygon.get(0).getY());
  35.         myPen.down();
  36.  
  37.         for(int i = 1; i < myPolygon.size(); i++)
  38.         {
  39.             myPen.move(myPolygon.get(i).getX(), myPolygon.get(i).getY());
  40.         }
  41.     }
  42.  
  43.     public double perimeter()
  44.     {
  45.         for(int i = 0; i < myPolygon.size(); i++)
  46.         {
  47.             perim += ((Point2D.Double)myPolygon.get(i)).distance((Point2D.Double)myPolygon.get(i + 1));
  48.         }
  49.         return perim;
  50.     }
  51.  
  52.     public double area()
  53.     {
  54.         for(int i = 0; i < myPolygon.size(); i++)
  55.         {
  56.             double X1 = (myPolygon.get(i).getX());
  57.             double Y1 = (myPolygon.get(i).getY());
  58.             double X2 = (myPolygon.get(i + 1).getX());
  59.             double Y2 = (myPolygon.get(i + 1).getY());
  60.             total += (X1 * Y2 - Y1 * X2);
  61.         }
  62.         return 0.5 * total;
  63.     }
  64. }

ANY help would be greatly appreciated! If you need any additional info, please let me know! Thanks in advance!
Oct 15 '09 #1
17 14267
pbrockway2
151 Expert 100+
@slapsh0t11
It would be more straight forward to say:

Expand|Select|Wrap|Line Numbers
  1. public static String blah = "Y";
  2.  
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).

Expand|Select|Wrap|Line Numbers
  1. myShape.toAdd(myPoint);//this line is causing problems
  2.  
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.
Oct 15 '09 #2
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)
Oct 15 '09 #3
pbrockway2
151 Expert 100+
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

Expand|Select|Wrap|Line Numbers
  1. myPolygon.add(myPoint);
  2.  

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?
Oct 15 '09 #4
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!
Oct 15 '09 #5
pbrockway2
151 Expert 100+
No - null does not mean "empty"! It's more like "nonexistent".

There is no actual array list (created with "new") to which myPolygon refers.
Oct 15 '09 #6
Isn't this the ArrayList to which myPolygon refers?

ArrayList <Point2D.Double> myPolygon = new ArrayList <Point2D.Double>();
Oct 15 '09 #7
pbrockway2
151 Expert 100+
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)
Oct 15 '09 #8
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!
Oct 15 '09 #9
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:
Expand|Select|Wrap|Line Numbers
  1. import java.awt.geom.*;
  2. import java.util.ArrayList;
  3. import gpdraw.*;
  4.  
  5. public class IrregularPolygon
  6. {
  7.     private ArrayList <Point2D.Double> myPolygon = new ArrayList <Point2D.Double>();//this is what I changed
  8.     private double x;
  9.     private double y;
  10.     private double perim;
  11.     private double total;
  12.     private DrawingTool myPen;
  13.     private SketchPad myPaper;
  14.  
  15.     public IrregularPolygon()
  16.     {    
  17.         //ArrayList <Point2D.Double> myPolygon = new ArrayList <Point2D.Double>();
  18.         x = 1;
  19.         y = 1;
  20.         perim = 0.0;
  21.         total = 0.0;
  22.         myPaper = new SketchPad(500,500);
  23.         myPen = new DrawingTool(myPaper);
  24.     }
  25.  
  26.     public void toAdd(Point2D.Double aPoint)
  27.     {
  28.         myPolygon.add(aPoint);
  29.     }
  30.  
  31.     public void draw()
  32.     {
  33.         myPen.up();
  34.         myPen.move(myPolygon.get(0).getX(), myPolygon.get(0).getY());
  35.         myPen.down();
  36.  
  37.         for(int i = 1; i < myPolygon.size(); i++)
  38.         {
  39.             myPen.move(myPolygon.get(i).getX(), myPolygon.get(i).getY());
  40.         }
  41.     }
  42.  
  43.     public double perimeter()
  44.     {
  45.         for(int i = 0; i < myPolygon.size(); i++)
  46.         {
  47.             perim += ((Point2D.Double)myPolygon.get(i)).distance((Point2D.Double)myPolygon.get(i + 1));
  48.         }
  49.         return perim;
  50.     }
  51.  
  52.     public double area()
  53.     {
  54.         for(int i = 0; i < myPolygon.size(); i++)
  55.         {
  56.             double X1 = (myPolygon.get(i).getX());
  57.             double Y1 = (myPolygon.get(i).getY());
  58.             double X2 = (myPolygon.get(i + 1).getX());
  59.             double Y2 = (myPolygon.get(i + 1).getY());
  60.             total += (X1 * Y2 - Y1 * X2);
  61.         }
  62.         return 0.5 * total;
  63.     }
  64. }
Oct 15 '09 #10
pbrockway2
151 Expert 100+
Use this:

Expand|Select|Wrap|Line Numbers
  1. public IrregularPolygon()
  2. {
  3.     myPolygon = new ArrayList <Point2D.Double>();
  4.     x = 1;
  5.     y = 1;
  6.     perim = 0.0;
  7.     total = 0.0;
  8.     myPaper = new SketchPad(500,500);
  9.     myPen = new DrawingTool(myPaper);
  10. }
  11.  
Do you see the difference? Do you see why the myPolygon used in the toAdd() method will now have a value?
Oct 15 '09 #11
pbrockway2
151 Expert 100+
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.
Oct 15 '09 #12
pbrockway2
151 Expert 100+
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).

Expand|Select|Wrap|Line Numbers
  1. for(int i = 0; i < myPolygon.size(); i++)
  2. {
  3. perim += ((Point2D.Double)myPolygon.get(i)).distance((Point 2D.Double)myPolygon.get(i + 1));
  4. }
  5.  
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?
Oct 15 '09 #13
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:
Expand|Select|Wrap|Line Numbers
  1.     public double area()
  2.     {
  3.         for(int i = 0; i < myPolygon.size()-1; i++)
  4.         {
  5.             double X1 = (myPolygon.get(i).getX());
  6.             double Y1 = (myPolygon.get(i).getY());
  7.             double X2 = (myPolygon.get(i + 1).getX());
  8.             double Y2 = (myPolygon.get(i + 1).getY());
  9.             total += (X1 * Y2 - Y1 * X2);
  10.         }
  11.         return 0.5 * total;
  12.     }
Oct 15 '09 #14
pbrockway2
151 Expert 100+
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.

Expand|Select|Wrap|Line Numbers
  1. 0   1   2   3                      n-1  (array size is n)
  2. *---*---*---*---*---*---*---*---*---*
  3.  

How many ___ sides need to be added up to find the perimeter?
Oct 15 '09 #15
pbrockway2
151 Expert 100+
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...
Oct 15 '09 #16
pbrockway2
151 Expert 100+
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.)
Oct 15 '09 #17
r035198x
13,262 8TB
@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.
Oct 15 '09 #18

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

Similar topics

2
by: Shamli | last post by:
I am looking for an algorithm that enlarge a 2D polygon. cheers,
6
BSOB
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...
4
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...
1
by: jojojjose | last post by:
function draw() { var s=''; s+='<v:polygon points="'; s+= //points from server s+="> </v:polygon>'"; document.wrie(s); }
3
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...
3
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 ,...
0
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....
6
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...
8
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
Oralloy
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,...
0
jinu1996
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...
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...

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.