CSci 2101 Labs 7. Iterators, queue.

Due Wednesday, March 19th<

35 points

The lab will be done in groups of 2-3 or individually.

Task 1: Random integers iterator.

Write a class RandomInts that produces n random integers between min (inclusive) and max (exclusive) as an iterator. This means that you can use it like this:

RandomInts myInts = new RandomInts(1, 101, 8);
for (int i: myInts){

The loop should give you 8 pseudo-random numbers uniformly distributed between 1 and 100 (inclusive).

The iterator in the class must implement Iterator<Integer>. The constructor for RandomInts takes 3 integers: the lower bound min, the upper bound max, and the number of random numbers needed n. Then each call to next() in the iterator returns a random integer between min and max (excluding the max). hasNext() returns true until n is reached, at which point it returns false. Note that different iterators for the same RandomInts class would generate different random integers.

Use only one instance of Random so that the numbers are guaranteed to be uniformly distributed. This means that Random should be an instance variable of the iterator, not a local variable in a method.

Task 2: Queue implementation (code reuse)

Write a class OurQueueImplementation that implements the OurQueue interface. Create a file in Eclipse, make your class implement the interface, and then use Eclipse QuickFix to add unimplemented methods. Make sure that element and remove return a type E and the iterator returns Iterable<E>.

Then write testing code for the queue in a file TestOurQueue. Write tests for all methods (including a for-each loop that tests the iterator), then start filling in the code. We will talk in class about the easiest way of implementting a queue.

Carefully follow method description. Make sure that exceptions are thrown as appropriate. Use try/catch blocks in your testing code to check for exceptions.

How to submit:

Send me your code. CC your partner if you worked in a pair.

CSci 2101 course web site.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.