CS35 Lab #1: Intro to Java

Due 11:59pm Monday, 29 January 2008

This homework will require you to write some basic classes in Java. You should save your programs in your cs35/homework/01 directory. This directory will appear if you run update35. The program handin35 will only submit files in this directory. For this assignment, you should work by yourself.

The Birthday Paradox

Do problem P-1.3 on page 56 in Goodrich and Tamassia. You should write a Birthday class in Birthday.java that generates a random valid birthday object. Only the month and the day are significant for this problem. See the documentation for java.util.Random for help in generating random numbers. There is also an example on page 107 in the text. To get a different sequence of random numbers each time you run the program, you can seed a new Random object. Example:

import java.util.Random;
...
Random rnGenerator = new Random(System.currentTimeMillis());

You should also write a TestBirthday class in TestBirthday.java that contains a main method that runs the experiment. Your output should be similar to the following (several rows omitted):

# People        # Experiments   # Positive      Pct
--------        -------------   ----------      ---
5               10              0               0.0
10              10              1               10.0
15              10              3               30.0
...
40              10              10              100.0
45		10              9               90.0
...

You can use the tab character \t to ensure your columns line up reasonably well. You should include a short summary of your experiments and results in a file called README.

Tips and Tricks
Start Early! Java can be tricky at first. If you have not programmed in Java before (this applies to most of the class), allow extra time on this assignment to learn Java. Write a few lines, then compile, fix bugs, and test.

All the top-down design principles from cs21 apply in any computer science course. Do not try to write the entire program in one go. Break the problem into managable pieces. One possible way of proceeding is to set up the loops that will create the table. Print out the heading and determine what variables/methods are required to print one row. In the first pass, you can populate the table with dummy data. Then go back and fill in the columns with real data that generate actual results.

Create only a single random number generator. Depending on your design, this may be in the Birthday class or the TestBirthday class, but the line that creates a new Random number generator should look something like this.
static Random rnGenerator = new Random(System.currentTimeMillis());
The keyword static is important here and makes sure you do not create a separate random number generator for each object/experimet. A possible source of confusion is that rnGenerator is a generator; it can generate multiple random numbers, not just a single value. Calling rnGenerator.nextInt(12) twice will generate two random numbers in the range 0 to 11, inclusive. For those coming from python, rnGenerator.nextInt(12) is equivalent to randrange(12).

The Birthday class should have a constructor Birthday() that takes no parameters and creates a random valid birthday. As part of your experiments, you will need to create and array of n random birthdays. This involves two steps: creating a Birthday array and populating it with random Birthdays. This can be done with the following code snippet:

int n=5; 

/*Create an array of n **empty** Birthday slots*/
Birthday[] bdays = new Birthday[n];

for(int i=0; i<n; i++){
  bdays[i]=new Birthday(); /*Create a new Birthday object*/
}
Another java gotcha is that you cannot compare objects directly. Well, you can, but it doesn't do what you expect. If you have two Birthday objects b1 and b2, then b1==b2 will simply check if b1 and b2 point or refer to the same object. If b1 and b2 refer to different objects, b1==b2 will return false, even if b1 and b2 have the same contents (month, day, etc..). You can only use == to compare base types. Most objects implement an equals(other) method to check for equality. This is true for Strings. To check if two strings are equal, you may have to use s1.equals(s2). If you want to compare two Birthdays, one option is to have methods in the Birthday class that return base types that can be compared using == or Strings that can be compared using equals(other). Another more advances option is to write your own public boolean equals(Birthday other) method in the Birthday class and then you can if two Birthdays are equal using b1.equals(b2).
Submitting
Along with your java source code, you should hand in a file named README. These files will be imported automatically via handin35. Your README should include a brief summary of your results for the Birthday program, along with any known problems/bugs in your code.

Once you are satisfied with your programs, hand them in by typing handin35 at the unix prompt. You may run handin35 as many times as you like, and only the most recent submission will be recorded. This is useful if you realize after handing in some programs that you'd like to make a few more changes to them.