Assignment 4: Overriding Methods and Applying the Birthday Class
Objective: Override instance methods and using a class you wrote.
Due: Before the next class (Friday).
Set Up
Create a directory for assign4
within
your cs209
directory. Copy your Birthday.java
file from last assignment into this assignment.
Part 1: Updating the Birthday Class
Overriding Methods
Add appropriate toString
and equals
methods. Make sure you use the appropriate signature for each
method, i.e., the same as the parent class's.
You will probably be tempted to have a Birthday
object
as a parameter to the equals
method you're writing.
However, the parameter should be an Object
object, to
match the parent Object
class's method.
Follow the procedure below for writing the equals
method:
- Use the == operator to check if the argument is a reference to this object. (If the variables are references to the same object, they're clearly equal!)
- Use the
instanceof
operator to check if the argument has the correct type. (Note: if a variable is a null reference, theninstanceof
will befalse
, so we don't need to check if the other object is null separately.) - Cast the argument to the correct type.
- For each "significant" field in the class, check if that field
of the argument matches the corresponding field of this object.
Note: for doubles, use
Double.compare
and for floats useFloat.compare
As always, make sure to test your methods appropriately. Save the
output from your tests in birthday.out
.
Part 2: Using the Birthday Class
Are you familiar with the birthday paradox? In a group of 23 randomly chosen people, there is more than 50% probability that some pair of them will both have been born on the same day. For 57 people, the probability is more than 99%.
Write a Java class (separate from the Birthday
class) that runs
an experiment that tests/verifies these probabilities. Name the file
BirthdayParadox.java
. Since Birthday.java
and BirthdayParadox.java
are in the same directory,
BirthdayParadox.java
does not need to
import Birthday.
For a set of people with randomly generated birthdays, determine if any of them have the same birthday. Because there is randomness involved, you need to run the experiment several times, each time with a new set of people with randomly generated birthdays. (30 is usually the magic number of experiments, but when you're first testing, you'll want to perform fewer experiments.)
Run the experiment with different numbers of people: from 5 to 100 people, in 5-person increments.
Note that a positive trial means that there was at least one pair of people with the same birthday. There could be more than one pair with the same birthday in a trial, but that only counts as one positive trial.
Print out the results of your experiment in a table. Example output:
# People # Trials # Positive Pct -------- -------- ---------- ------ 5 10 1 10.0 10 10 1 10.0 15 10 2 20.0 20 10 4 40.0 25 10 6 60.0 30 10 7 70.0 35 10 5 50.0 40 10 9 90.0 45 10 10 100.0 50 10 10 100.0 55 10 10 100.0 60 10 10 100.0 65 10 10 100.0 70 10 10 100.0 75 10 10 100.0 80 10 10 100.0 85 10 10 100.0 90 10 10 100.0 95 10 10 100.0 100 10 10 100.0
# People # Trials # Positive Pct -------- -------- ---------- --- 5 30 1 3.3333333333333335 10 30 5 16.666666666666668 15 30 8 26.666666666666668 20 30 12 40.0 25 30 17 56.666666666666664 30 30 21 70.0 35 30 23 76.66666666666667 40 30 27 90.0 45 30 30 100.0 50 30 27 90.0 55 30 30 100.0 60 30 30 100.0 65 30 30 100.0 70 30 30 100.0 75 30 30 100.0 80 30 30 100.0 85 30 30 100.0 90 30 30 100.0 95 30 30 100.0 100 30 30 100.0
In the above table, # Positive
is the number of times
at least two people had the same birthday out of all the times the
experiment was run.
Note: your new Java class should "see" your
Birthday
class if you used the public
keyword
before class
in Birthday.java
and
if the two .class
files are in the same
directory.
Use good program organization, e.g., write methods to make your code easier to read/understand.
Save the output from one run of your program using 30 trials in a
file named paradox.out
.
Students sometimes get overwhelmed by this problem. It is important that you break it into small steps to make sure that it works, then move on to the next part. (Recall the development process you described the first day of class.)
Extra Credit (3 pts)
Have your program take as a command-line argument the number of trials to perform. If the command-line argument is not given, default to 30 experiments. You may find Java's Integer class helpful.
Turning in Your Assignment
Copy your assign4
directory into your
turnin directory, using the turnin.sh
script.
Grading (80 pts)
You will be evaluated based on the correctness, efficiency, testing, and style of your programs:
- (30 pts) Part 1: Updating the Birthday class
- (50 pts) Part 2: Birthday paradox