Best writers. Best papers. Let professionals take care of your academic papers

Order a similar paper and get 15% discount on your first order with us
Use the following coupon "FIRST15"
ORDER NOW

I need to write a constructor for the second class I created in the file Sort.

I need to write a constructor for the second class I created in the file Sort.java. Below is what I have so far,

I brought over from another file Merge.java the code that was needed, then wrote code to initialize the needed variables. Can you help me straighten out my code?

import static MergeSort.merge;
import static MergeSort.mergeSort;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
package ms;
public class Sample {
class SampleThread implements Runnable{
public SampleThread(int x, int returnX);
int x;
int returnX;
SampleThread(int x){
this.x = x;
}
int getReturnX() {
return returnX;
}
@Override
public void run();
System.out.println(x);
returnX = 10;
}
}
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);

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;
}
//Initialize threads
Sample s = new Sample();
SampleThread st = s.new SampleThread();

//Create two threads
SampleThread st1 = s.new SampleThread();
Thread t = new Thread(st);
Thread t1 = new Thread(st1);

//To run threads
t.start();
t1.start();
//To stop threads
try {
t.join();
t1.join();
}
catch (InterruptedException e) {
e.printStackTrace();

}
System.out.println(st.getReturnX());
}

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]);

}
}

 
Looking for a Similar Assignment? Order now and Get 10% Discount! Use Coupon Code "Newclient"