Returning the Selected Ticket

A Java ticket iterator should return the ticket numbers 3, 7, 11 and 15, in that order. Its integer field cursor starts at 3. Assume the required exception class is imported.


The following methods compile, but next() contains a logic error:


Java
public boolean hasNext() {
    return cursor <= 15;
}

public Integer next() {
    if (!hasNext()) {
        throw new NoSuchElementException();
    }
    int saved = cursor;
    cursor += 4;
    return cursor;
}


Repair only the expression in the final return statement, leaving every other line unchanged. Complete the statements using one exact Java variable name and two exact integers.


The final return expression should be . After this repair, the first successful call to next() returns . Immediately after the fourth successful call, the field cursor contains .

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Iterable and Iterator Questions: