You are on page 1of 2

Computer Science 110 Intro to Algorithms and Programming: Java

Programming Project #4 Array and Sorting Programs (30 points)


Due Date: April 3, 2014
Phase 1 Single-dimensional Array (20 points)
Prior to starting this phase, make sure you download numTestData.txt and numbers.txt from
the Moodle class website. numTestData.txt will be the input file you use to get your program
working. numbers.txt contains a larger data set and should be used for input only after your
program works perfectly.
Write a console program (NO GUI) called NumberSorter which does the following:
1. Open and read the first line of the input file. See Reading Data Using Scanner on page
478 of the textbook for information on how to read data from a file using the Scanner and
File classes.
2. Create an array of double values named numberArray with size to match the first line of
the input file. So if the first line of the file contains 500, the array will have 500 slots.
3. Read the remaining lines of the input file, assigning each value to a slot in the array.
4. Use a method named sort to sort the array from low to high. NOTE: YOU MUST
WRITE A METHOD NAMED sort, even if you use the selection sort code shown on
page 270 of the textbook, this code MUST be incorporated into a method named sort.
The sort method MUST take an array of double values as a parameter and MUST have a
return data type of void. This sort method MUST be called from your main() method.
5. AFTER sorting numberArray, compute the average of the values in the array (not
including the count).
6. Use a for loop to count the number of values in numberArray which are larger than the
calculated average.
7. After the number of above average values have been counted, open a file named
dataout.txt (see Writing Data Using PrintWriter on page 476 of the textbook for
information on how to write data to a file using the PrintWriter and File classes). Write
the following information to dataout.txt:
a. The count (number of slots in numberArray).
b. The sorted list of numbers in numberArray (one per line).
c. The calculated average of the numbers in the array.
d. The number of above average values.
Once are certain that your NumberSorter.java program works perfectly, change the name of the
input file to numbers.txt and run the program. This should produce an output file containing the
number 100.0 on the first line followed by the sorted list of numbers from 1.0 to 100.0, the
average value, and then the number of values which are larger than the average value on the last
line.
Phase 2 Multi-dimensional Array (10 points)
Write a method that returns the sum of all the elements in a specified column in a matrix using
the following header:
public static double sumColumn(double[][] m, int columnIndex)
Write a test program called MultiDimensionalMatrix that allows the user to enter values into a
3-by-4 matrix and then uses the sumColumn method you wrote to display the sum of each
column. Here is a sample run:
----jGRASP exec: java MultiDimensionalMatrix

Enter a 3-by-4 matrix row by row:
1.5 2 3 4
5.5 6 7 8
9.5 1 3 1
Sum of the elements at column 0 is 16.5
Sum of the elements at column 1 is 9.0
Sum of the elements at column 2 is 13.0
Sum of the elements at column 3 is 13.0

----jGRASP: operation complete.

You might also like