Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
Lab 4 : Uniq program 
Introduction – READ THE INSTRUCTIONS FIRST! 
The Uniq program intends to emulate the main functionality of the GNU uniq program (remove duplicate 
adjacent lines from a file). 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 Uniq 
program works properly to get checked off at the end of the lab. These are all relatively minor 1 or 2 line 
programming fixes; do not redesign the program. 
 
Program Specifications 
The Uniq program should read lines of input from a single file or stdin by default and write unique adjacent 
lines to a file or stdout by default. 
 
If the program is run with the command line argument "--help", it should print a usage message and exit with the 
same exit status as the real uniq command. Run the Linux command uniq -–help and then check the exit 
status immediately with echo $? to see what value the real uniq command uses as its exit status. 
 
If the program is run with the  "-i" or "--ignore-case" command line option, it should ignore differences in case 
when comparing adjacent lines. 
 
If no input file is specified on the command line, it should read from stdin. 
If no output file is specified on the command line, it should write to stdout. 
 
If the program is run with the command line argument "-" as the input file name, it should read from stdin. 
If the program is run with the command line argument "-" as the output file name, it should write to stdout. 
 
The special command line argument "--" forces an end of command option scanning. This is to allow input or 
output file names that start with a '-' to be recognized as file names and not command line options. This is 
standard GNU getopt() and this behavior is supported by most Linux commands. 
 
If there is an error opening either the specified input or output file, output an error message (see examples 
below) to stderr with the name of the program (Uniq) concatenated with the exception message (see the 
Javadocs for class Throwable/Exception), and then exit with exit status 1. Do not tailor each exception error 
message to what comes after the "Uniq :" string. Let the exception object do all the work. Note: Catch only the 
specific exceptions required, not just the generic Exception. Also there should be no throws clauses in your 
program. This is different from the previous lab requirements. 
 
Note: In all examples, the dollar sign is your prompt and user input is in bold. They assume that there is one plain 
text file in the current directory named in1. The file in1 is available for you to copy along with the original buggy 
Uniq.java. To display the contents of the file, you can run: cat in1  
 
Make a new Lab4-Uniq directory in your cs15u home directory and copy all the files in ~/../public/Lab4-Uniq/ 
to your Lab4-Uniq you just created in your home directory. 
 
Your supervisor had to run off to happy hour and left you to finish this program. He swore it would compile and 
run without any problems (as you notice his pointy hair). And he could not remember if he implemented the "--" 
end of command line options scanning feature or not. 
 
Have fun! Seriously, this should be a fun program to debug now that you have some experience from previous 
labs. 
You first need to get Uniq.java to compile. Probably the first thing you will want to do is point your web 
browser to the Java APIs docs. As a hint for the first compiler error, what package is class BufferedReader 
defined to be in (and thus what package needs to be imported for the compiler to find this class)? 
 
Remember - focus on and fix the first compile error (and only that error) then recompile. 
 
Remember - after the usage message is printed, the exit status should be the same as the exit status of the real 
Linux uniq command. 
 
Remember - catch all exceptions with the most-specific exception type appropriate, and no throws clauses in the 
method headers for this program. 
 
$ cat in1 
Hello 
hello 
HeLLo 
Same Line 
Same Line 
Two Words 
two words 
           <<<< 3 blank lines 
 
 
One last duplicate line. 
One last duplicate line. 
$ 
 
Example – Passing "--help" as a command line argument: 
$ java Uniq --help 
Usage: java Uniq [OPTION]... [INPUT [OUTPUT]] 
Discard all but one of successive identical lines from INPUT 
(or standard input), writing to OUTPUT (or standard output). 
 
  -i, --ignore-case  ignore differences in case when comparing 
      --help         display this help and exit 
 
With no INPUT, or when INPUT is -, read standard input. 
With no OUTPUT, or when OUTPUT is -, write to standard output. 
 
The special argument -- forces an end of option scanning. 
 
Behavior should be like that of the Unix command named uniq. 
$ echo $?        <<<< This tests the exit status 
0 
$ 
 
Example – Bad input file name: 
$ java Uniq xxx > /dev/null     <<<< /dev/null is the bit bucket 
Uniq: xxx (No such file or directory) 
$ 
 
Example – Bad output file name (or some other file name where you do not have permission): 
$ java Uniq in1 /dev/mem > /dev/null 
Uniq: /dev/mem (Permission denied) 
$ echo $?        <<<< This tests the exit status 
1 
$ 
Example – Reading the contents of a file and output to stdout (default): 
$ java Uniq in1 
Hello 
hello 
HeLLo 
Same Line 
Two Words 
two words 
 
One last duplicate line. 
$ 
 
 
Example – Reading the contents of a file ignoring case and output to stdout (default): 
$ java Uniq -i in1  
Hello 
Same Line 
Two Words 
 
One last duplicate line. 
$ 
 
 
$ java Uniq --ignore-case in1 
Hello 
Same Line 
Two Words 
 
One last duplicate line. 
$ 
 
 
Example – Using "-" as input file to read from stdin and specify an output file: 
$ java Uniq – out 
CSE 15L 
cse 15L 
cse 15L 
CsE 15l 
+d entered by user 
$ cat out 
CSE 15L 
cse 15L 
CsE 15l 
$ 
 
 
$ java Uniq -i - out 
CSE 15L 
cse 15L 
cse 15L 
CsE 15l 
+d entered by user 
$ cat out 
CSE 15L 
$ 
 
$ java Uniq - /dev/mem 
Uniq: /dev/mem (Permission denied) 
$ 
 
Example – Using "-" as input file to read from stdin and "-" as output file to write to stdout: 
$ java Uniq – - 
CSE 15L 
CSE 15L 
CSE 15L 
CSE 15L 
cse 15L 
cse 15L 
+d entered by user 
$ 
 
 
Test using "--" to end command line options scanning (You will probably need to add a new boolean variable.) 
 
$ cat in1 > -xxx 
$ 
 
$ java Uniq -- -xxx 
Hello 
hello 
HeLLo 
Same Line 
Two Words 
two words 
 
One last duplicate line. 
$ 
 
 
$ java Uniq -i -- -xxx 
Hello 
Same Line 
Two Words 
 
One last duplicate line. 
$ 
 
 
$ java Uniq -- -i -yyy 
Uniq: -i (No such file or directory) 
$ 
 
 
$ java Uniq --ignore-case -- -xxx -yyy 
$ cat -- -yyy 
Hello 
Same Line 
Two Words 
 
One last duplicate line. 
$ 
 
$ java Uniq -x in1 
unrecognized option '-x' 
Try 'java Uniq --help' for more information. 
 
 
 
 
 
 
 
 
 
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 15L 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 Uniq. 
Answer this question: 
How do you test / what command do you type to get the following error message? 
 
extra operand 'zzz' 
Try 'java Uniq --help' for more information.