created 05/08/00


Programming Exercises


Exercise 1 --- Junk Mail Generator

Write a program that creates a "personalized" letter, given a form letter and a person's name. The form letter will be an input file of text (use file redirection as discussed in chapter 23). The person's name will be a command line argument. The file will normal text, but with a * wherever a person's name should be substituted. For example:

Dear *,

I have exciting news for you, *!!! For just $49.99 plus postage
and handling you, *, can be the proud owner of a genuine leather
mouse pad!  No more finger strain for you, *, as you surf the web
with style.

Act Soon,

Venture Marketing Corp.

Assume the above is in a file junk.txt. A run of the program outputs:

C:\chap49D>java  JunkGenerator "Occupant" < junk.txt
Dear Occupant,

I have exciting news for you, Occupant!!! For just $49.99 plus postage
and handling you, Occupant, can be the proud owner of a genuine leather
mouse pad!  No more finger strain for you, Occupant, as you surf the web
with style.

Act Soon,

Venture Marketing Corp.

C:\chap49D>

Write the program so that it will substitute for any number of * on one line, and accepts any number of lines as input. The main program loop will be a while loop that continues until the input string (read with readLine() is null. Each input line should be used to create a StringTokenizer with appropriate delimiters.

If you wish to avoid the command line argument, ask the user for the occupant name and input it in the usual way.

Click here to go back to the main menu.


Exercise 2 --- Word Reverser

Write a program that reads in a sentence from the user and prints it out with each word reversed, but with the words and punctuation in the original order:

C:\> java reverseSent
Input:  Go to the main menu.
oG ot eht niam unem.

Click here to go back to the main menu.


Exercise 3 --- 24 to 12 Hour Converter

Write a program that reads in a string that is a 12 hour version of the time and writes out a 24 hour version of the time.

C:\chap49D>java  TimeConverter
Input: 12:13AM

C:\chap49D>java  TimeConverter
Input: 12:23
0:23

C:\chap49D>java  TimeConverter
Input:8:30AM
8:30

C:\chap49D>java  TimeConverter
Input:8:30PM
20:30

C:\chap49D>java  TimeConverter
Input:8:30 PM
20:30

Assume that the user has correctly entered digits separated by a colon. If no "AM" or "PM" is given, assume "AM".

This can get complicated. If you want something easier, require "AM" or "PM" to immediately follow the minutes.

Or, if you want complication, try to make your program "bullet proof". Check that hours and minutes are both within range. Allow spaces on either side of the colon, and between minues and the AM or PM. Allow AM and PM to be upper or lower case.

C:\chap49D>java  TimeConverter
Input:8 : 30 pm
20:30

C:\chap49D>java  TimeConverter
Input:8:77
Bad input format.

Click here to go back to the main menu.


End of Exercises.