1. Member Join Date
Dec 2010
Posts
88
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.  Reply With Quote

2. ## if your
Java Code:
` if (_noOfVertices <MAX)`
is inside your for loop ... then I believe the max condition you will get is
Java 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
Last edited by Vinod Mukundan; 12-02-2010 at 03:14 PM.  Reply With Quote

3. Member Join Date
Dec 2010
Posts
88
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 ();
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");

}
}  Reply With Quote

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 ();
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");

}
}```

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
Java 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 .... :)
Last edited by Vinod Mukundan; 12-06-2010 at 07:15 AM.  Reply With Quote

5. ## See if this is what you want ....

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;
}
}```
By the way it will always return false only ......  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•