Assignment 3: Creating Classes; Overloaded and Static Methods

Objective: Create Basic Java classes and write overloaded and static methods.

Due: Before the next class (Wednesday).

Save the output from your programs in a reasonably named file.

Set Up

Create a directory for assign3 within your cs209 directory.

Part 1: Static Methods

Write a class with static methods that manipulate strings. Here are the definitions of the static methods:

/**
 * @param s the String to reverse
 * @return the string backwards; e.g., string --> gnirts
 */
public static String reverseString(String string)
/**
 * Tests if a string is a palindrome.  A palindrome is a word
 * that is the same fowards and backwards.
 * Some palindromes: 
 * "kayak"
 * "A man A plan A canal Panama"
 * In your algorithm, you don't need to consider spaces or 
 * punctuation as special cases, i.e., "A man A plan A canal Panama"
 * will return false.  You should consider
 * upper and lower case letters as the same.
 * @param s the string to test if it's a palindrome
 * @return true iff the string is a palindrome
 * @see http://www.palindromelist.com
 */
public static boolean isPalindrome(String string)

In a comment, answer Why is it appropriate for these methods to be static?

Use methods from the String class to help you write the isPalindrome method.

Your main method should test these methods. As usual, save the output in an appropriately named file.

Extra Credit (8 pts)

Write isPalindrome space efficiently, i.e., do not just reverse the string and compare to the original string. If you have questions about the efficiency of your algorithm, please ask.

Part 2: Modifying the Birthday Class

Overloaded Constructor

Add another constructor to the Birthday class that takes no parameters and randomly generates the month and date.

You will need a static array of integers representing the number of days in a month. (For this assignment, assume that February has 29 days.) Also, you want only one random number generator for the whole 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.

Testing

Test the new methods in your main method. Save the output in an appropriately named file.

Part 3: 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. 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 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.

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

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 in an appropriately named file.

Extra Credit (8 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 assign3 directory into your turnin directory.

There is no printed part of this assignment.

Grading (100 pts)

You will be evaluated based on the correctness, testing, and style of your programs: