Lab-1-5 – Now the O (Output)
Objective
Practice writing output to a file with exception handling.
Tip
- Use the command
$ ant build_labs
to compile and build the class. - Use the command
$ ant run_tests_lab5
to test the class. - Use the script
$ ./runLabs1.sh LabFive OutputFileName SomeText
to run the class.
Instructions
-
Create a new java class named LabFive in the java112.labs1 package.
-
Add a
main
method. - Add code in the
main
that will:- Check to see if there are two command line arguments.
- The first argument will be the name of the file that the class will create.
- The second argument will be a message. This message has to be one word with no spaces.
- If there aren’t two arguments then the method will output: “Please enter two arguments on the command line, a file name and a message” and terminate.
- If there are two arguments, then the method will instantiate an instance of the LabFive class and call its
run
method. The two arguments will be passed to the run method.
- Check to see if there are two command line arguments.
- Add a
run
method.- The method will have a void return type.
- The method will have two String parameters. The first parameter will be the name of output file. The second parameter will be a message.
- Add code in the
run
method that will:- Open a file for writing using its first parameter as the file path.
- Write the message parameter to the output file.
- Close the output file.
- Include full exception handling.
- The class must pass the lab 5 tests.
- Verify you can run the class.
- Pass in a file name and a message. Then, manually verify that the file was properly written with the message.
- Verify your error handling, too! What happens if you do not pass in the proper number of command line arguments?
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 lab5.png
.
lab5-tests.png
: The command line output showing all unit tests passinglab5-happyPath.png
: The command line output when running your program's "happy path".lab5-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.