Results 1 to 5 of 5
  1. #1
    Yakg is offline Member
    Join Date
    Dec 2010
    Posts
    59
    Rep Power
    0

    Default 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.

  2. #2
    Join Date
    Jul 2010
    Location
    India
    Posts
    58
    Rep Power
    0

    Default

    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.
    _______________________________________________
    give me beans .........

  3. #3
    Yakg is offline Member
    Join Date
    Dec 2010
    Posts
    59
    Rep Power
    0

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

    }
    }

  4. #4
    Join Date
    Jul 2010
    Location
    India
    Posts
    58
    Rep Power
    0

    Default

    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
    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.
    _______________________________________________
    give me beans .........

  5. #5
    Join Date
    Jul 2010
    Location
    India
    Posts
    58
    Rep Power
    0

    Default

    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 ......
    _______________________________________________
    give me beans .........

Posting Permissions

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