Skip to content

Lab-1-4 – First I/O, Just the I (Input)

Objective

Practice reading input from a file with exception handling.

Tip

  1. Use the command $ ant build_labs to compile and build the class.
  2. Use the command $ ant run_tests_lab4 to test the class.
  3. Use the script $ ./runLabs1.sh LabFour YourFilename to run the class.

Instructions

  1. Create a new java class named LabFour in the java112.labs1 package.
  2. Add a main method.
  3. Add code in the main method that will:
    1. Check to see if there is one command line argument.
    2. If there is not just one argument then the method will output: “Please enter one argument on the command line” and terminate.
    3. If there is one command line argument then the method will call the run method and pass the argument.
  4. Add a method named run.

    1. The method will have a void return type.
    2. The method will have one String parameter.
  5. Add code in the run method that will:

    1. Open the input file that was passed as a parameter and output the full contents to the terminal window. To accomplish this the method will need to:

      1. Open the input file.
      2. Loop through the lines of the input file.
      3. Output each line.
      4. Close the input file after the loop has run.
    2. Include full exception handling.

  6. The class must pass the lab 4 tests.

  7. Verify the class runs properly.
    1. Pass in a file name that exists and verify the file was properly read and displayed.
    2. Verify your error handling, too! What happens if you do not pass in file name? What if you pass in a file name that does not exist?

Screenshots

To receive credit for this lab save screenshot(s) in the projects/screenshots/week3 directory.

You may combine the below into one screenshot and name it lab4.png.

  1. lab4-inputFile.png: The input file.
  2. lab4-tests.png: The command line output showing all unit tests passing
  3. lab4-happyPath.png: The command line output when running your program's "happy path".
  4. lab4-errorHandling.png: The command line output demonstrating the error handling in your program.

Add, commit, and push and verify all work, including screenshots are visible in GitHub.


Rubric

All of the following must be satisfied to achieve a "Met" status

  • Screenshot should clearly show tests passing.
  • Screenshot should clearly show expected output when running the program (happy path).
  • Screenshot should clearly show any error conditions handled by the program code.
  • Code and screenshots are properly named and saved in the correct directory.
  • All lab steps have been accurately and appropriately implemented.
  • Code adheres to the course coding standards.
  • Commit messages are concise, atomic, and effectively describe the change(s) made.
  • JavaDoc documentation is properly implemented, providing clarity and understanding of the code's functionality and usage.
  • External sources (websites, classmates, AI tools, etc), if utilized, are referenced and documented within the code as comments.

Additional Considerations

  • Avoid using hard-coded values; utilize parameters passed into the program through the command line.
  • Catch and handle all exceptions.
  • Provide friendly and informative messages to users before displaying stack traces for error situations.