created 09/05/99; revised 06/12/00

Chapter 29 Programming Exercises

Exercise 1 --- Title Applier

Write a program that asks for the user's name and then writes that name to the monitor with either "Ms." or "Mr." in front, depending if the name is for a female or male. Assume that the only female names are

and that the only male names are

All other names will be echoed without a title. The program continutes looping until the user hits "enter" without first typing a name.

C:\>java Title
Enter a name:
Amy Johnson
Ms. Amy Johnson

Enter a name:
Fred Smith
Mr. Fred Smith

Enter a name:
Zoltan Jones
Zoltan Jones

Enter a name:

C:\>
 

Exercise 2 --- Documentation Printer

Write a program that reads in lines and outputs only those lines that start with "//" . When a Java source file is used as input the program will echo lines that describe the program.

C:\>java Stripper < Hello.java

// Hello World Program
// written by Elmer

// Here is the main method:

C:\>

Look at the chapter "File Input and Output" if it is not clear what the command line is doing.

 

Exercise 3 --- Better Documentation Printer

Write a program that reads in lines and outputs only some lines:

  1. Lines that start with "//" are output.
  2. Lines that start with "/*" are output.
  3. Lines that start with "*/" are output.
  4. All lines between those that start with "/*" and "*/" are output.

To do this use a boolean variable that is set to true when "/*" is encountered and is set to false when "*/" is encountered. Output lines when the variable is true.

Note: This program has a major problem because legal comments start with "/*" anywhere in a line and end with "*/" anywhere in a line. A source program might have a comment that begins with "/*" at the start of a line and ends with "*/" at the end of a line. Our program (as described) will miss that ending and continue writing lines. A better program (much harder to write) would find comments in the source file no matter where they begin and end. If you decide to do this, you will need String methods not discussed in this chapter.

 

Exercise 4 --- Name Echo

Write a program that asks for user's name and then writes it back with the first name as entered, and the second name all in captial letters.


C:\>java NameEcho
Enter your name:
Sherlock Holmes

Sherlock HOLMES

C:\>

You will have to look at the documentation of the String class and use methods not discussed in these notes:

Because this method throws an exception, your main() method must start with

public static void main (String[] args) throws StringIndexOutOfBoundsException
 

Exercise 5 --- charAt()

Write a program where the user enters a string, and the program echos it to the monitor with one character per line:


C:\>java LinePerChar
Enter a string:
Octopus

O
c
t
o
p
u
s

To do this you will need to use the following method from class String:

public char charAt( int inx ) throws  StringIndexOutOfBoundsException
This method returns the character that is at index inx of the String. Characters are indexed beginning at index 0.