Assignment #6: Sorting Program
[Modified Exercise 8.27]: Write a simple sorting utility, sort.
The sort command takes a file name as a parameter.
The file contains a sequence of lines; each line contains
fields. By default, the fields are separated by white space
(space or tab, in any amount),
but the -t option allows you to use any one character
as a field separator.
By default fields are to be interpreted as strings, but the -n
option allows you to interpret a field as a (double)
number.
The
-r option reverses the order of the comparisons.
Thus the sort proceeds from largest to smallest.
The
-f option makes the sort case-insensitive.
Finally, by default lines are
to be sorted as if the entire line was a single string
(breaking ties arbitrarily).
However, the -k option can be used to specify that
the sort is performed using a specified field.
Options may appear in any order.
The file name may appear anywhere; if it is not present,
use System.in.
You may not assume that there is a limiton the number of fields.
The sorted output is written to System.out.
Examples
If the command is
java Sort -n -t: -k2 fileName
and the data file fileName is
10:20:30:40
20:100:40:30
30:40:10:20
Then the output is sorted on the basis of the second field, which is interpreted as a number, and is thus
10:20:30:40
30:40:10:20
20:100:40:30
Without the -n option, the output would be
20:100:40:30
10:20:30:40
30:40:10:20
Without any options, each line would be viewed as a giant string.
The resulting output would be
10:20:30:40
20:100:40:30
30:40:10:20
Unusual Circumstances
If the file doesn't open,
a line does not have enough fields, or a field
that is to be a number can't be a number, then
you have to do something reasonable.
I will accept anything reasonable, but you have
to document the behavior.
You also must perform other checks, including verification
that the command-line arguments make sense.
For instance, a character must follow -t and
-k.
Algorithms
You may use any of the sorting algorithms in
java.util.Arrays; most likely
you will want the version that takes an array and a comparator.
Then, you can
define a class that stores the complete line (as a
String) and the key, on which the sort is
based. Since the key could be either a double
or a String, the logical declaration is:
class Line
{
String theLine;
Object theKey;
}
You would then define a Comparator that would
compare any two Line objects, according to the
options passed by the user.
At this point, you need to hook everything together,
and much of the work is parsing the command-line arguments
and then the lines.
Java Notes
- I suggest that some method accept an InputStream
as a parameter; that InputStream will receive either
the InputStream from a successfully opened file or
System.in.
Don't forget your buffering.
- There are many ways to convert a String into a
double.
The easiest is probably Double.parseDouble.
Submission Details
I will provide
several good test cases of various sizes, using various options,
and two additional test cases that are degenerate.
Look for them to eventually appear.
-
Submit all your source code, and the results of running the program
on the test cases.
-
You must also provide a man page that explains what
your program does. In particular, tell me how you
handle degenerate cases.
See
javac docs
for an example of reasonable documentation.
I expect a professional job -- type it!
The man page is important
components of your grade.