Define a class NumberIterator for iterating through
a sequence of numbers.
It should support the following methods.
public class NumberIteratorTest {
public static void run() {
NumberIterator it = new NumberIterator(5, 8);
IO.print(it.nextNumber());
while(it.hasMoreNumbers()) {
int i = it.nextNumber();
IO.print(" " + i);
}
}
}
public class NumberIterator {
private int current;
private int last;
public NumberIterator(int start, int stop) {
current = start;
last = stop;
}
public boolean hasMoreNumbers() {
return current <= last;
}
public int nextNumber() throws NoSuchElementException {
if(current <= last) {
current++;
return current - 1;
} else {
throw new NoSuchElementException();
}
}
}