Objective: Create basic Java classes and write overloaded and static methods.
Due: Before the next class (Wednesday).
Create a directory for assign3 within
your cs209 directory.
Copy Birthday.java from
your assign2 directory to
your assign3.
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.
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.
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.
Copy your assign3 directory into your
turnin directory, using the turnin.sh script.
You will be evaluated based on the correctness, testing, and style of your programs: