JMU JMU - Department of Computer Science
Help Tools
Lab: Experimenting with Input/Output


Instructions: Answer the following questions one at a time. After answering each question, check your answer (by clicking on the check-mark icon if it is available) before proceeding to the next question.

Getting Ready: Before going any further, you should:

  1. Depending on your development environment, create either a directory or a project for this lab.
  2. Setup your development environment.
  3. Download the following files:
    to an appropriate directory/folder. (In most browsers/OSs, the easiest way to do this is by right-clicking/control-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?
    Rooter.java:6: cannot find symbol
    symbol  : class PrintWriter
    location: class Rooter
           PrintWriter     screen;
    
    Expand
  4. Fix this error. What line did you need to add?
    import java.io.*;
    
    Expand
  5. Compile Rooter.
  6. What was the first error generated by the compiler this time?
    Rooter.java:10: cannot find symbol
    symbol  : class Scanner
    location: class Rooter
           Scanner         keyboard;
           ^
    
    Expand
  7. Fix this problem. What line did you add?
    import java.util.*;
    
    Expand
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
        

    Note: If you are using an IDE that keeps the source code and the .class files in different directories, you will need to be in the directory that contains the .class files (e.g., the bin directory).

  4. Why was no output generated?
    Text sent to a stream is buffered and, hence, is not instantly sent to the output device.
    Expand
  5. Fix this mistake. What line did you add?
    screen.flush();
    Expand
  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 "streams end with" and find a reputable site.)
    Ctrl+D in Unix and Ctrl+Z in MS-Windows.
    Expand
  7. Why does this application terminate when you type the end-of-stream character?
    Because the call to keyboard.hasNext() returns false and, as a result, the application does not enter the body of the while loop.
    Expand
3. File Input Basics: This part of the lab will help you understand file input.
  1. Open Remember and compile Remember.
  2. Execute Remember from the terminal window using the command:
        java Remember < birthdays.txt
        

    Note: If you are using an IDE that keeps the source code and the .class files in different directories, you will need to save the .txt files to the the directory that contains the .class files (e.g., the bin directory).

  3. What output was generated?
    It depends on the date!
    Expand
  4. Execute Remember from the terminal window using the command:
        java Remember < anniversarys.txt
        
  5. What happened and why?
        The operatings system was unable to find the file 
        anniversarys.txt and, hence, couldn't redirect it
        to System.in.
        
    Expand
  6. Add a declaration of a File variable named dates to main(). What change(s) did you make?
           File          dates;   
        
    Expand
  7. Instantiate the variable named dates passing the constructor command-line argument 0. What change(s) did you make?
           dates = new File(args[0]);   
        
    Expand
  8. Modify main() so that dateScanner uses dates and the application will compile and execute properly. What changes did you need to make?
              try
              {
                 dateScanner = new Scanner(dates);          
                 processDates(today);          
              }
              catch (FileNotFoundException fnfe)
              {
                 screen.printf("Unable to open %s", dates.getAbsolutePath());
                 screen.flush();
              }
        
    Expand
  9. Execute Remember from the terminal window using the command:
        java Remember birthdays.txt
        
  10. What output was generated?
    It depends on the date!
    Expand
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?
           for (int i=0; i<args.length; i++)
           {
              dates = new File(args[i]);
    
              try
              {
                 dateScanner = new Scanner(dates);          
                 processDates(today);          
              }
              catch (FileNotFoundException fnfe)
              {
                 screen.printf("Unable to open %s", dates.getAbsolutePath());
                 screen.flush();
              }
           }
        
    Expand
  2. Execute Remember from the terminal window using the command:
        java Remember birthdays.txt assignments.txt
        
  3. What output was generated?
    It depends on the date!
    Expand
5. IDE Skills: This part of the lab will help you improve your IDE skills (if you are using one).
  1. Read the section of the course "Help" page on your IDE that explains how to incorporate resources in a project.
  2. Add birthdays.txt and assignments.txt to your project.
  3. Read the section of the course "Help" page on your IDE that explains how to pass command-line arguments to an application (sometimes called a "run configuration").
  4. Setup your IDE so that you can run Remember with the command-line arguments birthdays.txt assignments.txt.
  5. Make sure that you can execute Remember using these command-line arguments from within your IDE.
6. More Practice: Here are some small assignments that you can complete 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.
7. 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 2019