Skip to content

Lab-1-8 – First Set

Objective

Practice using a Set with Java I/O and exception handling. You are required to use try-with-resources in this lab.

Tip

  1. Use the command $ ant build_labs to compile and build the class.
  2. Use the script $ ant run_tests_lab8 to test the class.
  3. Use the script $ ./runLabs1.sh LabEight OutputFileName to run the class.

Instructions

  1. Create a class named LabEight and place it in the java112.labs1 package.
  2. Add one instance variable that is a Sorted Set of Strings.
  3. Add a main method.
  4. Add code in the main method that will:

    1. Check to see if there are one command line argument. The argument will be the name of the output file the class will make.
    2. If there isn’t just one argument, then the method will output: “Please enter one argument on the command line, an output file name” and terminate.
    3. If there is one argument, then the method will instantiate an instance of the LabEight class and call its run method. The argument will be passed to the run method.
  5. Add a method named run.

    1. The method will have a void return type.
    2. The method will have one String parameter. The parameter is the output file path for the file that will be created.
  6. Add code in the run method that will:
    1. Instantiate an instance of the TreeSet class and assign it to the instance variable.
    2. Add ten elements to the Set. The strings: “one”, “one”, “five”, “two”, “four”, “two”, “three”, “three”, “four”, “three”.
    3. Call another method named writeSetToOutputFile passing the output file path as the argument to the method.
  7. Add a method named writeSetToOutputFile.
    1. The method will have one parameter that is a String for the output file path name.
  8. Add code in the writeSetToOutputFile method that will:
    1. Open a PrintWriter object to the output file path.
    2. Write each element of the Set instance variable to the output file with each element on its own line.
    3. Close the output file.
    4. Include full exception handling for I/O code.
  9. The class must pass the lab 8 tests.
  10. Verify the class runs correctly.
    1. Pass in a file name and verify the file was written properly.
    2. Did you test your error handling?!

Screenshots

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

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

  1. lab8-tests.png: The command line output showing all unit tests passing
  2. lab8-happyPath.png: The command line output when running your program's "happy path".
  3. lab8-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

  • Instant variables are private and have JavaDoc.
  • Catch and handle all exceptions.
  • Provide friendly and informative messages to users before displaying stack traces for error situations.
  • Correct implementation of try-with-resources to manage resources.