# Problem with method

• November 8th, 2013, 10:15 AM
Slapy
Problem with method
Code :

```public abstract class SimplePolygon {   public abstract Vertex2D getVertex(int index) throws IllegalArgumentException; public abstract int getNumVertices();   public double getArea() {   int area = 0;   for (int i = 0; i < getNumVertices(); i++) {   area += (getVertex(i).getX() * getVertex(i + 1).getY() - getVertex(i + 1).getX() * getVertex(i).getY() ); }   return 0.5 * area; }   public double getWidth() {   double max = -Double.MAX_VALUE, min = Double.MAX_VALUE;   for (int i = 0; i < getNumVertices(); i++) {   max = Math.max(max, getVertex(i).getX() ); min = Math.min(min, getVertex(i).getX() ); }   return max - min; }   public double getHeight() {   double max = -Double.MAX_VALUE, min = Double.MAX_VALUE;   for (int i = 0; i < getNumVertices(); i++) {   max = Math.max(max, getVertex(i).getY() ); min = Math.min(min, getVertex(i).getY() ); }   return max - min; }   public double getLength() {   double length = 0;   for (int i = 0; i < getNumVertices(); i++) {   length += getVertex(i).distance(getVertex(i + 1)); }   return length; }   public String toString() {   StringBuilder result = new StringBuilder();   for (int i = 0; i < getNumVertices(); i++) {   result.append(" "+ getVertex(i).toString()); }   return "Polygon: vertices ="+ result; } }   public class ArrayPolygon extends SimplePolygon {   private Vertex2D[] vertices;   public ArrayPolygon(Vertex2D[] vertices) {   if (vertices == null) throw new IllegalArgumentException();   for (int i = 0; i < vertices.length; i++) {   if (vertices[i] == null) throw new NullPointerException(); }   this.vertices = new Vertex2D[vertices.length];   System.arraycopy(vertices, 0, this.vertices, 0, vertices.length); }   public Vertex2D getVertex(int index) throws IllegalArgumentException {   if (index < 0) throw new IllegalArgumentException();   return vertices[index % vertices.length]; }   public int getNumVertices() {   return vertices.length; }   public ArrayPolygon invert() {   Vertex2D[] temp = new Vertex2D[vertices.length];   for (int i = vertices.length - 1, y = 0; i >= 0; i--) {   temp[++y] = vertices[i]; }   return new ArrayPolygon(temp); }   public boolean compare(ArrayPolygon pol) {   int i = 0;   for (; i < vertices.length; i++) {   if (vertices[0].equals(pol.getVertex(i) ) ) break; }   System.out.println(i +"; "+ vertices.length); return true; } }   public class Vertex2D {   private double x,y;   public Vertex2D(double newX, double newY) {   x = newX; y = newY; }   public double getX() {   return x; }   public double getY() {   return y; }   public String toString() {   return "["+ x +", "+ y +"]"; }   public double distance (Vertex2D vert) {   if(vert == null) {   return -1.0; } else {   return Math.sqrt( (vert.x - x) * (vert.x - x) + (vert.y - y) * (vert.y - y) ); }   }   }   public class Tester {   public static void main(String[] args) {   Vertex2D[] vert1 = { new Vertex2D(-100,-100), new Vertex2D( -40, 10), new Vertex2D( 50, 20), new Vertex2D( 10, -20), new Vertex2D( 60, -40) };   Vertex2D[] vert2 = { new Vertex2D( 50, 20), new Vertex2D( 10, -20), new Vertex2D( 60, -40), new Vertex2D(-100,-100), new Vertex2D( -40, 10) };   Vertex2D[] vert3 = { new Vertex2D( -40, 10), new Vertex2D(-100,-100), new Vertex2D( 60, -40), new Vertex2D( 10, -20), new Vertex2D( 50, 20) };   Vertex2D[] vert4 = { new Vertex2D( -40, 10), new Vertex2D(-100,-100), new Vertex2D( 50, 20), new Vertex2D( 10, -20), new Vertex2D( 60, -40) };   System.out.println(new ArrayPolygon(vert1).compare(new ArrayPolygon(vert1))); System.out.println(""); System.out.println(new ArrayPolygon(vert1).compare(new ArrayPolygon(vert2))); System.out.println(""); System.out.println(new ArrayPolygon(vert1).compare(new ArrayPolygon(vert3))); System.out.println(""); System.out.println(new ArrayPolygon(vert1).compare(new ArrayPolygon(vert4))); } }```

I think when I run Tester.java it have to return something like 0; 5 \n true \n \n 3; 5 \n true \n \n 2; 5 \n true \n \n 2;5 \n true. But it is returning 0; 5 \n true \n \n 5; 5 \n true \n \n 5; 5 \n true \n \n 5;5 \n true. Whats wrong with my code? I am looking at it for a while and I can't come up to whats wrong. It's one file for each class.
• November 8th, 2013, 10:33 AM
Norm
Re: Problem with method
Can you explain what is wrong with the current output? What items are wrong?

If the current output and the desired output were put on lines one above the other, it would be easier to see what the differences are.
• November 8th, 2013, 10:51 AM
Slapy
Re: Problem with method
basically I am comparing two instances of class Vertex 2D.

Code :

```int i = 0;   for (; i < vertices.length; i++) {   if (vertices[0].equals(pol.getVertex(i) ) ) break; }   System.out.println(i +"; "+ vertices.length);```

so I take zero Vertex2D from vert1 and then I am searching for first match in vertex1, vertex2, vertex3 then vertex4 and that matches are 0 3 1 1 so I think output was to be 0 3 1 1 or 1 4 2 2 but real output is 0 5 5 5 and thats a problem and I don't know why it is not working. :-(
Code :

``` Vertex2D[] vert1 = { new Vertex2D(-100,-100), new Vertex2D( -40, 10), new Vertex2D( 50, 20), new Vertex2D( 10, -20), new Vertex2D( 60, -40) };   Vertex2D[] vert2 = { new Vertex2D( 50, 20), new Vertex2D( 10, -20), new Vertex2D( 60, -40), new Vertex2D(-100,-100), new Vertex2D( -40, 10) };   Vertex2D[] vert3 = { new Vertex2D( -40, 10), new Vertex2D(-100,-100), new Vertex2D( 60, -40), new Vertex2D( 10, -20), new Vertex2D( 50, 20) };   Vertex2D[] vert4 = { new Vertex2D( -40, 10), new Vertex2D(-100,-100), new Vertex2D( 50, 20), new Vertex2D( 10, -20), new Vertex2D( 60, -40) };```
• November 8th, 2013, 11:07 AM
Norm
Re: Problem with method
Quote:

output is 0 5 5 5
What do the values of those 4 digits mean? What is the 0 and each 5?

Should the search loop determine that no match was found and print out a different message instead of always printing the same message even if not found?
• November 8th, 2013, 11:19 AM
Slapy
Re: Problem with method
it's value of int i but like I said output have to be 0 3 1 1 or 1 4 2 2 not 0 5 5 5 and there is no case when match is not found so I think it's not necessary to fix.

Edit: so value of i have to be turn of for when match is found and then I can easily found in which array is match.
• November 8th, 2013, 11:25 AM
Norm
Re: Problem with method
Quote:

not necessary to fix.
Are you sure that there is always a match found in the loop?

What do the values of the 4 digits: 0 5 5 5 represent?

When I compile and execute the code, I get this output which is different from what you posted:
Quote:

0; 5
true

5; 5
true

5; 5
true

5; 5
true
What is wrong with this output?
• November 8th, 2013, 11:49 AM
Slapy
Re: Problem with method
yes I am sure and like I said values of 4 digits: 0 5 5 5 have to be numbers of variable in array with match but it doesn't.

--- Update ---

It's just little bit different. First number have to be number of match on array second is lenght of array and true is just true. But like I said first numbers have to be 0 3 1 1 or 1 4 2 2 and not 0 5 5 5.

Edit: In this post, second code block are arreys about which I am talking.
• November 8th, 2013, 11:51 AM
Norm
Re: Problem with method
Try some debugging. I debug by using the println() method to print out the values of variables as the code executes. Add some println() statements to the compare() method to see what values the code is looking at as it runs the loop.
• November 8th, 2013, 11:53 AM
Slapy
Re: Problem with method
Sorry for duplicate I don't know how to delete it.
• November 8th, 2013, 11:58 AM
Norm
Re: Problem with method
What prints out when you add the println() statements to the compare() method and execute the code?
• November 8th, 2013, 11:59 AM
Slapy
Re: Problem with method
Code :

`System.out.println(vertices[0] +"; "+ pol.getVertex(i));`
before
Code :

`if (vertices[0].equals(pol.getVertex(i) ) ) break;`
and you will see it have to break that shitty for but it doesn't I just don't get it why.
• November 8th, 2013, 12:04 PM
Norm
Re: Problem with method
Sorry, I don't understand what you are saying?

What prints out when you execute the code?

I still think the code should say if it does not find a match instead of printing the same type of message.
Or does 5;5 mean that there was no match? Makes for confusion to not specifically say: NO MATCH
• November 8th, 2013, 12:29 PM
Slapy
Re: Problem with method
So that if have to break for if there are same values as you can see in first one it's all ok but in another ones it just failed I don't know why.
Code :

```[-100.0, -100.0]; [-100.0, -100.0] 0; 5 true   [-100.0, -100.0]; [50.0, 20.0] [-100.0, -100.0]; [10.0, -20.0] [-100.0, -100.0]; [60.0, -40.0] [-100.0, -100.0]; [-100.0, -100.0] [-100.0, -100.0]; [-40.0, 10.0] 5; 5 true   [-100.0, -100.0]; [-40.0, 10.0] [-100.0, -100.0]; [-100.0, -100.0] [-100.0, -100.0]; [60.0, -40.0] [-100.0, -100.0]; [10.0, -20.0] [-100.0, -100.0]; [50.0, 20.0] 5; 5 true   [-100.0, -100.0]; [-40.0, 10.0] [-100.0, -100.0]; [-100.0, -100.0] [-100.0, -100.0]; [50.0, 20.0] [-100.0, -100.0]; [10.0, -20.0] [-100.0, -100.0]; [60.0, -40.0] 5; 5 true```

and what I want is
Code :

```[-100.0, -100.0]; [-100.0, -100.0] 0; 5 true   [-100.0, -100.0]; [50.0, 20.0] [-100.0, -100.0]; [10.0, -20.0] [-100.0, -100.0]; [60.0, -40.0] [-100.0, -100.0]; [-100.0, -100.0] 3; 5 true   [-100.0, -100.0]; [-40.0, 10.0] [-100.0, -100.0]; [-100.0, -100.0] 1; 5 true   [-100.0, -100.0]; [-40.0, 10.0] [-100.0, -100.0]; [-100.0, -100.0] 1; 5 true```

--- Update ---

So problem was Vertex2D is my own class and I have to do my own equals for comparing this classes.
• November 8th, 2013, 12:30 PM
Norm
Re: Problem with method
Quote:

I have to do my own equals for comparing this classes.
Yes, you need your own method to compare the contents of your own class.