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

Exercises:
From the text (Budd's Classic Data Structures in Java), Chapter 3, pg 66:
Do numbers 3 and 4.

Programming Projects:
1.  Purpose: improve understanding of constructors and inheritance, and working with arrays.  Build upon your pseudorandom number generator from last week.  (Again, this problem is adapted from Data Structures & Other Objects Using Java, Mark Main.)

Create a new class, call it UniformRandom, that extends your class from last week.  For purposes of this example, let's say you called your class MyRand.  Then you would start out with "public class UniformRandom extends MyRand {" and so on.

First, if your MyRand class only had one constructor that took four arguments, add a second (default) constructor that takes zero arguments but initializes the seed, multiplier, increment and modulus to some reasonable values.  (The values given in Assignment 2 would be good candidates.)  Someone using your new class should be able to create UniformRandom instances either without arguments or with the four integer args we encountered last week for the seed, multiplier, increment and modulus.

Next, override your next() method.  This new version of the method should return a container, or wrapper, class instance that holds a double type value between 0.0 (inclusive) and 1.0 (exclusive).  (Look in the java documentation under the java.lang package for an appropriate candidate.)  You can accomplish this by dividing the generated number by your modulus.  (Note, since the number generated by your method from last week is the remainder of a division by the modulus, it will never be greater than or equal to the modulus.  Therefore, when dividing by the modulus, the quotient will always be strictly less than 1.0.)  If one repeatedly calls this new method, it should generate a pseudorandom sequence in the range [0.0 .. 1.0).

Ideally, this is the only method that you will need to override from your previous class.  (That is, your UniformRandNum class might only contain a default constructor and the one method, nextElement().)  However, if you need or want to make changes to your version of MyRand, feel free to do so.  Be sure to include a copy of your old class with your submission whether you alter it or not.

Once your new method is generating appropriate numbers, run experiments that evaluate the distribution of resulting numbers.  You'll probably run these experiments from a main method, perhaps in another class.  Break the range [0..1) into ten smaller ranges: [0.. 0.1), [0.1 .. 0.2), ... [0.9 .. 1.0).  For a large sequence of numbers, say a million, accumulate the number of pseudorandom numbers that were generated in each of these smaller ranges.  Finally, print out a table such as this:

Range   Number of Occurances
[0.0..0.1)   99889
[1.0..0.2)   100309
...
[0.9..1.0)   99904

Run your experiment several times with different values for the multiplier, increment, and modulus.  A set of good choices for these will yield approximately 10% of the total in each range.  A generator with this equal-interval behavior is called uniformly distributed.  Try to find a set of values that produce a better distribution than the values recommended in Assignment 2.  (Note, the initial seed value is not too important in this regard as long as you have a large sample.  So don't worry too much about playing with that value.)

2.  Purpose: More arrays, more interfaces, and introduction to Neural Networks.  [updated 9/14/2006]  This portion of the assignment is postponed.  After you finish the rest of the assignment, I encourage you to think about this and begin on it.  But it will be part of a separate assignment.  Sorry for the confusion.

You will write the first part of a neural network.  A linear threshold unit (LTU) is the fundamental building block of a neural network.  This week, you will write a class that implements the functionality of a LTU.  These units compute a single output value based a number of inputs.  The output is determined by computing a linear weighted sum of their inputs and using this sum as the input to an activation function.  The activation function may be thought of as a using a threshold; if the activation is below the threshold, the output is 0, and if it is above the threshold the output is 1.  In other words, weighted sum is the input to the activation function, which is usually either a threshold function (as just described) or a sigmoid function (which we'll talk about later).

A single LTU can implement the boolean operators, AND, OR and NOT.  For example, a unit with two inputs (x1 and x2 with weights w1=1 and w2=1) and threshold of 1.5 will compute the logical conjunction (AND) of its two inputs (with input values either 1 or 0).

In fact, your class should implement the NeuralNetUnit interface that I provide.  There are currently eleven public methods prescribed by the interface.  At this stage, there are eight (two of which are deprecated) public methods that you should implement.  (For now, you should throw UnsupportedOperationException for the last three methods in the interface.  Later, we will implement those as well.)  Your inputs should be Integer wrapper objects containing 0/1 values for input to the LTU.

Your class should include a constructor that takes a single int value indicating how many inputs the unit combines.  For example, you might want to create an instance with two inputs and then set the weights and threshold according to the example above.  Or you can experiment with other boolean functions on two or more inputs.

Submission Instructions:
On your machine where you are doing your homework, create a folder called <your email name> followed by "A3".  For example, someone with email address "cjones" would create a folder called "cjonesA3".  Inside that folder, place plain text file(s) containing your answers to any exercises.  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>A3" will be created.  Submit your file via Eureka.