Lab-1-7 – First List
Objective
Practice using an ArrayList with Java I/O and exception handling. You are required to use try-with-resources in this lab.
Tip
- Use the command
$ ant build_labs
to compile and build the class. - Use the script
$ ant run_tests_lab7
to test the class. - Use the script
$ ./runLabs1.sh LabSeven OutputFileName
to run the class.
Instructions
- Create a class named LabSeven and place it in the java112.labs1 package.
- Add one instance variable that is a List of Strings.
- Add a main method.
- Add code in the main method that will:
- Check to see if there is one command line argument. The argument will be the name of the output file the class will make.
- 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.
- If there is one argument, then the method will instantiate an instance of the LabSeven class and call its
run
method. The argument will be passed to the run method.
- Add a method named
run
.- The method will have a void return type.
- The method will have one String parameter. The parameter is the output file path for the file that will be created.
- Add code in the
run
method that will:- Instantiate an instance of the ArrayList class and assign it to the instance variable.
- Add ten elements to the List. The strings: “one”, “two”, “three”, “four”, “five”, “six”, “seven”, “eight”, “nine”, “ten”.
- Call another method named writeListToOutputFile passing the output file path as the argument to the method.
- Add a method named
writeListToOutputFile
.- The method will have a void return type.
- The method will have one parameter that is a String for the output file path name.
- Add code in the
writeListToOutputFile
method that will:- Open a PrintWriter object to the output file path.
- Write each element of the List instance variable to the output file with each element on its own line.
- Close the output file.
- Include full exception handling for I/O code.
- The class must pass the lab 7 tests.
- Verify the class runs correctly.
- Pass in a file name and verify the file was written properly.
- 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 lab7.png
.
lab7-tests.png
: The command line output showing all unit tests passinglab7-happyPath.png
: The command line output when running your program's "happy path".lab7-outputFile.png
: The output file that was created.lab7-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.