Lab : Sort program Introduction Your pointy haired Dilbert manager decided to write a Sort program which you have now inherited. The Sort program intends to emulate the main functionality of the GNU sort program (sort lines from stdin or a into alphabetic/sorted order or reverse alphabetic/sorted order and display the sorted lines to stdout or a ). However, the version of the program given to you has some problems. It is your job to fix it so that it meets all the specifications below. In addition, you must keep an interactive Lab Notes log to show us along with demonstrating your Sort program works properly to get checked off at the end of the lab. In particular, there are 9 different/unique problems in the program. error occurs multiple times (same error in multiple locations) but only counts as of the 9 unique problems. These are all relatively minor or line programming fixes; do not redesign the program. Make sure the program compiles with no warnings! In particular take note of any "Note:" messages compiler. Make a new Lab-Sort directory in your cs5u account and copy all the from ~/../public/lab-sort/ to your new Lab-Sort directory. Program Specifications The Sort program should read lines of input from a single or stdin by default and write the lines sorted in alphabetic order to a or stdout by default. If the program is run with the command line argument "--help", it should print a usage message and exit. If no input is specified on the command line, it should read from stdin. If no output is specified on the command line, it should write to stdout. If the program is run with the command line argument "-" as the input name, it should read from stdin. If the program is run with the command line argument "-" as the output name, it should write to stdout. If the program is run with the "-r" command line argument, it should output the lines sorted in reverse order. If there is an error opening either the specified input or output, output an error message (see examples below) to stderr with the name of the program ("Sort: ") concatenated with the detailed message string of this exception (see the Javadocs for class Throwable/Exception), and then exit with exit status. (Exit status of 0 means success; an exit status of any non-zero value means failure.) Note: Catch the most specific exception type in this case, not a generic Exception. This is the only exception handling you need to do. Note: In all examples, the dollar sign is your prompt and user input is in bold. Assume there is one plain text in the current directory in. The in is available for you to you (you should have copied it along with the original broken Sort.java). To display the contents of the, you can run: cat in You first need to get your boss' program to compile! Remember - do not redesign the program no matter how badly you may want to. First understand what the program is trying to do by reading the source code. Sometimes compiler errors can be a bit misleading. You need to understand what the compiler thinks your code is trying to do and what you meant your code to do. Some of these compiler errors and warnings may be new to you. Most have to do with the use of the ArrayList type and generics. After trying to figure these out together, if you continue to have problems compiling the program without errors or warnings, ask for help from one of the CSE 5L staff.
Get the help message to print. Example Passing "--help" as a command line argument: $ java Sort --help Usage: java Sort [-r] [input [output]] Read input from standard input or a until the end of input is reached then print the input to standard output or a in sorted order. If the -r flag is present print the input in reverse sorted order. Using '-' for the input will read from standard input and for the output will print to standard output. Get the exception handling of bad input/output s working. Check that the error message is going to stderr and the program is exiting with exit status. Handle these with a single try-catch and a single method call to print the detail message string (hint: look at the javadocs for Throwable). How do you determine what single exception type to catch in the above examples? Remember - you are to catch the most specific type of exception that covers these errors. And how do you get the different/specific detailed message string from this exception object to output/print in a general way? If you cannot figure out how to determine this after trying, ask for help from one of the CSE 5L staff. Example Bad input name (catch exception; display that exception's error message; check exit status): $ java Sort xxx Sort: xxx (No such or directory) Example Bad output name (or some other name where you do not have permission): $ java Sort in /dev/mem Sort: /dev/mem (Permission denied) $ java Sort in /tmp Sort: /tmp (Is a directory) How do you test to make sure the error message is going to stderr? If you need help/hint, check how you tested this on the command line in Lab. We will ask you to demonstrate this. echo $? prints the exit status. The exit status of the previous cmd is stored in the special shell variable $? Now get this to work. Probably a few fixes need Example Sorting the contents of a to stdout: $ java Sort in in.
Example Sorting the contents of a to an output specified on the command line: $ java Sort in out $ cat out in. Example No command line arguments. Default input from stdin and default output going to stdout: $ java Sort $ <<<<<--- Note: Testing empty/no input $ touch in <<<<<--- Note: This create an empty $ java Sort in $ $ java Sort Hey! Things are looking. Now try the following. Example Using "-" as input to read from stdin and default output to stdout: $ java Sort - Oops. The program thinks the "-" is a command line option (or at least the beginning on a command line option) instead of the input. Where is the program trying to differentiate these?
Example Using "-" as input to read from stdin and "-" as output to write to stdout: $ java Sort - - Example Using "-" as input to read from stdin and specify an output : $ java Sort out $ cat out Test all of the above with the "-r" option to reverse the sort. Only showing a couple examples here. Example Sorting the contents of a in reverse order to stdout: $ java Sort r in in. $ java Sort -r - - Be sure to test the "-r" option with all variations/combinations of options from above.
One more Example Redirecting stdin and stdout: $ java Sort -r - - < in > out $ cat out in. < in redirects stdin in > out redirects stdout to the out Be sure to keep a running log (Lab Notes) of each observed bug, hypothesize what is wrong, how you fixed it, and how you tested the fix. Be sure to include erroneous hypotheses and non-fixes. Learning from mistakes is as important (if not more important). Get one of the CSE 5L staff to check you off for the lab. You will need to go through your log describing each bug interaction and demo your fixed version of Sort.