Please improve the java code to be able to compile successfully. Below please find the 3 class codes.
Provide the improved Sort, MergeSort, and SortTest java codes that successfully compile and run.
Here is the Sort.java code:
import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.IOException;import java.util.ArrayList;
public class Sort extends Thread{ /** 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 file * @ return * @tgrows IOException */ public static String[] threadedSort(File[] files) throws IOException{ //commenting out to ue the multithread /*throw new java.lang.IllegalStateException(“Method not implemented”); */ String[] sortedData = new String[0]; Thread t = new Thread(new Runnable(){ public void run(){ try{ for(File file : files){ String[] data = getData(file); data = MergeSort.mergeSort(data); sortedData = MergeSort.merge(sortedData, data); } } catch(IllegalStateException e){ System.out.println(“Message: ” + e.getMessage()); } } }); t.start(); return sortedData; } /** * 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 the 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 file until the end of file is reached while(true){ String line = in.readLine(); if(line == null){ //the end of the file was reached break; }else{ data.add(line); } } //close the input stream and return the data in.close(); return data.toArray(new String[0]); } }
Here is the MergeSort.java:
public class MergeSort { // The mergeSort method returns a sorted copy of the // String objects contained in the String array data. /** * Sorts the String objects using the merge sort algorithm. * * @param data the String objects to be sorted * @return the String objects sorted in ascending order */ public static String[] mergeSort(String[] data) {
if (data.length > 1) { String[] left = new String[data.length / 2]; String[] right = new String[data.length – left.length]; System.arraycopy(data, 0, left, 0, left.length); System.arraycopy(data, left.length, right, 0, right.length); left = mergeSort(left); right = mergeSort(right); return merge(left, right); } else { return data; } } /** * The merge method accepts two String arrays that are assumed * to be sorted in ascending order. The method will return a * sorted array of String objects containing all String objects * from the two input collections. * * @param left a sorted collection of String objects * @param right a sorted collection of String objects * @return a sorted collection of String objects */ public static String[] merge(String[] left, String[] right) { String[] data = new String[left.length + right.length]; int lIndex = 0; int rIndex = 0; for (int i=0; i<data.length; i++) { if (lIndex == left.length) { data[i] = right[rIndex]; rIndex++; } else if (rIndex == right.length) { data[i] = left[lIndex]; lIndex++; } else if (left[lIndex].compareTo(right[rIndex]) < 0) { data[i] = left[lIndex]; lIndex++; } else { data[i] = right[rIndex]; rIndex++; } } return data; } }
Here is the SortTest.java code:
import java.io.File;import java.io.IOException;
/** * The class SortTest is used to test the threaded and non-threaded * sort methods. This program will call each method to sort the data * contained in the four test files. This program will then test the * results to ensure that the results are sorted in ascending order. * * Simply run this program to verify that you have correctly implemented * the threaded sort method. The program will not verify if your sort * uses threads, but will verify if your implementation correctly * sorted the data contained in the four files. * * There should be no reason to make modifications to this class. */public class SortTest { public static void main(String[] args) throws IOException { File[] files = {new File(“enable1.txt”), new File(“enable2k.txt”), new File(“lower.txt”), new File(“mixed.txt”)};
// Run Sort.sort on the files long startTime = System.nanoTime(); String[] sortedData = Sort.sort(files); long stopTime = System.nanoTime(); double elapsedTime = (stopTime – startTime) / 1000000000.0; // Test to ensure the data is sorted for (int i=0; i<sortedData.length-1; i++) { if (sortedData[i].compareTo(sortedData[i+1]) > 0) { System.out.println(“The data returned by Sort.sort is not sorted.”); throw new java.lang.IllegalStateException(“The data returned by Sort.sort is not sorted”); } } System.out.println(“The data returned by Sort.sort is sorted.”); System.out.println(“Sort.sort took ” + elapsedTime + ” seconds to read and sort the data.”); // Run Sort.threadedSort on the files and test to ensure the data is sorted startTime = System.nanoTime(); // Commenting out /*String[] threadSortedData = Sort.threadedSort(files); stopTime = System.nanoTime(); double threadedElapsedTime = (stopTime – startTime)/ 1000000000.0; // Test to ensure the data is sorted if (sortedData.length != threadSortedData.length) { System.out.println(“The data return by Sort.threadedSort is missing data”); throw new java.lang.IllegalStateException(“The data returned by Sort.threadedSort is not sorted”); } for (int i=0; i<threadSortedData.length-1; i++) { if (threadSortedData[i].compareTo(threadSortedData[i+1]) > 0) { System.out.println(“The data return by Sort.threadedSort is not sorted”); throw new java.lang.IllegalStateException(“The data returned by Sort.threadedSort is not sorted”); } } System.out.println(“The data returned by Sort.threadedSort is sorted.”); System.out.println(“Sort.threadedSort took ” + threadedElapsedTime + ” seconds to read and sort the data.”); } } */
Why Work with Us
Top Quality and Well-Researched Papers
We always make sure that writers follow all your instructions precisely. You can choose your academic level: high school, college/university or professional, and we will assign a writer who has a respective degree.
Professional and Experienced Academic Writers
We have a team of professional writers with experience in academic and business writing. Many are native speakers and able to perform any task for which you need help.
Free Unlimited Revisions
If you think we missed something, send your order for a free revision. You have 10 days to submit the order for review after you have received the final document. You can do this yourself after logging into your personal account or by contacting our support.
Prompt Delivery and 100% Money-Back-Guarantee
All papers are always delivered on time. In case we need more time to master your paper, we may contact you regarding the deadline extension. In case you cannot provide us with more time, a 100% refund is guaranteed.
Original & Confidential
We use several writing tools checks to ensure that all documents you receive are free from plagiarism. Our editors carefully review all quotations in the text. We also promise maximum confidentiality in all of our services.
24/7 Customer Support
Our support agents are available 24 hours a day 7 days a week and committed to providing you with the best customer experience. Get in touch whenever you need any assistance.
Try it now!
How it works?
Follow these simple steps to get your paper done
Place your order
Fill in the order form and provide all details of your assignment.
Proceed with the payment
Choose the payment system that suits you most.
Receive the final file
Once your paper is ready, we will email it to you.
Our Services
No need to work on your paper at night. Sleep tight, we will cover your back. We offer all kinds of writing services.
Essays
No matter what kind of academic paper you need and how urgent you need it, you are welcome to choose your academic level and the type of your paper at an affordable price. We take care of all your paper needs and give a 24/7 customer care support system.
Admissions
Admission Essays & Business Writing Help
An admission essay is an essay or other written statement by a candidate, often a potential student enrolling in a college, university, or graduate school. You can be rest assurred that through our service we will write the best admission essay for you.
Reviews
Editing Support
Our academic writers and editors make the necessary changes to your paper so that it is polished. We also format your document by correctly quoting the sources and creating reference lists in the formats APA, Harvard, MLA, Chicago / Turabian.
Reviews
Revision Support
If you think your paper could be improved, you can request a review. In this case, your paper will be checked by the writer or assigned to an editor. You can use this option as many times as you see fit. This is free because we want you to be completely satisfied with the service offered.