Course Description
Syllabus
Reading List
Team Project
|
Exercise 4
Last updated: Oct 6, 8:49 pm
For this exercise, I again encourage you to work in pairs or in a
group. To learn more about Pair Programming, see PairProgramming.com.
1. Working with Strings
Use the Java API
documentation for the String class to write a program that
manipulates Strings as follows:
- Create a String s and assign it the value "The quick brown fox
jumps over the lazy dog."
- Print the length of the string. It should be 44.
- Print the string in all upper case.
- Use the replaceAll() method to print this string with "fox"
changed to "cat".
- Use the indexOf() method to print the position of the
letter "x" in this string. The Java docs refer to this as the "index",
but don't confuse this with an array index. (The "x" should be
at position 18.)
- Use the substring() method to extract and print the portion of the
string starting at position 31 and ending at position 43. This should
display "the lazy dog".
- Declare an array of Strings named "wordarray". Initialize this
array to the list of words in the string s by calling the split()
method, passing it a single space " " as the regular expression.
A regular expression is a way of specifying patterns. (We are using it
very simply here, to specify a single space. Use a for() loop to
print each word on a separate line.) Tip: You can determine the
length of the array (i.e., the number of elements in it) by using the
length attibute: wordArray.length. This will be useful in your for
statement.
2. Working with dates and times
Use the code on page 305-306 of HFJ and the Java API
documentation for the GregorianDate class
to write a program that does the following:
- Display the current date and time in the format: MM/DD/YYYY
HH:MM:SS AM/PM. Tip: Make sure you print the month correctly -
Java counts months starting from 0.
- Display the year, month, and day on separate lines.
- Compute and display the date one month from now.
- Compute and display the date 30 days from now.
- As a challenge, use an array of month names and an array of days
of the week to display the date in the form: Friday, October 6.
Remember to import any needed packages at the beginning of your class file.
For this exercise, you can put this code in the main() method.
3. Start building the Music Machine
As you are reading chapter 11, build the Music Machine. As
you do this, use the Java API
documentation to explore the classes used in this application.
This will apply material we covered on using the
documention, and on using packages and imports.
Bill Kules
|