created 05/08/00

Programming Exercises

Exercise 1 --- C-style Loop

Modify the first example in the chapter so that it uses the C-style input loop.

Click here to go back to the main menu.

Exercise 2 --- Shorter File Copy Program

The file copy example program in the chapter is longer than it needs to be. Make it shorter by using fewer try{} blocks. (This will result in less specific error messages).

Click here to go back to the main menu.

Exercise 3 --- GUI File Copy

Write a file copy program that uses a graphical user interface. The user enters the name of the file to copy and the name of the destination file in TextFields and clicks a button to perform the copy. Write error messages into another TextFields.

For a nicer program, look in your documentation for the FileDialog class. Include it in the GUI so the user can choose the source file graphically.

Click here to go back to the main menu.

Exercise 4 --- Reading Random Integer Data

Write a class that reads data from a file containing integers in character format. This class will be a software tool that other programs can use to simplify their own input. The constructor for the class will use the name of the input file as a parameter, and will create the appropriate streams. Write a close() method and a method int getNextInt() that returns the value of the next integer from the stream. When an error is encountered, these methods will write an error message and stop the program.

The input file may have none, one, or several integers per line. A suitable file could be created by the program for Exercise 1 of the previous chapter. The class will work with any number of integers total, and a varying number of integers per line.

Use BufferedReader, FileReader, parseInt(), and StringTokenizer.

Test your class by using it in a program that writes the integers from the input file on the monitor, one per line. Once the class is debugged there are many other programs you can write that use it:

Click here to go back to the main menu.

Exercise 5 --- HTML Java Source Code Reserved Word Highlighter

Write a program that inputs a Java source code file and outputs a copy of that file with Java keybords surrounded with HTML tags for bold type. For example this input:

public class JavaSource
{
  public static void main ( String[] args ) 
  {
    if ( args.length == 3 )
      new BigObject();
    else
      System.out.println("Too few arguments.");
  }
}

will be transformed into:

<B>public</B> <B>class</B> JavaSource
{
  <B>public</B> <B>static</B> <B>void</B> main ( String[] args ) 
  {
    <B>if</B> ( args.length == 3 )
      <B>new</B> BigObject();
    <B>else</B>
      System.out.println("Too few arguments.");
  }
}

In a browser the code will look like this:

public class JavaSource
{
  public static void main ( String[] args ) 
  {
    if ( args.length == 3 )
      new BigObject();
    else
      System.out.println("Too few arguments.");
  }
}
Click here to go back to the main menu.

Exercise 6 --- HTML Filter

Any text editor, such as Notepad, can be used to create web pages. Unfortunately, these editors usually do not check spelling. Word processors can open a text file and check its spelling. But when a file is sprinkled with HTML tags they all are flagged as errors and the real spelling errors are hard to see. This exercise is to write a utility that strips the HTML tags from a text file.

Write a program that reads in a text file and writes out another text file. The input file may have any number of HTML tags per line. The output file will be a copy of the input file but with spaces substituted for each HTML tag. The program will not check HTML syntax; it looks at the file as a stream of tokens and substitutes spaces for each token that is a tag. For this program, an HTML tag is any token that looks like one of these:

<Word>       </Word>

Assume that Word is a single word (perhaps just one letter or no letters) and that there are no spaces between the left and right angle brackets. With this definition, the following are tags:

<p>       </p>     <em>     </em>
<rats>       </1234>     <blockquote>     </>

With this definition, the following are NOT tags (although some are with real HTML):

< p>       </ p>     <em >     </e m>
<table border cellpadding=5>     <block quote>     < /em>
Challenging Exercise: Write the program to filter out any tag that looks like one of these:

<Word .... >       </Word ... >

Now Word is a single word that immediately follows the left angle bracket, but may be followed by more text which may include spaces. A tag ends with a right angle bracket, which might or might not be preceeded by a space. Assume that a tag starts and ends on the same line. With this definition, the following are tags

<p>       </p>     <em >     </em >
<table border cellpadding=5>     <word another word>     </x y z>

Start by setting a flag to false. Now look at the input stream of tokens one by one. When a token starts a tag set a boolean flag to true. While the flag is true discard tokens until encountering a tag end (either stuff> or >). Set the flag to false.

Click here to go back to the main menu.

End of Exercises.