CS 240: Algorithms and Data Structures
James Madison University, Spring 2024

Introduction

In this lab, you will review and create three iterators and submit the assignment through Gradescope.

Files

Download the following files:

The file iterator_lab.zip contains all of these files in single download.

Part 1 - Review an Iteratable

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:

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

There is nothing to submit for this exercise.