CS35 Homework #1: Intro to OOP

Due 11:59pm Wednesday, 12 September 2007

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. 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 BirthdayTest class in BirthdayTest.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.

Class Inheritance

For this part, you will implement a few classes related to automobiles as depicted in the figure below:

You will write your code in five files: Odometer.java, Auto.java, SportsCar.java, Truck.java, and AutoTest.java. Each class lists its fields beneath the class name, followed by a list of methods in that class. You will need to determine the types for the fields and return types for methods as well as the signature for each class's constructor. The AutoTest class should contain a main method that tests the features of the Auto, SportsCar, and Truck classes. For example, the following code

auto1.drive(300);
auto1.printAuto();

System.out.println("\n---Next auto---\n");
auto2.drive(7000);
auto2.printAuto();

System.out.println("\n---Next auto---\n");
auto3.drive(200);
auto3.drive(300);
auto3.printAuto();
Might produce the following output depending on how auto1, auto2, and auto3 are initialized.
2000 Toyota Corolla:
  Current Odo: 300
  Special Features: 
    * None

---Next auto---

Reached 3000 miles. Time for an oil change
Reached 6000 miles. Time for an oil change
2007 Mazda RX-8:
  Current Odo: 7000
  Special Features: 
     * Racing Tires

---Next auto---

2006 Ford F-250:
  Current Odo: 500
  Towing Capacity: 6 tons
  Special Features: 
     * Extended Cab
     * Monster Truck Tires

Some additional requirements/hints:



Submitting
Along with your java source code, you should hand in a file named README and your Makefile if you used one. 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.