Programming Assignment 3
Wrapper Classes, Text Processing, and Recursion with Merge Sort
Objectives
To demonstrate an understanding of recursive methods, text processing, and boxed primitives.
Requirements
You will implement a Java console application that sorts and prints floating-point values read from a text file.
The application determines the name of the text file from the command-line arguments. If the user supplies no arguments, or if the user supplies two or more arguments, the application terminates after writing to Standard Error, “Invalid command-line arguments.”.
The application loops over every line of the input file. For every iteration, the application parses the line as a Double
. If a line from the file is empty, the application terminates after writing to Standard Error, “Empty line encountered.”.
If a line from the input file contains anything other than one valid Double
, the application terminates after writing to Standard Error, “Invalid line encountered: 'XXX'”, where 'XXX' is the contents of the first invalid line.
If any other I/O error occurs while reading the input file, the application terminates after writing to Standard Error, “Failed to read input file: XXX”, where XXX is the value returned by Exception.getMessage()
.
The application stores the parsed values from the text file into a collection of type ArrayList<Double>
. Once all values are added to this collection, the application creates an array of type double[]
that is the same length as the size of the collection. The application “unboxes” the values from the collection and stores the corresponding primitive values into the array.
The application sorts the array using the recursive MergeSort algorithm.
1. | if min = max then return |
2. | lhsMax ← ⌊(min + max) / 2⌋ |
3. | rhsMin ← lhsMax + 1 |
4. | MergeSort(V, S, min, lhsMax) |
5. | MergeSort(V, S, rhsMin, max) |
6. | S[min … max] ← V[min … max] |
7. | lhs ← min |
8. | rhs ← rhsMin |
9. | dst ← min |
10. | while lhs ≤ lhsMax and rhs ≤ max do |
11. | if S[lhs] < S[rhs] then do |
12. | V[dst] ← S[lhs] |
13. | lhs ← lhs + 1 |
14. | else |
15. | V[dst] ← S[rhs] |
16. | rhs ← rhs + 1 |
17. | end if |
18. | dst ← dst + 1 |
19. | end while |
20. | while lhs ≤ lhsMax do |
21. | V[dst] ← S[lhs] |
22. | lhs ← lhs + 1 |
23. | dst ← dst + 1 |
24. | end while |
25. | while rhs ≤ max do |
26. | V[dst] ← S[rhs] |
27. | rhs ← rhs + 1 |
28. | dst ← dst + 1 |
29. | end while |
The implementation defines a method with the following signature to start the sort:
static void mergeSort(double[] values)
The mergeSort(double[])
method allocates a scratch array that is the same size as the input array. This method then calls a method with the following signature to perform the recursive portion of the algorithm:
static void mergeSort( double[] values, double[] scratch, int min, int max)
Once the sort finishes, the application writes the contents of the sorted array to Standard Output. Each value in the array is printed consecutively as lines of output. When all values have been printed, the appilcation terminates immediately without displaying any additional information.
Examples
Resources
- Merge Sort Description and Animation
- input-10.txt
- input-100.txt
- input-1000.txt
- input-10000.txt
- input-100000.txt
- input-1000000.txt
- one-dot-two-dot-three.txt
- bad-line.txt
- missing-line.txt
- no-values.txt
- one-value.txt
- ten-zeros.txt
- two-values-one-line.txt
- Relevant API
Student Performance and Statistics
Analysis on overall student performance and coverage of requirements