Question: Complete the hasNext() and next() methods to enable this iteration. In case it is of use, String has two methods which may provide useful --

Complete the hasNext() and next() methods to enable this iteration. In case it is of use, String has two methods which may provide useful -- size() (returning the number of chars it contains) and charAt() (returning the character at the location passed as an argument).

package edu.uncc.cse; import java.util.Iterator; import java.util.NoSuchElementException; /** * A simple iterator class that can be used to access each character in a {@code String}. * */ public class CharacterIterator implements Iterator { /** The String whose characters will be returned by this Iterator. */ private String characterSource; /** The next character in the String which will be returned by the Iterator. */ private int cursor; /** * Create a new Iterator which can be used to go through the characters in this String. * * @param str Source of characters over which we will be iterating. */ public CharacterIterator(String str) { cursor = 0; characterSource = str; } /** Returns whether the iterator has a next object. */ public boolean hasNext() { } /** Returns the next object in the iterator. */ public Character next() throws NoSuchElementException { } public void remove() { throw new UnsupportedOperationException(); } } 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!