Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

import java.io.BufferedReader; import java.io.File; import java.io.FileReader; i

ID: 666686 • Letter: I

Question


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class Sort {

/**
   * You are to implement this method. The method should invoke one or
   * more threads to read and sort the data from the collection of Files.
   * The method should return a sorted list of all of the String data
   * contained in the files.
   *
   * @param files
   * @return
   * @throws IOException
   */
public static String[] threadedSort(File[] files) throws IOException {
    throw new java.lang.IllegalStateException("Method not implemented");
}


/**
   * Given an array of files, this method will return a sorted
   * list of the String data contained in each of the files.
   *
   * @param files the files to be read
   * @return the sorted data
   * @throws IOException thrown if any errors occur reading the file
   */
public static String[] sort(File[] files) throws IOException {

    String[] sortedData = new String[0];

    for (File file : files) {
      String[] data = getData(file);
      data = MergeSort.mergeSort(data);
      sortedData = MergeSort.merge(sortedData, data);
    }

    return sortedData;

}


/**
   * This method will read in the string data from the specified
   * file and return the data as an array of String objects.
   *
   * @param file the file containing the String data
   * @return String array containing the String data
   * @throws IOException thrown if any errors occur reading the file
   */
private static String[] getData(File file) throws IOException {

    ArrayList<String> data = new ArrayList<String>();
    BufferedReader in = new BufferedReader(new FileReader(file));

    // Read the data from the file until the end of file is reached
    while (true) {
      String line = in.readLine();
      if (line == null) {
        // the end of file was reached
        break;
      }
      else {
        data.add(line);
      }
    }
   
    //Close the input stream and return the data
    in.close();
    return data.toArray(new String[0]);

}
}

Modify the Program:

Improve the performance of the Java program by adding threads to the Sort.java file. Implement the threadedSort() method within the Sort class. Reuse any of the existing methods by calling them as necessary from your threadedSort method. You may add additional methods to the Sort class, if necessary.

Analyze the Program:

When running the provided SortTest program, the output presents data to support analyzing the performance of the threaded and non-threaded sort methods. Analyze your threaded implementation by comparing its performance to the original non-threaded implementation and explain the measured behavior. Document your analysis as a short paper (1–3 pages), using APA format. Be sure to discuss the relative performance improvement you expect for your threaded implementation and how the expected performance compares to the measured performance

Explanation / Answer

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class Sort {
/**
* You are to implement this method. The method should invoke one or
* more threads to read and sort the data from the collection of Files.
* The method should return a sorted list of all of the String data
* contained in the files.
*
* @param files
* @return
* @throws IOException
*/
public static String[] threadedSort(File[] files) throws IOException {
/*
Running a thread to sort each file given in the Array files;
*/
for (int i = 0; i < files.length; i++){
file[] f = new file[1];
f[0] = files[i];
Thread t = new Thread() {
@Override
public void run() {
return sort(f);
}
}
t.start();
}
throw new java.lang.IllegalStateException("Method not implemented");
}


/**
* Given an array of files, this method will return a sorted
* list of the String data contained in each of the files.
*
* @param files the files to be read
* @return the sorted data
* @throws IOException thrown if any errors occur reading the file
*/
public static String[] sort(File[] files) throws IOException {
String[] sortedData = new String[0];
for (File file : files) {
String[] data = getData(file);
data = MergeSort.mergeSort(data);
sortedData = MergeSort.merge(sortedData, data);
}
return sortedData;
}

/**
* This method will read in the string data from the specified
* file and return the data as an array of String objects.
*
* @param file the file containing the String data
* @return String array containing the String data
* @throws IOException thrown if any errors occur reading the file
*/
private static String[] getData(File file) throws IOException {
ArrayList<String> data = new ArrayList<String>();
BufferedReader in = new BufferedReader(new FileReader(file));
// Read the data from the file until the end of file is reached
while (true) {
String line = in.readLine();
if (line == null) {
// the end of file was reached
break;
}
else {
data.add(line);
}
}
//Close the input stream and return the data
in.close();
return data.toArray(new String[0]);
}
}