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