Create a polygone

• January 4th, 2011, 03:47 PM
r0x
Create a polygone
Hi all! My name is Dan and I'm first time here. Hope someone can help me with the task.
The exercice is from my exam and it sounds like this:

a) Define proper data structures to represent a polygone. The endpoints of the line segments are defined by two
float values. Make sure in the design of your data structures that an existing polygon can be easily modified by
dynamically adding and removing line segments.

b) Implement a function createPolygon that will create a new polygon based on an array of end points passed as
argument, such as for example
float endpoints[][2] = { {2.7, 5.4}, {1.1, 2.8}, {1.9, 1.3}};
• January 4th, 2011, 05:24 PM
Freaky Chris
Re: Create a polygone
Welcome r0x,

I'm afraid we cannot provide a solution for you, but if you show us what you have so far and what you are stuck on then we should be able to point you in the right direction with your problem.

Regards,
Chris
• January 6th, 2011, 11:16 AM
r0x
Re: Create a polygone
ok

Code :

```public class PolygoneMain { public static void main(String[] args) {   float[][2] endpoints = { {2.7,5.4}, {1.1,2.8}, {1.9,1.3} };   }   }```

Multiple markers at this line
- Type mismatch: cannot convert from double to
float
- Syntax error on token "2", delete this token
- Type mismatch: cannot convert from double to
float
- Type mismatch: cannot convert from double to
float
- Type mismatch: cannot convert from double to
float
- Type mismatch: cannot convert from double to
float
- Type mismatch: cannot convert from double to
float

1) why this array can't be float ?
• January 6th, 2011, 11:35 AM
KevinWorkman
Re: Create a polygone
2.7 is a double. 2.7f is a float. (float)2.7 is also a float.
• January 6th, 2011, 11:51 AM
r0x
Re: Create a polygone
ok thanks but I changed to double
so I was thinking may be an ArrayList would be a good choice so here is the code

Code :

```public class Point {   private double x; private double y;   public Point(double x, double y) { this.x = x; this.y = y; }   }```

Code :

```import java.util.*;   public class PolygoneMain {   public static void main(String[] args) {   ArrayList<Double> points = new ArrayList<Double>();   points.add(new Point(2.7, 5.4));   } }```

ERROR: The method add(Double) in the type ArrayList<Double> is not applicable for the arguments
(Point)

Here I don't understand how to solve the error, because basically the add method accepts Objects and Point for me it's an object right?
• January 6th, 2011, 11:55 AM
KevinWorkman
Re: Create a polygone
No. Your points ArrayList only accepts Doubles. A Point is not a Double, it contains Doubles (or doubles). Trying to add a Point to an ArrayList of Doubles won't work. Perhaps you want an ArrayList of Points instead?
• January 6th, 2011, 12:58 PM
r0x
Re: Create a polygone
I got it ) thank you
• January 6th, 2011, 01:04 PM
r0x
Re: Create a polygone
Code :

```public class Point {   private double x; private double y;   public Point(double x, double y) { this.x = x; this.y = y; }   }```

Code :

```import java.util.*;   public class PolygoneMain {   public static void main(String[] args) {   ArrayList<Point> points = new ArrayList<Point>();   points.add(new Point(2.7, 5.4)); points.add(new Point(3.1, 10.5)); points.add(new Point(1.1, 34.3));   System.out.println(points.size()); System.out.println("Contents of points: " + points);   } }```

Console:
3
Contents of points: [Point@268b819f, Point@10eb017e, Point@4d20a47e]

Why there is this abrakadabra as the contents of points ?
• January 6th, 2011, 01:18 PM
KevinWorkman
Re: Create a polygone
Because that's what's returned by Point.toString(), which is probably just using Object's toString().

What did you expect to print out? Java doesn't automatically know how to format a String version of an Object. For example, what do you think should happen if you did something like this:

Code :

```JFrame frame = new JFrame(); System.out.println("Frame: " + frame);```

If you want something else to print out, you'll have to get at that information yourself.
• January 6th, 2011, 01:41 PM
r0x
Re: Create a polygone
the problem is that I have no idea what toString is, I heard about this but never understood :-/
can u just write how can I see the contents of points ?
• January 6th, 2011, 01:43 PM
KevinWorkman
Re: Create a polygone

Java Platform SE 6 (that's a link, click it)
• January 6th, 2011, 01:48 PM
r0x
Re: Create a polygone
Code :

```// Custom toString() Method. public String toString() { return "X=" + x + " " + "Y=" + y; }```

ok, so I need to write my own version of toString method ? so that after in Main it could be overriden.
• January 6th, 2011, 01:51 PM
KevinWorkman
Re: Create a polygone
Quote:

Originally Posted by r0x
Code :

```// Custom toString() Method. public String toString() { return "X=" + x + " " + "Y=" + y; }```

ok, so I need to write my own version of toString method ? so that after in Main it could be overriden.

I suppose that's one way to do it. Or you could just use the getter functions in Point to print the values out yourself.

For example, say I have a Rectangle r, and I want to print its width. I would just do this:

Code :

`System.out.println("Width: " + r.getWidth());`
• January 6th, 2011, 01:58 PM
r0x
Re: Create a polygone
so the idea is that when I use System.out.println() this method needs toString to print the stuff ? and by default println takes Object's toString, am I right ?
• January 6th, 2011, 02:02 PM
KevinWorkman
Re: Create a polygone
Quote:

Originally Posted by r0x
so the idea is that when I use System.out.println() this method needs toString to print the stuff ?

Sort of. If you don't use a getter or some other function (like in my example), then println() is going to look at the toString() method to figure out what it should print.

Quote:

Originally Posted by r0x
and by default println takes Object's toString, am I right ?

Not necessarily. For example, many Objects (take Integer for example) override toString to provide a more meaningful String representation. If toString is not overridden by any of the parent classes, then the toString() inherited from Object is used.

Check out the API to see which classes override toString().
• January 6th, 2011, 02:06 PM
r0x
Re: Create a polygone
Thank you very much for explications!! This forum is cool \m/
• January 6th, 2011, 02:49 PM
r0x
Re: Create a polygone
Have another question, I put the code straight away to be more clear
Code :

`ArrayList<Point><Line> points = new ArrayList<Point><Line>();`

Is it possible to put in the same ArrayList objects of different type ? In this case Point and Line.
• January 6th, 2011, 03:02 PM
helloworld922
Re: Create a polygone
 oops, didn't read to the end :P

No, you can't do something like that. You could base type Point and Line and have ArrayList contain that base type.

Code Java:

```// I didn't define the classes, but you obviously would need to public class Geometry {}   public class Point extends Geometry {}   public class Line extends Geometry {}```

Then something like this would work:
Code Java:

```ArrayList<Geometry> geometries = new ArrayList<Geometry>(); geometries.add(new Line(/*with the correct constructor params*/)); geometries.add(new Point(/*with the correct constructor params*/));```

Additionally, all objects are base-typed as Object, so in theory you could create an ArrayList of Objects and it can hold anything, however this practice is generally frowned upon.
• January 6th, 2011, 06:54 PM
KevinWorkman
Re: Create a polygone
And to reiterate what helloworld922 said, you could also do something like this:

Code :

```ArrayList<Number> numbers = new ArrayList<Number>(); Integer i = new Integer(1); Double d = new Double(3.14); numbers.add(i); numbers.add(d);```

You can do this because both Integer and Double extend Number.

So since everything extends Object, you could have an ArrayList of Objects (or just don't user generics). But again, that's generally not the way to go.
• January 6th, 2011, 06:59 PM
r0x
Re: Create a polygone
big thanks for answers!!! so the polygone was created let's say in my way but there is still teacher's method (because he wanted to start off with the 2D array of endpoints), for now I don't understand what this line means, ok basically it's a 2D array of type float but what happens inside of curly braces it's a mystery for me. Also [][2] it means we have 2 columns and infinite rows ??

float endpoints[][2] = { {2.7, 5.4}, {1.1, 2.8}, {1.9, 1.3} };
• January 6th, 2011, 10:03 PM
helloworld922
Re: Create a polygone
Arrays in Java are always fixed size. You can create a new array to replace the old one, but you must fix the size of the array.

Actually that notation of [][2] isn't correct Java syntax. Instead, you must do one of the following:

Remove the 2 and let Java figure out the size the array must be.
Code Java:

```float endpoints[][] = { {2.7, 5.4}, {1.1, 2.8}, {1.9, 1.3} }; // Java will create a 3*2 array of floats float endpoints[][] = new float[][] { {2.7, 5.4}, {1.1, 2.8}, {1.9, 1.3} }; // this does the exact same thing```

Declare the size of all the dimensions.
Code Java:

`float endpoints[][] = new float[3][2]; // you can put values into endpoints either 1 by 1 or row by row`

If you want an "infinite" number of rows, you can either create an array of very large size (generally not a good idea) or use ArrayLists (see: http://www.javaprogrammingforums.com...t-example.html). Note that ultimately you are going to be limited by the amount of memory your system has (more specifically the maximum JVM heap size).

With Java arrays there is no way to enforce that the columns must be width 2 (at least without a wrapper class), however if you're careful you can enforce this de-facto.