CS351 - Design of Large Programs - Professor Ackley

Tuesday and Thursday: 11:00am - 12:15pm (Centennial B146) Labs: Monday (001) 1:00pm - 1:50pm and Thursday (005) 12:30pm - 1:20pm (Centennial B146)

Lab #10: Iterator

Goals

  • Use custom Iterators by implementing java.lang.Iterable<T> and java.util.Iterator<T>

Needs


Background

Last class, Professor Ackley introduced the idea of iterators and how they are used to iterate over a collection of some sort of object. For many of the standard collections that we use (i.e. LinkedList, HashSet, TreeSet etc.) these iterators are built in. Therefore, if we have a class who's sole "collection" attribute is just a java.util.Collections list, map etc., we can just delegate the iteration using the iterator of the java.util.Collections object.

SEE PROFESSOR ACKLEY'S CLASS NOTES.

However, what if we have our own custom collection of something that we want to be able to distinguish a way to iterate over and delegation is impossible (i.e. the custom collection doesn't have a standard java.util.Collections object holding all of the collected objects)? This is when a custom iterator is needed for the sleek

for (IterateOver i : customIterableCollection)

for-loop.

Whatever the custom collection is will have to implement java.lang.Iterable<T>. This says, this collection is allowed to have an iterator. More specifically, the T generic is the type of object in which the collection will hold.


//TODO

A Book is a collection of things. What type of things specifically?

  • Letters
  • Words
  • Paragraphs
  • Pages

You need to create a com.putable.labs.lab10.Book class which implements java.lang.Iterable<T>. Since T is the generic that specifies what the Book holds, this can either be com.putable.labs.lab10.Word, com.putable.labs.lab10.Paragraph or com.putable.labs.lab10.Page. It's up to you; however, I HIGHLY suggest you iterate over Paragraph.

Next, you must have an inner class com.putable.labs.lab10.Book.'T'Iter which implements java.util.Iterator<T> where 'T' (in both cases) is the generic type that you decided to iterate over (Words, Paragraphs or Pages). When implementing java.util.Iterator<T>, you are forced to @Override 3 methods:

  1. public boolean hasNext();
  2. public Paragraph next(); => if we are iterating over Paragraph
  3. public void remove();
public void remove(); is an OPTIONAL OPERATION. You don't need to actually do anything with this except throw a java.lang.UnsupportedOperationException.UnsupportedOperationException().

Your final goal is to allow a driver loop through an instance of your com.putable.labs.lab10.Book object as it iterates over each of your T generic types contained in the Book and prints them to standard out. Also, If you decided that a Book contains Words, the output must print a single _ character in between each word. If you decided that a Book contains Paragraphs, the output must print a ¶ character in between each paragraph. Finally, if you decided that a Book contains Pages (which is nice but might be hard for this assignment), the output must print exactly 3 ∼ characters in sequence with each other in between each page.

The driver should be a single main() method that exists inside of com.putable.labs.lab10.Book. The "sleek/enhanced" foreach loop must be used.

An example of an expected output when a Book was decided to contain Paragraphs is shown HERE.


NTK

  • The book you will be using is "The Gold-Bug" by Edgar Allen Poe. Load it using a File like you know how.
  • _ is ascii code 95
  • ¶ is ascii code 182
  • ∼ is ascii code 8764


Extra Credit Opportunity

You might notice that this assignment can be accomplished by delegating the iterator to a collection that is used inside of the com.putable.labs.lab10.Book. For example, you could just ask the a List that holds all of the Book's Paragraphs for the iterator. For some undisclosed amount of extra credit, you can turn in an additional implementation of Book that uses a java.util.Collections object's iterator as the iterator of the Book. This way, the "sleek/enhanced" foreach loop is not used, but the Java 1.4 style is used instead:

for (Iterator i = book.iterator(); i.hasNext();)

is used.

A nice way to differentiate between these two implementations is to think of the custom iterable as a type of lazy evaluation. That is, at the initialization of the Book, a member collection variable of some sort isn't being built up with all of the Paragraphs right away; instead, the parsing of the Book contents is done only when the caller asks for the next Paragraph. In the other implementation where we delegate the iterator to a collection variable inside of the class, all of the calculation is done in the initialization of the Book (all Paragraphs are parsed and placed in a List) and just iterating over the list using its own iterator is enough.

If you should decide to implement this extra credit, you will create a com.putable.labs.lab10.XCBook in addition to com.putable.labs.lab10.Book.


Turn in a Iterator.jar file containing at least com.putable.labs.lab10.Book class. The main() method should live in this class. You should also have one of the following: com.putable.labs.lab10.Word, com.putable.labs.lab10.Paragraph or com.putable.labs.lab10.Page. Make sure to turn in the java source files otherwise you will get a 0.

The Iterator.jar should be turned in using the online interface by 11:59:59 PM of your lab day.