Skip to main content.

Assignment 3: Overloaded and Static Methods

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

Due: Before the next class (Wednesday).

Set Up

Create a directory for assign3 within your cs209 directory.

Part 1: Modifying the Birthday Class

Copy Birthday.java from your assign2 directory to your assign3.

Overloading Constructors

Create a new constructor for the Birthday class. This constructor takes no parameters and randomly generates the birthday (the month and date).

To create the random birthday, you may need a static, final array of integers representing the number of days in a month. (What does the index of that array represent?) For this assignment, assume that February has 29 days. Also, you want only one random number generator object for the whole Birthday class to ensure the best random number generation. (We can debate about whether a uniform distribution is the correct way to generate birthdays, but for simplicity, that is our assumption.)

(Nope, we're still not done yet with this. We are breaking the problem into smaller pieces and building up.)

In this case, does it make sense for one of the constructors to call the other constructor?

Some of you don't like the array that represents the number of days in a month. You could write the code differently--it's a tradeoff of computation speed vs amount of memory used. That's fine.

Testing

Add to your testing from the last program to test the new constructor in your main method. (Is there any other testing you should do?) Save the output in birthday.out.

Static Methods

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

/**
 * Reverses the given string.
 * @param string the String to reverse
 * @return the string backwards; e.g., "stars" --> "srats"
 */
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"
 *
 * You should consider upper and lower case letters as the same.

 * However, you don't need to consider punctuation as special cases,
 * i.e., "A man. A plan. A canal. Panama." will return false.
 *
 * @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 or StringBuilder classes to help you write the methods.

Your main method should test these methods. As usual, save the output from several runs into palindromes.out.

Turning in Your Assignment

Copy your assign3 directory into your turnin directory, using the turnin.sh script.

Grading (55 pts)

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