Learn (Basic) Java IO

The purpose of this note is to direct the student in incrementally exploring the Java in order to become familiar with the documentation format and using these routines to accomplish goals. Answers are available but should only be consulted to prevent frustration. Consulting the answers too soon will reduce the educational value of the exercise.

1. Create a program to type out "Hello World" to the CRT ten times.

2. Modify program 1 to type out any string the user types in ten times. (Hint: use BufferedReader's readLine() .)

3. Create a program which outputs "I hate _________" where the item of hatred is supplied by the person who runs the program.

4. Write a program which reads a character (using System.in.read() ), writes a character (using System.out.print() ) unless the character is the letter P. You should learn something here about how the system usually feeds a line at a time to the Java program.

5. Write a program with a while loop which works as in 4 except that it keeps reading/writing until it sees the letter P.

6. Rewrite the program of 5 to use a do-while construct.

7. Write a program which reads from a file named TEST.IN (you will have to create this file using the editor) and writes the file's contents to the CRT. Be sure to close the file after you have reached the end of the file. (How is this signaled?)

8. Rewrite the program in problem 7 to write the inputted file to the file TEST.OUT.

9. Rewrite the program in problem 8 to get the input filename from the command line (use args[]).

10. Rewrite the program in problem 8 to stdin/stdout re-direction instead. Be sure to run the program without redirecting anything, redirecting only the output, redirecting only the input, redirecting both the input and the output.

11. Write a function called mymag to give you the magnitude of a vector when you input the x and y components of a vector. (Square root of the sum of the squares, remember?) Write a small test program which sets x and y to values, invokes the function mymag, and using outputs the result to the CRT.

12. Modify the program in problem 11 to prompt for the x and y values.

13. Modify the program of problem 12 (and 11) to ask the user if they want to repeat the job getting a Y (for Yes) or N (for No) response. Watch for details of how your input system works!

14. You should have the idea by now. Experiment! You are expected to be able to utilize the documentation to effectively code working Java programs.


David Green
Last modified: Thu Sep 23 12:52:58 CDT 1999