Lab: Experimenting with Input/Output

Instructions:

Answer as many of the following questions as you can during the lab period. If you are unable to complete the assignment during the lab period you must complete it on your own.

You may work on this assignment alone or in a group (of no more than 3 students).

If you are using Eclipse as an IDE, you should not make an Eclipse project for this lab. Instead, open the Java files directly from the File menu. You will then compile and execute your Java files from a terminal. As a reminder the terminal command for compiling Java files is javac:

$ javac SomeClass.java

Compiled Java programs can be executed using the java command:

$ java SomeClass

Getting Ready: Before going any further, you should:

  1. Make a directory for this lab.
  2. Download the following files:
    to your working directory. (In most browsers, the easiest way to do this is by right-clicking on each of the links above.)

1. Using the Java APIs: This part of the lab will help you remember how to use the Java APIs.
  1. Open Rooter.java in the editor.
  2. Compile Rooter.
  3. What was the first error generated by the compiler?
  4. Fix this error. What line did you need to add?
  5. Compile Rooter.
  6. What was the first error generated by the compiler this time?
  7. Fix this problem. What line did you add?
2. Keyboard Input and Screen Output: This part of the lab will help you understand the basics of keyboard input and screen output.
  1. Open a terminal/shell window and make your working directory the directory you created for this lab.
  2. Compile Rooter.
  3. Execute Rooter from the terminal window using the command:
        java Rooter
        
  4. Why was no output generated?
  5. Fix this mistake. What line did you add?
  6. What do you need to type in the terminal window (as a response to the prompt) in order to make this application terminate normally? (Hint: Search on the WWW for the phrase "send stream end in a terminal" and find a reputable site.)
  7. Why does this application terminate when you type the end-of-stream character?
3. File Input Basics: This part of the lab will help you understand file input.
  1. Open Remember in the editor.
  2. Compile Remember.
  3. Execute Remember from the terminal window using the command:
        java Remember < birthdays.txt
        
  4. What output was generated?
  5. Execute Remember from the terminal window using the command:
        java Remember < anniversarys.txt
        
  6. What happened and why?
  7. Add a declaration of a File variable named dates to main(). What change(s) did you make?
  8. Instantiate the variable named dates passing the constructor command-line argument 0. What change(s) did you make?
  9. Modify main() so that dateScanner uses dates and the application will compile and execute properly. What changes did you need to make?
  10. Execute Remember from the terminal window using the command:
        java Remember birthdays.txt
        
  11. What output was generated?
4. More on File Input: This part of the lab will give you more experience working with file input.
  1. Modify main() so that it will process multiple files when their names are passed-in as command-line arguments. What changes did you need to make?
  2. Execute Remember from the terminal window using the command:
        java Remember birthdays.txt assignments.txt
        
  3. What output was generated?
5. Programming Practice After the Lab: Here are some small assignments that you can complete (on your own time) to get more practice.
  1. Modify Remember so that it checks the dates when it reads them and writes invalid dates to a file with the same name as the input file but with a file type of ".err".
  2. Further modify Remember so that it checks for the existence of the error log and prompts the user before overwriting it.
  3. Write an application named Untab that is passed a file name as a command-line argument, renames that file so that it has an extension of ".tmp" reads the ".tmp" file, replaces all of the tab characters with four spaces, and writes the result to a file with the original name.
  4. Change the format string and Locale in the call to screen.printf() that prints the variable today in various ways. Try to achieve the same results using a Formatter object and the println() method. Now, try to achieve the same results using a DateFormat object and the println() method.
  5. Create a version of Remember that uses a StringTokenizer rather than a Scanner to tokenize/parse the dates.
6. Questions to Think About: This part of the lab will help you get ready for the next part of the course.
  1. Should the Scanner and PrintWriter in Remember be static attributes? Why or why not?
  2. Should Remember have a main() method, or should there be a distinct driver? Why or why not?
  3. Should the methods in Remember be static? Why or why not?

Copyright 2013