Question

This file that has the outline of an iterator which will be used to return each...

This file that has the outline of an iterator which will be used to return each char within a String. 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).

import java.util.Iterator;
import java.util.NoSuchElementException;

public class CharacterIterator implements Iterator<Character> {
  /** 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();
  }
}

Homework Answers

Answer #1

Here are the two methods filled for you:

import java.util.Iterator;
import java.util.NoSuchElementException;

public class CharacterIterator implements Iterator<Character> {
/** 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() {
   return cursor < characterSource.length();
}

/** Returns the next object in the iterator. */
public Character next() throws NoSuchElementException {
   cursor++;
   return characterSource.charAt(cursor-1);
}

public void remove() {
throw new UnsupportedOperationException();
}
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
This is the java code that I have, but i cannot get the output that I...
This is the java code that I have, but i cannot get the output that I want out of it. i want my output to print the full String Form i stead of just the first letter, and and also print what character is at the specific index instead of leaving it empty. and at the end for Replaced String i want to print both string form one and two with the replaced letters instead if just printing the first...
Write a program that reads in a line consisting of a student’s name, Social Security number,...
Write a program that reads in a line consisting of a student’s name, Social Security number, user ID, and password. The program outputs the string in which all the digits of the Social Security number and all the characters in the password are replaced by x. (The Social Security number is in the form 000-00-0000, and the user ID and the password do not contain any spaces.) Your program should not use the operator [] to access a string element....
Step 1: Edit StringExplode.java Download the StringExplode.java file, and open it in jGrasp (or a text...
Step 1: Edit StringExplode.java Download the StringExplode.java file, and open it in jGrasp (or a text editor of your choice). This program will “explode” a String into an array of characters (char[]), and then print out the array. The String comes from the first command-line argument. Example output with the command-line argument foo is below: f o o --------------------------------------------------------------------- public class StringExplode { // TODO - write your code below this comment. // You will need to write a method...
Sort by the following (name, address, dependent and gender) of these and ask the user which...
Sort by the following (name, address, dependent and gender) of these and ask the user which field to sort by !. this mean the following java must sort by address if we need, by name , by dependent , and by gender it depend on the following java it must have an option which we need to sort. please i need your help now, you just add the sorting on the following java. // Use a custom comparator. import java.io.BufferedReader;...
Assignment 1 - ITSC 2214 I have to complete the array list for a shopping list...
Assignment 1 - ITSC 2214 I have to complete the array list for a shopping list code and can't figure out how to complete it: Below is the code Complete the three empty methods (remove(), find(), and contains()) in the ShoppingListArrayList.java file. These methods are already implemented in the ShoppingListArray class. /////////////////////////////////////////////////////////////////////////////////////////////////////////// Grocery Class (If this helps) package Shopping; public class Grocery implements Comparable<Grocery> { private String name; private String category; private int aisle; private float price; private int quantity;...
The language is Java. Using a singly-linked list, implement the four queue methods enqueue(), dequeue(), peek(),...
The language is Java. Using a singly-linked list, implement the four queue methods enqueue(), dequeue(), peek(), and isEmpty(). For this assignment, enqueue() will be implemented in an unusual manner. That is, in the version of enqueue() we will use, if the element being processed is already in the queue then the element will not be enqueued and the equivalent element already in the queue will be placed at the end of the queue. Additionally, you must implement a circular queue....
def clear(file_path: str): """ Clears the file at the specified file path. :param file_path: A path...
def clear(file_path: str): """ Clears the file at the specified file path. :param file_path: A path to a file :return: None """ def delete_last_line(file_path: str) -> str: """ Removes the last line in the file at the specified file path. Then it saves the new value with the last line removed to the file. Finally it returns the deleted last line. If the file has nothing in it an empty string ("") is returned. :param file_path: A path to file...
Task 1: You will modify the add method in the LinkedBag class.Add a second parameter to...
Task 1: You will modify the add method in the LinkedBag class.Add a second parameter to the method header that will be a boolean variable: public boolean add(T newEntry, boolean sorted) The modification to the add method will makeit possible toadd new entriesto the beginning of the list, as it does now, but also to add new entries in sorted order. The sorted parameter if set to false will result in the existing functionality being executed (it will add the...
Finish the CustomerAccountTransactions program that reads customer accounts receivable data from a file and then applies...
Finish the CustomerAccountTransactions program that reads customer accounts receivable data from a file and then applies a series of transactions to the accounts. Its main() method uses the CustomerAccounts class to manage a collection of CustomerAccount objects: reading customer accounts data & transactions, and obtaining a String showing the customer accounts data after the operations are complete. You will need to complete the readCustomerAccounts () and applyTransactions() methods in the CustomerAccounts class. First, please fill in your name in the...
C++ See the provided specification files. Complete the implementation for each as a separate file. void...
C++ See the provided specification files. Complete the implementation for each as a separate file. void seen(std::string); If there is already a Word object in the Words list, then the number of occurrences for this word is incremented. If there is no Word object for this word already, create a new word object with occurrence =1, and insert this object into the list of Word objects. std::string getNextWord(); Returns the next word of the list and sets the currentItem pointer...