<<

Lab (CSE 15L, October 5, 2011)

The Java program named Uniq should emulate the main functionality of the GNU () uniq program: it should print the contents of a with consecutive identical lines collapsed to just one . Of course, the version of the program given to you has defects. It is your job to it meet the specification above and work correctly on the cases below. You must submit your joint lab notes before 7pm on Wednesday, and submit a joint lab report by Saturday noon.

Program Specification: The program reads input from standard input or a file, then prints its contents with adjacent identical lines replaced by just one copy. Printing is to standard output (the screen) or a file. The program reads from standard input if no input files are specified, or if the specified file is named “-”. The program prints to standard output if no output file is specified, or if the specified file is named “-”. When the program is called with the argument “--”, it should print usage instructions. A user can call the program with the “-i” or the “--ignore-case” argument to tell the program to ignore case differences when comparing lines. The default behavior is to take case differences into account.

Note: In all examples, blue is user input, red is output, and the dollar sign is the Linux prompt. There are two plain text files in the current directory, named input1.txt and input2.txt whose contents are shown below.

Contents of input1.txt hello hello hello

Contents of input2.txt human human human dog CAT Cat CAt cat dog amazing AmAzInG Amazing

Example 1 - Printing the contents of input1.txt with adjacent identical lines collapsed to one: $java Uniq input1.txt hello

Example 2 - Reading data with no command line argument; the data shown is from the terminal. $java Uniq hello hello hello

Example 3 - Reading data with “-” as command line argument; this scenario is similar to a scenario with no command line argument. $java Uniq - hello hello hello

Example 4 - 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 With no INPUT, or when INPUT is -, read standard input. With no OUTPUT, or when OUTPUT is -, to standard output. Behavior should be like that of the command named uniq.

Example 5 - Printing the contents of input2.txt with adjacent identical lines collapsed to one ignoring case: $java Uniq -i input2.txt human dog CAT dog amazing

Explanation: Since CAT, Cat, CAt and cat are treated similarly when the program compares lines while ignoring cases, only the first line (CAT) is printed out. This is also true for amazing, AmAzInG and Amazing.

Example 6 - Printing input2.txt with adjacent identical lines collapsed to one ignoring case to a file: $java Uniq -i input2.txt output.txt $cat output.txt human dog CAT dog amazing

Hints 1. There is considerable similarity between this lab and last week’s. Learn from your experience. 2. In Java, the && operator has higher precedence (binds tightly) than the || operator. 3. Think carefully about the logic of exactly when the program should print out the current line. 4. If a logical condition is complicated, one good approach is to break it down into nested if commands.

Report submission ● Email your joint lab report to the same email address [email protected] by noon on Saturday. ● The subject of the email should be Report 1 where username1 and username2 are your UCSD usernames in alphabetical order. Cc yourselves using only your UCSD email addresses. ● Include your full names at the , put your report as the body of the email, and attach nothing. Before the deadlines, you may resubmit notes and/or the report. If you do, use the “Reply” email command, so that the previous version is attached to the later version. . Things to remember for submissions ● See the notes guidelines at http://cseweb.ucsd.edu/~elkan/15L/NotesGrading.pdf ● See the report guidelines at http://cseweb.ucsd.edu/~elkan/15L/ReportGrading.pdf ● In the notes, do not skip steps. Include everything you try, whether it succeeds or fails.