Results 1 to 5 of 5
- 12-02-2010, 02:00 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 59
- Rep Power
- 0
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.
- 12-02-2010, 02:11 PM #2
if your
is inside your for loop ... then I believe the max condition you will get isJava Code:if (_noOfVertices <MAX)
and your if loop just checks the greater than condition rite ......Java Code:_noOfVertices == MAX
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 MLast edited by Vinod Mukundan; 12-02-2010 at 02:14 PM.
_______________________________________________
give me beans .........
- 12-03-2010, 03:32 PM #3
Member
- Join Date
- Dec 2010
- Posts
- 59
- Rep Power
- 0
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");
}
}
- 12-06-2010, 06:03 AM #4
Java 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; }
Java 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
Feel free to contact in case of any doubt...Java Code:public Polygon (){ Point _vertices [] = new Point [MAX]; } //you can give public Polygon (){ _vertices = new Point [MAX]; }
Happy Coding .... :)Last edited by Vinod Mukundan; 12-06-2010 at 06:15 AM.
_______________________________________________
give me beans .........
- 12-06-2010, 06:24 AM #5
See if this is what you want ....
By the way it will always return false only ......Java 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; } }_______________________________________________
give me beans .........


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks