Lab 2: Practicing Iterator/Iterable Implementation
In this lab, you will review and create three iterators and submit the assignment through Gradescope.
Files
Download the following files:
- BackScanner.java (Completed)
- IteratorDriver.java (Completed)
- SkipScanner.java
- NGramScanner.java
- SkipScannerTest.java
- SkipScannerGenericTest.java
- NGramScannerTest.java
The file iterators.zip contains all of these files in single download.
Package Declaration
The files do NOT include a package declaration!
You are free to put the files in any package you like. We recommend labs.iterators or something similar.
Part 1 - Review an Iterable
Take a few minutes to read over the file BackScanner.java to make sure you understand
how it works. You can test it using the provided driver (IteratorDriver.java)
Part 2 - Implement a Skip Iterator
Complete SkipScanner.java so that it conforms to the provided
javadoc comments. You can test your iterator with the
SkipScannerTest JUnit tests.
Part 3 - Implement a Skip Iterator with Generics
Make a copy of your working SkipScanner.java class to a new file named
SkipScannerGeneric.java. Change the SkipScannerGeneric class
so that instead of accepting an array of Strings it will accept an array
containing any type of object.
Part 4 - Implement a NGram iterator
Complete NGramScanner.java so that it conforms to the provided
javadoc comments. You can test your iterator with the
NGramScannerTest JUnit tests.
Lab Submission
Submit the following files through Gradescope:
SkipScanner.javaNGramScanner.javaSkipScannerGeneric.java
Going Further
If you have extra time, implement a PrimesGenerator class that
implements Iterable<Integer>. Your class should make it possible to
iterate over prime numbers as in the following example:
public static void main(String[] args) {
for (int p : new PrimesGenerator(10)) {
System.out.println(p);
}
}
In this case, the expected output would be the first ten primes:
2
3
5
7
11
13
17
19
23
29