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 Nagahide
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 Nagahide
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.

Click here to go back to the main menu.


Exercise 2 --- Word Reverser

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

C:\> java reverseSent "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 as a 12 hour version of the time and writes out a 24 hour version of the time. Use a command line argument or user input.

c:\chap49D>java  TimeConverter "12:13 am"
12:13

c:\chap49D>javac TimeConverter.java

c:\chap49D>java  TimeConverter "12:23"
0:23

c:\chap49D>java  TimeConverter "8:30AM"
8:30

c:\chap49D>java  TimeConverter "8:30PM"
20:30

c:\chap49D>java  TimeConverter "8:30 pm"
20:30

c:\chap49D>java  TimeConverter "8:30"
8:30

c:\chap49D>java  TimeConverter "8:77"
Bad input format.

Assume that the user has correctly entered digits separated by a colon. Check that the hours and minutes are in range. If no "AM" or "PM" is given, assume "AM". Allow lower case "am" and "pm", and allow spaces after the minutes.

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

Click here to go back to the main menu.


End of Exercises.