0

I'm trying to write an function named boolean hasNext() which checks if there is another element after the current one or not I have an class named TourElement, it contains a lot of points. Here is my code // class waypoint:

public class Waypoint {
    int x  ;
    int y  ;
    public int getX()
    {
        return this.x;
    }
    public int getY()
    {
        return this.y;
    }
    public void setXY(int x, int y)
    {
        this.x = x;
        this.y = y;
    }

//class tourElement

 public class TourElement {
     private Waypoint points;
     private TourElement next;

      public void setWaypoint( Waypoint points){
       this.points = points; 
     }
      public void setTourElement(TourElement next) {
          this.next = next;
      }
     Waypoint getWaypoint() {
         return this.points;
     }

     TourElement getNext(){
         return this.next;
     }

    boolean hasNext(Waypoint first){
    // What am I doing wrong here?
        TourElement current = getNext();
        while( current.next != null)
        {
            return true;
        }
        return false;

    }
    // my test case
         public void testHasNext()
        {
           TourElement elem = createElementList(new int[][] {{0, 0}, {1, 1}, {2, 2}});

            assertEquals(true,elem.hasNext(createWaypoint(1, 1)));
        }

//create element list:

private TourElement createElementList(int[][] waypoints){
        assert waypoints.length > 0;
        TourElement elem = new TourElement();
        int lastIndex = waypoints.length-1;
        Waypoint wp = createWaypoint(waypoints[lastIndex][0], waypoints[lastIndex][1]);
        elem.setWaypoint(wp);
        for (int i = lastIndex-1; i >= 0 ; i--) {
            wp = createWaypoint(waypoints[i][0], waypoints[i][1]);
            elem = elem.addStart(wp);
        }
        return elem;
    }

// create waypoint:

private Waypoint createWaypoint(int x, int y) {
        Waypoint wp = new Waypoint();
        wp.setXY(x, y);
        return wp;
    }

I expect that with my hasNext Function, if I pass an point like {1,1}, it will return true because there is one more point after this point. but when I pass{2,2}. it will return false

2 Answers2

0

Your method can be improved. Take a look

boolean hasNext(){
    if (this.next != null) return true;
    return false;
}

You don't need to loop to the end of the LinkledList, you should only care about what is next.

MrBuggy
  • 423
  • 4
  • 12
0

This line:

assertEquals(true,elem.hasNext(createWaypoint(1, 1)));

Will always fail the assertion, because you're creating a new waypoint, and then attempting to find it in the list of existing waypoints. But this new waypoint isn't in the list.

The only waypoints in the list are the ones that you added, and this is a brand new waypoint that just happens to have the same x and y values as one of the waypoints in your list.

You don't include the code for the TourElement.addStart method, so I can't tell if there's any issue there. And others have pointed out that you don't need a loop inside hasNext. But the main issue here is that you need to do something other than use the WayPoint passed into the hasNext method. This will likely involve traversing the waypoint graph trying to find an existing WayPoint with the same x and y values as the one passed into the method, and then checking to see if that has a next.

Jordan
  • 2,273
  • 9
  • 16