Assignment 7/8

CSIS 10B
Assignment 7 and 8 Design and Iterators
Due: next Wed
In Class Demo (0 points):
1) Inheritance, abstract classes and interfaces
a. Employee and subclasses
b. Generator classes: randomIntGenerator
c. Card and subclasses
2) Iterators, Using Iterator
a. Listing a vector of String with an iterator
b. Using UniqueIterator to show only unique strings in a vector
c. Generating prime factors with an iterator
Assignment (10 points) Do either Basic or Advanced.
Basic
1) Making a StringIterator class. This class will implement Iterator<Character> since each value it returns will
be of type Character. The constructor will accept a String and will save it to an instance variable myString.
Every call to next will return the next letter in myString. Remember: in your next method to retrieve a letter
from myString use the charAtmethod ( myString.charAt(k), where k holds the position of the letter you
want) and you’ll need to cast the return value as Character to return Character not char.
Use the following to demonstrate your StringIterator. When run it displays “hello world” one letter at a time
vertically on the screen:
public static void main(String [] args){
System.out.println("\f");
String test = "hello world";
StringIterator si = new StringIterator(test);
while ( si.hasNext()){
Character c = si.next();
System.out.println(c);
}
}
2) Write a filtering iterator, ReverseIterator, that reverses the stream of values produced by another Iterator.
You may assume that the base Iterator will eventually have no more elements, but you may not bound the
number.
Inside this iterator’s constructor you’ll need to create a vector and dump all the values of the base iterator
into it. Then your next method will iterate through your internal vector backwards. Write a test method to
verify it works on a Vector of String, similar to the UniqueFilter demo.
3) Extra Credit (1 point): Create an UlamGenerator class that implements Iterator<Integer> (see PFgenerator
for an example). This class generates the Ulam sequence starting with a base number provided to the
constructor.
The Ulam Sequence is a sequence where each number n is succeeded by a new value calculated as follows:
If n is 1, stop.
If n is even, the next number is n/2.
If n is odd, the next number is 3*n + 1.
And continues with this process until reaching 1. (at this point your hasNext method should return false).
As an example, suppose UlamGenerator u = new UlamGenerator (5)
Then the statements:
while( u.hasNext())
System.out.print( u.next() + " -> ");
will print:
5 -> 16 -> 8 -> 4 -> 2 -> 1 ->
while an UlamGenerator (7) iterator in same code above will print:
7 -> 22 -> 11 -> 34 -> 17 -> 52 -> 26 -> 13 -> 40 -> 20 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1 ->
Advanced: Two Towers
Do the lab starting on page 175 of your text