CS30 -- Introduction to Computer Science II
Fall, 2006
Homework 2
(updated 9/9/2006)

Update for Programming Project:  Generally, you should able to create your own driver class or include a main method in your class.  This should be a regular part of your development and testing.  But for this assignment, you might want to use this driver class that I am providing.  You might want to inspect this driver as I use it to demonstrate several Java techniques, including for loops, while loops, and autoincrement.

From the text (Budd's Classic Data Structures in Java), Chapter , pg 47:

Exercises:
Do number 2.

Programming Projects:

1.  Do the following (adapted from Data Structures & Other Objects Using Java, Mark Main):  Design and implement a pseudorandom number generator class.  We are going to think of a sequence of random numbers as a Collection.  Thus, your class should implement the Iterator interface (but of course you need not implement the remove() method) such that one can access random numbers from the sequence through the methods prescribed by the Iterator interface.

Computers cannot generate "truly" random numbers.  Instead, they compute a sequence of "pseudorandom" numbers that appear random.  The basic approach is the linear congruence method.  The sequence generated is dependent upon a seed, a multiplier, the increment, and the modulus.  The formula for generating the next pseudorandom number in the sequence is:

(multiplier * seed + increment) % modulus

The seed is initially set by one of several schemes; for our purposes, it is an input to the constructor.  After the first pseudorandom number has been generated, the seed is always set to the most recently generated number.  For example: with multiplier, increment and modulus values of 40, 3641 and 729 respectively, and an initial seed of 1, our first pseudorandom number would be:

(40 * 1 + 3641) % 729 = 36

and the seed is set to 36.  The next would be

(40 * 36 + 3641) % 729 = 707

with the seed now equal to 707.  And so on.

For this programming assignment, design and implement a class that can generate a pseudorandom sequence in the manner just described.  You will also write a constructor for this class that consumes four integer inputs.  Your arguments to your constructor should be in the order: initial seed, multiplier, increment and modulus.  You should provide a method, setSeed(int s), that has the effect of setting the seed to a given value.  Since your class will implement the Iterator interface, you will provide a method that generates and returns the next pseudorandom number given the current value of the seed and a method that indicates if there are more random numbers to be obtained.

Submission Instructions:
On your machine where you are doing your homework, create a folder called <your email name> followed by "A2".  For example, someone with email address "cjones" would create a folder called "cjonesA2".  Inside that folder, place plain text file(s) containing your answers to numbers 2.  Also, place whatever Java files are necessary for your Programming Projects in the same folder.  Finally, either tar or zip the folder so that when I extract it, the folder "<emailname>A2" will be created.  Finally, submit the packaged file for assignment 2 on Eureka.