created 08/13/99


Chapter 21 Programming Exercises

Exercise 1 — Control Characters

Write a program that writes ten x's to the screen (modify the HelloWorld.java program). Use file redirection to send the program's output to a text file. Use the DIR command (or in Unix, the ls -l command) to see how many bytes are in the file. Depending on how your operating system implements end of lines, you will see more than 10 bytes.

C:\>java Hello > output.txt

C:\>dir output.txt

08/13/99  07:01p                    12 output.txt
               1 File(s)             12 bytes
                            392,968,704 bytes free

The additional bytes are the control characters that indicate the end of the line.

Click here to go back to the main menu.


Exercise 2 — More Control Characters

Modify the program so that it outputs 10 lines of 10 x's. Run it, redirect the output to a file, and examine the file. (It would be a very good idea to be sure that you don't have an infinite loop before you redirect the output to a file.)

C:\>java Hello > output.txt

C:\>dir output.txt

08/13/99  07:05p                    120 output.txt
               1 File(s)             120 bytes
                             392,968,704 bytes free

The previous file has been replaced by a new file (with the same name).

Click here to go back to the main menu.


Exercise 3 — Appending to an Existing File

Now run the original program 10 times in a row. With the first run, create a text file (replacing any other of the same name). With each additional run, add the output to the first file. Check if the length is the same as in Exercise 2:

C:\>java Hello > output.txt
C:\>java Hello >> output.txt
  . . . . . . . . . . .
C:\>java Hello >> output.txt

C:\>dir output.txt

08/13/99  07:15p                    120 output.txt
               1 File(s)             120 bytes
                             392,968,704 bytes free

The file has the same characters as the previous program that used a loop.

Click here to go back to the main menu.


Exercise 4 — Really Small File

Now modify the program so that it outputs no characters. Redirect the output to a file. Is a file created (even though it contains no data?) What size is it?

Click here to go back to the main menu.


Exercise 5 — Text Editor

Write a program that asks the user to enter lines of text. After each line the program echos the line to the terminal. The program finishes when the user enters a line that contains the characters //done (use a sentinel-controlled loop and the equals() method of class String.) Once the program is working, run it, and redirect the output to a file.

If everything is working, try using your program to create a small Java source file by running the small editor and typing in the lines of the new program. You can correct mistakes as long as you are on the same line, but once you hit "enter" that line is committed. In the following example, the user creates the file Hello.java using the small editor, and then compiles and runs it.

D:\> java TextEdit > Hello.java
class Hello
{
  public static void main( String[] a )
  {
    System.out.println("Hello new File!");
  }
}
//done

D:\> javac Hello.java

D:\> java Hello

Hello new FIle!

Click here to go back to the main menu.