Starting from:
$30

$24

Lab 2 Solution

For today’s lab you will be doing two exercises pertaining to Input/Output and Exceptions. Incorporate all the concepts we covered in class.




Part 1




Write a program, Lab2InputOutput.java, that reads a file containing text.




The name of the input file will be provided by a user of your program as a (the first) command line argument to your program (see lecture 2_1 or the textbook for a reminder on command line arguments).




Prompt the user (system.in) for the name they want to use for the output file using the console.




For this part you do not need to handle the exception. Rather, you can just "throws" it to the main level and have the stack trace printed.







Read each line and send it to the output file, preceded by line numbers.




Example




If the input file is




Mary had a little lamb




Whose fleece was white as snow.




And everywhere that Mary went,




The lamb was sure to go!




then the program produces the output file




/* 1 */ Mary had a little lamb




/* 2 */ Whose fleece was white as snow. /* 3 */ And everywhere that Mary went, /* 4 */ The lamb was sure to go!

CSE 271 – Due 11:59pm @ Feb 7 (Thursday Labs); @ Feb 8 (Friday Labs) Lab 2













Part 2




Write a program, Lab2FloatingPoint.java, that asks the user to input a set of floating-point (float) values ONE AT A TIME.




When the user enters a value that is not a number, give the user a second chance to enter the value USING EXCEPTIONS by catching the appropriate exception.




After two (failed) chances in a row, quit reading input and continue the program.




Finally, add all correctly specified values and print the sum when the user is done entering data. You must use exception handling to detect improper inputs.




HINT: Follow the DataAnalyzer example from class & Textbook Section 7.5 in regards to having an outer loop with exceptions




Sample run of second part of Lab 2, demonstrating that it is two exceptions in a row.




2




4




a




4




2




a




b













(final total will be 12)

CSE 271 – Due 11:59pm @ Feb 7 (Thursday Labs); @ Feb 8 (Friday Labs) Lab 2













Submission




Turn in a .zip of your two .java files to Canvas by your appropriate due date. Ensure you have actually submitted (received confirmation and checked online), since no late labs will be accepted.




Rubric




Part 1 – 15 Points




11 points – Program is implemented correctly (free from errors) and adheres to specification




4 points – Comments, Efficiency, File Naming




Part 2 – 15 Points




11 points – Program is implemented correctly (free from errors) and adheres to specification. Use of exceptions to check for number format is essential.




4 points – Comments, Efficiency, File Naming




Grading will be based on conforming to the standards we reviewed in class as well as following the requirements of this lab.

More products