You are on page 1of 2

CSC207H: Exercise 2

Due Date: Wednesday 11:59pm Oct 7th

What to do for this exercise


1. Write a Java program "Ex2.java" that obeys the specifications below. It must be
in a package "e2".
2. Check the program into your Exercise 2 repository. We will look for a file called
"Ex2.java" in a directory "e2", and that directory may be either the "exercises/e2"
itself or some descendant of it in the tree of directories. You will need to make
any necessary directories and add/commit them to the repo.

Specifications for Ex2.java


When the user gives the command

java e2.Ex2 command file1 file2 ...

then the program must examine command to see whether it is "commonstrings"


or "privatemembers".

If command is "commonstrings", then the program has exactly two more


command-line arguments, both of which are file names. The program must
print all the lines that appear in both of those files, in sorted order. You must
use two java.util.Sets (one for each file) to maintain the lines in the file. A
line should appear at most once in the output no matter how many times it
occurs in either file.

If "command" is "privatemembers", then the program has at least one more


command-line argument, and possibly many. All of them are names of Java
files. The program must print all lines that start with "private" (removing
leading whitespace), in sorted order.

For both parts, if any sort of exception occurs because of bad input such as a
nonexistent file, you should catch it in method main and print "Exception
occurred.". Your program should then terminate.
Hints

• In main, the command will be args[0] and the file names will be args[1],
args[2], and so on.
• Only use System.out.println for output; don't use System.out.print.
• Use a java.util.Scanner to read a file.
• Use a java.util.Set of some kind to store the lines for the "commonstrings"
part, extract them into an array using an appropriate method in java.util.Set,
and sort that array using java.util.Arrays.sort. The sorting order ("collating
sequence") is just the one used for ordinary string comparison, so there's not a
lot of computer science here.
• Use a java.util.List of some kind to store the lines for the "privatemembers"
part, and then Collections.sort to sort the list. The sorting order ("collating
sequence") is again just the one used for ordinary string comparison, so there's
not a lot of computer science here. Make sure you trim whitespace here.
• The previous couple of hints are invitations to read the Java API documentation
(http://java.sun.com/javase/6/docs/api/overview-summary.html).
• Don't ask the user for the file names. They're program arguments.
• Don't print anything but the sorted lines. If you print a header, your mark will be 0.
• You can assume the user actually provides the necessary file names on the
command line, and you don't need to worry that we will try your program on files
that don't contain valid characters.
• You can assume that every line in the input files, including the last, ends with a
newline character.
• You can't assume the number of lines in the file is greater than 0.
• To handle all possible exceptions, wrap the contents of your main method in a
try/catch block and catch Exceptions.