• 12-02-2010, 03:00 PM
Yakg
Hi, I have this code:

public boolean addVertex (int x,int y){
Point pnt = new Point (x,y);
Point [] _vertices = new Point [MAX];
for (_noOfVertices=0; _noOfVertices<MAX && _vertices [_noOfVertices] !=null;)
_noOfVertices++;
if (_vertices [_noOfVertices] ==null)
_vertices [_noOfVertices] = pnt;
if (_noOfVertices <MAX)
return true;
return false;
}

The problem with it is, I want it to return false when the _noOfVertices reaches the MAX variable. However when I check it in a main class it still acts like it's true.

• 12-02-2010, 03:11 PM
Vinod Mukundan
if your
Code:

` if (_noOfVertices <MAX)`
is inside your for loop ... then I believe the max condition you will get is
Code:

` _noOfVertices == MAX`
and your if loop just checks the greater than condition rite ......

And how did you check the same in main class ??

Kindly format the code and use code tag and be more specific so that guys in here can help you out

warm regards
Vinod M
• 12-03-2010, 04:32 PM
Yakg
Here's the whole thing + main
class Polygon{

private Point _vertices [];
private int _noOfVertices = 0;
final int MAX = 10;

public Polygon (){
Point _vertices [] = new Point [MAX];
}

public boolean addVertex (int x,int y){
Point pnt = new Point (x,y);
Point [] _vertices = new Point [MAX];
while ( _noOfVertices<MAX && _vertices[_noOfVertices] !=null){
_noOfVertices++;
if (_vertices [_noOfVertices] == null)
_vertices [_noOfVertices] = pnt;
}
if (_noOfVertices<MAX)
return true;
return false;
}

------------------------------------------------------------------------
//main

class Tester{
public static void main (String args[]){

Polygon a = new Polygon ();
System.out.println ("yes");
System.out.println ("yes");
System.out.println ("yes");
System.out.println ("yes");
System.out.println ("yes");
System.out.println ("yes");
System.out.println ("yes");
System.out.println ("yes");
System.out.println ("yes");
System.out.println ("10");
System.out.println ("11");
System.out.println ("end");

}
}
• 12-06-2010, 07:03 AM
Vinod Mukundan
Code:

```class Polygon{ private Point _vertices []; private int _noOfVertices = 0; final int MAX = 10; public Polygon (){ Point _vertices [] = new Point [MAX]; } public boolean addVertex (int x,int y){ Point pnt = new Point (x,y); Point [] _vertices = new Point [MAX]; while ( _noOfVertices<MAX && _vertices[_noOfVertices] !=null){ _noOfVertices++; if (_vertices [_noOfVertices] == null) _vertices [_noOfVertices] = pnt; } if (_noOfVertices<MAX) return true; return false; }```

Code:

``` class Tester{ public static void main (String args[]){ Polygon a = new Polygon (); if (a.addVertex (3,4)); System.out.println ("yes"); if (a.addVertex (3,4)); System.out.println ("yes"); if (a.addVertex (3,4)); System.out.println ("yes"); if (a.addVertex (3,4)); System.out.println ("yes"); if (a.addVertex (3,4)); System.out.println ("yes"); if (a.addVertex (3,4)); System.out.println ("yes"); if (a.addVertex (3,4)); System.out.println ("yes"); if (a.addVertex (3,4)); System.out.println ("yes"); if (a.addVertex (3,4)); System.out.println ("yes"); if (a.addVertex (3,4)); System.out.println ("10"); if (a.addVertex (3,4)); System.out.println ("11"); if (a.addVertex (3,4)); System.out.println ("end"); } }```

Now tell were is the issue and how you want the output .......

Ok lets analyze ....

Your while loop will never be executed due to the condition _vertices[_noOfVertices] !=null.... this will always be null.

So the code never enters the while loop.Check what exactly you want to achieve.

Also you are defining _vertices [] as private and then again in Polygon() and addVertex (int x,int y) method .. You can define a variable private and then assign it in functions no need to redifine it in functions again so instead of
Code:

```public Polygon (){ Point _vertices [] = new Point [MAX]; } //you can give public Polygon (){  _vertices = new Point [MAX]; }```
Feel free to contact in case of any doubt...

Happy Coding .... :)
• 12-06-2010, 07:24 AM
Vinod Mukundan
See if this is what you want ....

Code:

```public class Polygon {     private Point _vertices[];     private int _noOfVertices = 0;     final int MAX = 10;     public Polygon() {         _vertices = new Point[MAX];     }     public boolean addVertex(int x, int y) {         Point pnt = new Point(x, y);         _vertices = new Point[MAX];         while (_noOfVertices < MAX && _vertices[_noOfVertices] == null) {             if (_vertices[_noOfVertices] == null)                 _vertices[_noOfVertices] = pnt;                 _noOfVertices++;         }         if (_noOfVertices < MAX)             return true;         return false;     } }```
By the way it will always return false only ......