Please help with a little problem
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.
Thank you for your help.
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 ();
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");
}
}