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.)
Document your analysis as a short paper (1 page).
Main File (sort.java):
001import java.io.BufferedReader;
002import java.io.File;
003import java.io.FileReader;
004import java.io.IOException;
005import java.util.ArrayList;
006import java.util.logging.Level;
007import java.util.logging.Logger;
008
009public class Sort {
010
011 /**
012 * You are to implement this method. The method should invoke one or
013 * more threads to read and sort the data from the collection of Files.
014 * The method should return a sorted list of all of the String data
015 * contained in the files.
016 *
017 * @param files
018 * @return
019 * @throws IOException
020 */
021 public static String[] threadedSort(File[] files) throws IOException
022 {
023 String[] sortedData = new String[0];
024 SortThread[] sortThreads = new SortThread[files.length];
025 for (int i=0; i
026 sortThreads[i] = new SortThread(files[i]);
027 sortThreads[i].start();
028 }
029
030 for (SortThread thread : sortThreads) {
031 try {
032 thread.join();
033 }
034 catch (InterruptedException ex){
035 Logger.getLogger(Sort.class.getName()).log(Level.SEVERE, null, ex);
036 }
037 return sortedData;
038 }
039 throw new java.lang.IllegalStateException(“Method not implemented”);
040 }
041
042 private static class SortThread extends Thread {
043 private File file;
044 private String[] data;
045
046
047 public SortThread(File file) {
048 this.file = file;
049 }
050
051 @Override
052 public void run() {
053 try {
054 data = Sort.getData(file);
055 for (int i = 0; i
056 //Thread.sleep(100);
057 //I commented this out because it was wasting time
058 //System.out.println(data[i]);
059 //I commented this out because I didn’t want to display the entire files
060 }
061 }
062 catch (IOException ex) {
063 Logger.getLogger (Sort.class.getName()).log(Level.SEVERE, null, ex);
064 }
065 }
066 }
067
068 /**
069 * Given an array of files, this method will return a sorted
070 * list of the String data contained in each of the files.
071 *
072 * @param files the files to be read
073 * @return the sorted data
074 * @throws IOException thrown if any errors occur reading the file
075 */
076 public static String[] sort(File[] files) throws IOException {
077
078 String[] sortedData = new String[0];
079
080 for (File file : files) {
081 String[] data = getData(file);
082 data = MergeSort.mergeSort(data);
083 sortedData = MergeSort.merge(sortedData, data);
084 }
085
086 return sortedData;
087
088 }
089 /**
090 * This method will read in the string data from the specified
091 * file and return the data as an array of String objects.
092 *
093 * @param file the file containing the String data
094 * @return String array containing the String data
095 * @throws IOException thrown if any errors occur reading the file
096 */
097 private static String[] getData(File file) throws IOException {
098
099 ArrayList data = new ArrayList();
100 BufferedReader in = new BufferedReader(new FileReader(file));
101 // Read the data from the file until the end of file is reached
102 while (true) {
103 String line = in.readLine();
104 if (line == null) {
105 // the end of file was reached
106 break;
107 }
108 else {
109 data.add(line);
110 }
111 }
112 //Close the input stream and return the data
113 in.close();
114 return data.toArray(new String[0]);
115
116 }
117}
Mergesort.java:
01public class MergeSort {
02
03 // The mergeSort method returns a sorted copy of the
04 // String objects contained in the String array data.
05 /**
06 * Sorts the String objects using the merge sort algorithm.
07 *
08 * @param data the String objects to be sorted
09 * @return the String objects sorted in ascending order
10 */
11 public static String[] mergeSort(String[] data) {
12
13 if (data.length > 1) {
14 String[] left = new String[data.length / 2];
15 String[] right = new String[data.length – left.length];
16 System.arraycopy(data, 0, left, 0, left.length);
17 System.arraycopy(data, left.length, right, 0, right.length);
18
19 left = mergeSort(left);
20 right = mergeSort(right);
21
22 return merge(left, right);
23
24 }
25 else {
26 return data;
27 }
28
29 }
30
31 /**
32 * The merge method accepts two String arrays that are assumed
33 * to be sorted in ascending order. The method will return a
34 * sorted array of String objects containing all String objects
35 * from the two input collections.
36 *
37 * @param left a sorted collection of String objects
38 * @param right a sorted collection of String objects
39 * @return a sorted collection of String objects
40 */
41 public static String[] merge(String[] left, String[] right) {
42
43 String[] data = new String[left.length + right.length];
44
45 int lIndex = 0;
46 int rIndex = 0;
47
48 for (int i=0; i
49 if (lIndex == left.length) {
50 data[i] = right[rIndex];
51 rIndex++;
52 }
53 else if (rIndex == right.length) {
54 data[i] = left[lIndex];
55 lIndex++;
56 }
57 else if (left[lIndex].compareTo(right[rIndex]) < 0) {
58 data[i] = left[lIndex];
59 lIndex++;
60 }
61 else {
62 data[i] = right[rIndex];
63 rIndex++;
64 }
65 }
66
67 return data;
68
69 }
70
71}
SortTest.java:
01import java.io.File;
02import java.io.IOException;
03
04/**
05 * The class SortTest is used to test the threaded and non-threaded
06 * sort methods. This program will call each method to sort the data
07 * contained in the four test files. This program will then test the
08 * results to ensure that the results are sorted in ascending order.
09 *
10 * Simply run this program to verify that you have correctly implemented
11 * the threaded sort method. The program will not verify if your sort
12 * uses threads, but will verify if your implementation correctly
13 * sorted the data contained in the four files.
14 *
15 * There should be no reason to make modifications to this class.
16 */
17public class SortTest {
18
19 public static void main(String[] args) throws IOException {
20
21 File[] files = {new File(“enable1.txt”), new File(“enable2k.txt”), newFile(“lower.txt”), new File(“mixed.txt”)};
22
23 // Run Sort.sort on the files
24 long startTime = System.nanoTime();
25 String[] sortedData = Sort.sort(files);
26 long stopTime = System.nanoTime();
27 double elapsedTime = (stopTime – startTime) / 1000000000.0;
28
29 // Test to ensure the data is sorted
30 for (int i=0; i
31 if (sortedData[i].compareTo(sortedData[i+1]) > 0) {
32 System.out.println(“The data returned by Sort.sort is not sorted.”);
33 throw new java.lang.IllegalStateException(“The data returned by Sort.sort is not sorted”);
34 }
35 }
36 System.out.println(“The data returned by Sort.sort is sorted.”);
37 System.out.println(“Sort.sort took ” + elapsedTime + ” seconds to read and sort the data.”);
38
39 // Run Sort.threadedSort on the files and test to ensure the data is sorted
40 startTime = System.nanoTime();
41 String[] threadSortedData = Sort.threadedSort(files);
42 stopTime = System.nanoTime();
43 double threadedElapsedTime = (stopTime – startTime)/ 1000000000.0;
44
45 // Test to ensure the data is sorted
46 if (sortedData.length != threadSortedData.length) {
47 System.out.println(“The data return by Sort.threadedSort is missing data”);
48 throw new java.lang.IllegalStateException(“The data returned by Sort.threadedSort is not sorted”);
49 }
50 for (int i=0; i
51 if (threadSortedData[i].compareTo(threadSortedData[i+1]) > 0) {
52 System.out.println(“The data return by Sort.threadedSort is not sorted”);
53 throw new java.lang.IllegalStateException(“The data returned by Sort.threadedSort is not sorted”);
54 }
55 }
56 System.out.println(“The data returned by Sort.threadedSort is sorted.”);
57 System.out.println(“Sort.threadedSort took ” + threadedElapsedTime + ” seconds to read and sort the data.”);
58
59
60 }
61
62}
The error am getting:
run:The data returned by Sort.sort is sorted.Sort.sort took 2.072120353 seconds to read and sort the data.The data return by Sort.threadedSort is missing dataException in thread “main” java.lang.IllegalStateException: The data returned by Sort.threadedSort is not sortedat SortTest.main(SortTest.java:49)Java Result: 1BUILD SUCCESSFUL (total time: 3 seconds)
"Looking for a Similar Assignment? Get Expert Help at an Amazing Discount!"
Important To Note The Sentence In Item 13 Of The Instruction Data That States As
/in Uncategorized /by developerNeed help putting this information into the correct tax forms. Thank you
5. Important to note the sentence in item #13 of the instruction data that states "assume that the self—employed health insurance deduction is $384". This means enter as adeduction on Form 1040. line 29 $384, assume this is an acceptable deduction for CA with no adjustment. (no further explanation provided) 6. Be sure to view the State Tax Video to help support the Schedule A Income tax deduction calculation. FORM 1040 LINE 44. TAX. Included in the tax calculation for the Cortez family are two taxes. a. Use the Qualified Dividends and Capital Gain Tax Worksheet to calculate regulartaxI]. Use the Form 8314 to calculate the Kiddie Tax c. Add the regular tax to the Kiddie tax and enter the sum on line 44. FORM 1040 LINE 61. SHARED RESPONSIBILITY PAYMENTWORKSHEET. Note: the textbook version or the worksheet is what we are using, this was available BEFORE the official version so is dinerent than in the instmctions to Fonn 3965 HealthCoverage Exemptions. Here are hints to support your completion of the worksheet: a. Household Income: AGI plus any tax-exempt interest line so plus any Gary’s interest income I]. Line #8 of Shared Responsibility Payment Woncsheet: Enter the amount listed below for your filing status: Single—$10,350 Head of household—$13,350 Married filing jointly— 4i1i’i’201? $20,?od Married filing separately—$4,050 Qualifying widower) with dependent child—$15,650 FORM 1040 LINE 67. SCHEDULE 8812.Be sure to consider that the Cortez family mayr qualify for additional child tax credit from the completion of the schedule 8812. FORM 1040 LINE 69. FORM 3962. Yes, the Cortez family will receive some tax credit, AND they have to pay some individual responsibility {Form 1040 line 61). Modified AGI Form 8962 Box 2A will be the federal AGI that you have calculated plus any tax—exempt interest income. Dependent’s modified AGI will be for Gregory. $2,100 (3,100 minus 1,000 already included in AGI for Cortez). Enter $2,100 on line 2b Form 3962. CALIFORNIA — FORM 3803We did not cover a sample or this in our California study. This is for Gary. Line 1a: $3,100, then continue with the remainder of the form. CALIFORNIA -ASSUME DO NOT QUALIFY FOR RENTERS’ CREDIT
"Looking for a Similar Assignment? Get Expert Help at an Amazing Discount!"
Important Trends That Have Changed Since The Original Rm Algorithms Were Develop
/in Uncategorized /by developerMcGuire, Kelly A. (2016). Hotel pricing in a social world: Driving value in the digital economy. Hoboken, NJ: John Wiley & Sons, Inc
Read chapter 2: Demystifying Price Optimization
"Looking for a Similar Assignment? Get Expert Help at an Amazing Discount!"
Important Use The Internet To Study Two Franchisees In Two Different Industries
/in Uncategorized /by developerIMPORTANT** Use the Internet to study two franchisees in two different industries (or sectors). Then, carefully evaluate and compare their “vertical restraints” for the purposes of assessing the consequences of these provisions for strategic decision making. “Vertical Restraints”Please respond to the following:From the your online research, evaluate and compare the “vertical restraints” of the TWO INDUSTRIES/ SECTORS for the purposes of assessing the consequences of these provisions for strategic decision making.
"Looking for a Similar Assignment? Get Expert Help at an Amazing Discount!"
Importer That Incurs Costs In Gbp And Bills Its Customers In Usd Is Concerned Ab
/in Uncategorized /by developerA U.S. importer that incurs costs in GBP and bills its customers in USD is concerned about the depreciation of USD against GBP due to GBP payables of £10,000,000 in a month. To hedge (protect) the position, the importer decides to use futures markets. Currently GBP contracts (62,500 GBP each) are traded at 1.5290. Spot rate is 1.5310 (i.e., GBP/USD 1.5310). Suppose the importer takes an equal futures position to its cash market position (GBP 10m) at 1.5290. Assume that the futures contract price and spot rates are 1.5995 and 1.6020, respectively, when the hedge is liquidated. What should the unit cost of GBP be for the importer in terms of USD?
"Looking for a Similar Assignment? Get Expert Help at an Amazing Discount!"
Impressionism Vs Out Of Doors Urban Please Respond To The Following Discussion T
/in Uncategorized /by developerImpressionism Vs. Out-of-Doors Urban
Please respond to the following discussion topic and submit it to the discussion forum. Your initial post should be 75-150 words in length. Then, make at least two thoughtful responses to your fellow students’ posts.
Describe the relationship between Impressionism and the out-of-doors urban scene in Paris.
next
Artwork Review
Answer all three of the following questions per work of art shown below. You should reference your book to aid you in answering these questions. Answers should be in essay format, be a minimum of three-five sentences each, and include at least three terms from the glossary for each work..
Gradin
"Looking for a Similar Assignment? Get Expert Help at an Amazing Discount!"
Imprints 12 As You Read This Story Consider How The Images Support And Complemen
/in Uncategorized /by developerRead the short story and answer the following questions
IMPRINTS 12As you read this story .consider how the images supportand complement the textThingsThat Flyby Douglas CouplandI’m sitting hunched over the livingroom coffee table on a Sunday80night , in a daze , having just wokenup from a deep deep sleep on acouch shared with pizza boxes andcrushed plastic cherry yogurt con -cainers . In front of me a TV gameshow is playing on MUTE and myhead rests on top of my hands , as though I am praying , but Iam not ; I am rubbing my eyes and trying to wake up , and myhair is brushing the cabletop which is covered in crumbs and !am thinking to myself that , in spite of everything that has hap -pened in my life , I have never lost the sensation of alwaysbeing on the brink of some magic revelation – that if only Iwould look closely enough at the world , then that magic reve-lation would be mine – if only I could wake up just that little*bit more , then … well – let me describe what happened today .Today went like this : I was up atnoon ; instant coffee ; watched a talksshow ; a game show ; a bit of football ;a religious something – or – other ; thenI turned the TV off . I drifted list-lessly about the house , from silentroom to silent room , spinning the- awheels of the two mountain bikeson their racks in the hallway andstraightening a pile of CDS glued together with spilled OrangeCrush in the living room . I suppose I was trying to pretend !had real things to do , but , well , I didn’t .143
"Looking for a Similar Assignment? Get Expert Help at an Amazing Discount!"
Improve The Following Business Message Our E Tailing Site Www Bestbabygear Com S
/in Uncategorized /by developerImprove the following business message: Our e-tailing site, www.BestBabyGear.com, specializes in only the very best products for parents of newborns, infants, and toddlers. We constantly scour the world looking for products that are good enough and well-built enough and classy enough good enough that is to take their place alongside the hundreds of other carefully selected products that adorn the pages of our award-winning website, www.bestbabygear.com. We aim for the fences every time we select a product to join this portfolio; we don t want to waste our time with onesey-twosey products that might sell a half dozen units per annum no, we want every product to be a top-drawer success, selling at least one hundred units per specific model per year in order to justify our expense and hassle factor in adding it to the abovementioned portfolio. After careful consideration, we thusly concluded that your Inglesina lines meet our needs and would therefore like to add it.
"Looking for a Similar Assignment? Get Expert Help at an Amazing Discount!"
Improve The Paper The Following Two Attachments One Is The Requirements Of The P
/in Uncategorized /by developerHomework cancelled
"Looking for a Similar Assignment? Get Expert Help at an Amazing Discount!"
Improve The Performance Of The Java Program By Adding Threads To The Sort Java F 1
/in Uncategorized /by developerImprove 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.)
Document your analysis as a short paper (1 page).
Main File (sort.java):
001import java.io.BufferedReader;
002import java.io.File;
003import java.io.FileReader;
004import java.io.IOException;
005import java.util.ArrayList;
006import java.util.logging.Level;
007import java.util.logging.Logger;
008
009public class Sort {
010
011 /**
012 * You are to implement this method. The method should invoke one or
013 * more threads to read and sort the data from the collection of Files.
014 * The method should return a sorted list of all of the String data
015 * contained in the files.
016 *
017 * @param files
018 * @return
019 * @throws IOException
020 */
021 public static String[] threadedSort(File[] files) throws IOException
022 {
023 String[] sortedData = new String[0];
024 SortThread[] sortThreads = new SortThread[files.length];
025 for (int i=0; i
026 sortThreads[i] = new SortThread(files[i]);
027 sortThreads[i].start();
028 }
029
030 for (SortThread thread : sortThreads) {
031 try {
032 thread.join();
033 }
034 catch (InterruptedException ex){
035 Logger.getLogger(Sort.class.getName()).log(Level.SEVERE, null, ex);
036 }
037 return sortedData;
038 }
039 throw new java.lang.IllegalStateException(“Method not implemented”);
040 }
041
042 private static class SortThread extends Thread {
043 private File file;
044 private String[] data;
045
046
047 public SortThread(File file) {
048 this.file = file;
049 }
050
051 @Override
052 public void run() {
053 try {
054 data = Sort.getData(file);
055 for (int i = 0; i
056 //Thread.sleep(100);
057 //I commented this out because it was wasting time
058 //System.out.println(data[i]);
059 //I commented this out because I didn’t want to display the entire files
060 }
061 }
062 catch (IOException ex) {
063 Logger.getLogger (Sort.class.getName()).log(Level.SEVERE, null, ex);
064 }
065 }
066 }
067
068 /**
069 * Given an array of files, this method will return a sorted
070 * list of the String data contained in each of the files.
071 *
072 * @param files the files to be read
073 * @return the sorted data
074 * @throws IOException thrown if any errors occur reading the file
075 */
076 public static String[] sort(File[] files) throws IOException {
077
078 String[] sortedData = new String[0];
079
080 for (File file : files) {
081 String[] data = getData(file);
082 data = MergeSort.mergeSort(data);
083 sortedData = MergeSort.merge(sortedData, data);
084 }
085
086 return sortedData;
087
088 }
089 /**
090 * This method will read in the string data from the specified
091 * file and return the data as an array of String objects.
092 *
093 * @param file the file containing the String data
094 * @return String array containing the String data
095 * @throws IOException thrown if any errors occur reading the file
096 */
097 private static String[] getData(File file) throws IOException {
098
099 ArrayList data = new ArrayList();
100 BufferedReader in = new BufferedReader(new FileReader(file));
101 // Read the data from the file until the end of file is reached
102 while (true) {
103 String line = in.readLine();
104 if (line == null) {
105 // the end of file was reached
106 break;
107 }
108 else {
109 data.add(line);
110 }
111 }
112 //Close the input stream and return the data
113 in.close();
114 return data.toArray(new String[0]);
115
116 }
117}
Mergesort.java:
01public class MergeSort {
02
03 // The mergeSort method returns a sorted copy of the
04 // String objects contained in the String array data.
05 /**
06 * Sorts the String objects using the merge sort algorithm.
07 *
08 * @param data the String objects to be sorted
09 * @return the String objects sorted in ascending order
10 */
11 public static String[] mergeSort(String[] data) {
12
13 if (data.length > 1) {
14 String[] left = new String[data.length / 2];
15 String[] right = new String[data.length – left.length];
16 System.arraycopy(data, 0, left, 0, left.length);
17 System.arraycopy(data, left.length, right, 0, right.length);
18
19 left = mergeSort(left);
20 right = mergeSort(right);
21
22 return merge(left, right);
23
24 }
25 else {
26 return data;
27 }
28
29 }
30
31 /**
32 * The merge method accepts two String arrays that are assumed
33 * to be sorted in ascending order. The method will return a
34 * sorted array of String objects containing all String objects
35 * from the two input collections.
36 *
37 * @param left a sorted collection of String objects
38 * @param right a sorted collection of String objects
39 * @return a sorted collection of String objects
40 */
41 public static String[] merge(String[] left, String[] right) {
42
43 String[] data = new String[left.length + right.length];
44
45 int lIndex = 0;
46 int rIndex = 0;
47
48 for (int i=0; i
49 if (lIndex == left.length) {
50 data[i] = right[rIndex];
51 rIndex++;
52 }
53 else if (rIndex == right.length) {
54 data[i] = left[lIndex];
55 lIndex++;
56 }
57 else if (left[lIndex].compareTo(right[rIndex]) < 0) {
58 data[i] = left[lIndex];
59 lIndex++;
60 }
61 else {
62 data[i] = right[rIndex];
63 rIndex++;
64 }
65 }
66
67 return data;
68
69 }
70
71}
SortTest.java:
01import java.io.File;
02import java.io.IOException;
03
04/**
05 * The class SortTest is used to test the threaded and non-threaded
06 * sort methods. This program will call each method to sort the data
07 * contained in the four test files. This program will then test the
08 * results to ensure that the results are sorted in ascending order.
09 *
10 * Simply run this program to verify that you have correctly implemented
11 * the threaded sort method. The program will not verify if your sort
12 * uses threads, but will verify if your implementation correctly
13 * sorted the data contained in the four files.
14 *
15 * There should be no reason to make modifications to this class.
16 */
17public class SortTest {
18
19 public static void main(String[] args) throws IOException {
20
21 File[] files = {new File(“enable1.txt”), new File(“enable2k.txt”), newFile(“lower.txt”), new File(“mixed.txt”)};
22
23 // Run Sort.sort on the files
24 long startTime = System.nanoTime();
25 String[] sortedData = Sort.sort(files);
26 long stopTime = System.nanoTime();
27 double elapsedTime = (stopTime – startTime) / 1000000000.0;
28
29 // Test to ensure the data is sorted
30 for (int i=0; i
31 if (sortedData[i].compareTo(sortedData[i+1]) > 0) {
32 System.out.println(“The data returned by Sort.sort is not sorted.”);
33 throw new java.lang.IllegalStateException(“The data returned by Sort.sort is not sorted”);
34 }
35 }
36 System.out.println(“The data returned by Sort.sort is sorted.”);
37 System.out.println(“Sort.sort took ” + elapsedTime + ” seconds to read and sort the data.”);
38
39 // Run Sort.threadedSort on the files and test to ensure the data is sorted
40 startTime = System.nanoTime();
41 String[] threadSortedData = Sort.threadedSort(files);
42 stopTime = System.nanoTime();
43 double threadedElapsedTime = (stopTime – startTime)/ 1000000000.0;
44
45 // Test to ensure the data is sorted
46 if (sortedData.length != threadSortedData.length) {
47 System.out.println(“The data return by Sort.threadedSort is missing data”);
48 throw new java.lang.IllegalStateException(“The data returned by Sort.threadedSort is not sorted”);
49 }
50 for (int i=0; i
51 if (threadSortedData[i].compareTo(threadSortedData[i+1]) > 0) {
52 System.out.println(“The data return by Sort.threadedSort is not sorted”);
53 throw new java.lang.IllegalStateException(“The data returned by Sort.threadedSort is not sorted”);
54 }
55 }
56 System.out.println(“The data returned by Sort.threadedSort is sorted.”);
57 System.out.println(“Sort.threadedSort took ” + threadedElapsedTime + ” seconds to read and sort the data.”);
58
59
60 }
61
62}
The error am getting:
run:The data returned by Sort.sort is sorted.Sort.sort took 2.072120353 seconds to read and sort the data.The data return by Sort.threadedSort is missing dataException in thread “main” java.lang.IllegalStateException: The data returned by Sort.threadedSort is not sortedat SortTest.main(SortTest.java:49)Java Result: 1BUILD SUCCESSFUL (total time: 3 seconds)
"Looking for a Similar Assignment? Get Expert Help at an Amazing Discount!"
Improve The Performance Of The Java Program By Adding Threads To The Sort Java F 2
/in Uncategorized /by developerImprove 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.)
Document your analysis as a short paper (1 page).
Main File (sort.java):
001import java.io.BufferedReader;
002import java.io.File;
003import java.io.FileReader;
004import java.io.IOException;
005import java.util.ArrayList;
006import java.util.logging.Level;
007import java.util.logging.Logger;
008
009public class Sort {
010
011 /**
012 * You are to implement this method. The method should invoke one or
013 * more threads to read and sort the data from the collection of Files.
014 * The method should return a sorted list of all of the String data
015 * contained in the files.
016 *
017 * @param files
018 * @return
019 * @throws IOException
020 */
021 public static String[] threadedSort(File[] files) throws IOException
022 {
023 String[] sortedData = new String[0];
024 SortThread[] sortThreads = new SortThread[files.length];
025 for (int i=0; i
026 sortThreads[i] = new SortThread(files[i]);
027 sortThreads[i].start();
028 }
029
030 for (SortThread thread : sortThreads) {
031 try {
032 thread.join();
033 }
034 catch (InterruptedException ex){
035 Logger.getLogger(Sort.class.getName()).log(Level.SEVERE, null, ex);
036 }
037 return sortedData;
038 }
039 throw new java.lang.IllegalStateException(“Method not implemented”);
040 }
041
042 private static class SortThread extends Thread {
043 private File file;
044 private String[] data;
045
046
047 public SortThread(File file) {
048 this.file = file;
049 }
050
051 @Override
052 public void run() {
053 try {
054 data = Sort.getData(file);
055 for (int i = 0; i
056 //Thread.sleep(100);
057 //I commented this out because it was wasting time
058 //System.out.println(data[i]);
059 //I commented this out because I didn’t want to display the entire files
060 }
061 }
062 catch (IOException ex) {
063 Logger.getLogger (Sort.class.getName()).log(Level.SEVERE, null, ex);
064 }
065 }
066 }
067
068 /**
069 * Given an array of files, this method will return a sorted
070 * list of the String data contained in each of the files.
071 *
072 * @param files the files to be read
073 * @return the sorted data
074 * @throws IOException thrown if any errors occur reading the file
075 */
076 public static String[] sort(File[] files) throws IOException {
077
078 String[] sortedData = new String[0];
079
080 for (File file : files) {
081 String[] data = getData(file);
082 data = MergeSort.mergeSort(data);
083 sortedData = MergeSort.merge(sortedData, data);
084 }
085
086 return sortedData;
087
088 }
089 /**
090 * This method will read in the string data from the specified
091 * file and return the data as an array of String objects.
092 *
093 * @param file the file containing the String data
094 * @return String array containing the String data
095 * @throws IOException thrown if any errors occur reading the file
096 */
097 private static String[] getData(File file) throws IOException {
098
099 ArrayList data = new ArrayList();
100 BufferedReader in = new BufferedReader(new FileReader(file));
101 // Read the data from the file until the end of file is reached
102 while (true) {
103 String line = in.readLine();
104 if (line == null) {
105 // the end of file was reached
106 break;
107 }
108 else {
109 data.add(line);
110 }
111 }
112 //Close the input stream and return the data
113 in.close();
114 return data.toArray(new String[0]);
115
116 }
117}
Mergesort.java:
01public class MergeSort {
02
03 // The mergeSort method returns a sorted copy of the
04 // String objects contained in the String array data.
05 /**
06 * Sorts the String objects using the merge sort algorithm.
07 *
08 * @param data the String objects to be sorted
09 * @return the String objects sorted in ascending order
10 */
11 public static String[] mergeSort(String[] data) {
12
13 if (data.length > 1) {
14 String[] left = new String[data.length / 2];
15 String[] right = new String[data.length – left.length];
16 System.arraycopy(data, 0, left, 0, left.length);
17 System.arraycopy(data, left.length, right, 0, right.length);
18
19 left = mergeSort(left);
20 right = mergeSort(right);
21
22 return merge(left, right);
23
24 }
25 else {
26 return data;
27 }
28
29 }
30
31 /**
32 * The merge method accepts two String arrays that are assumed
33 * to be sorted in ascending order. The method will return a
34 * sorted array of String objects containing all String objects
35 * from the two input collections.
36 *
37 * @param left a sorted collection of String objects
38 * @param right a sorted collection of String objects
39 * @return a sorted collection of String objects
40 */
41 public static String[] merge(String[] left, String[] right) {
42
43 String[] data = new String[left.length + right.length];
44
45 int lIndex = 0;
46 int rIndex = 0;
47
48 for (int i=0; i
49 if (lIndex == left.length) {
50 data[i] = right[rIndex];
51 rIndex++;
52 }
53 else if (rIndex == right.length) {
54 data[i] = left[lIndex];
55 lIndex++;
56 }
57 else if (left[lIndex].compareTo(right[rIndex]) < 0) {
58 data[i] = left[lIndex];
59 lIndex++;
60 }
61 else {
62 data[i] = right[rIndex];
63 rIndex++;
64 }
65 }
66
67 return data;
68
69 }
70
71}
SortTest.java:
01import java.io.File;
02import java.io.IOException;
03
04/**
05 * The class SortTest is used to test the threaded and non-threaded
06 * sort methods. This program will call each method to sort the data
07 * contained in the four test files. This program will then test the
08 * results to ensure that the results are sorted in ascending order.
09 *
10 * Simply run this program to verify that you have correctly implemented
11 * the threaded sort method. The program will not verify if your sort
12 * uses threads, but will verify if your implementation correctly
13 * sorted the data contained in the four files.
14 *
15 * There should be no reason to make modifications to this class.
16 */
17public class SortTest {
18
19 public static void main(String[] args) throws IOException {
20
21 File[] files = {new File(“enable1.txt”), new File(“enable2k.txt”), newFile(“lower.txt”), new File(“mixed.txt”)};
22
23 // Run Sort.sort on the files
24 long startTime = System.nanoTime();
25 String[] sortedData = Sort.sort(files);
26 long stopTime = System.nanoTime();
27 double elapsedTime = (stopTime – startTime) / 1000000000.0;
28
29 // Test to ensure the data is sorted
30 for (int i=0; i
31 if (sortedData[i].compareTo(sortedData[i+1]) > 0) {
32 System.out.println(“The data returned by Sort.sort is not sorted.”);
33 throw new java.lang.IllegalStateException(“The data returned by Sort.sort is not sorted”);
34 }
35 }
36 System.out.println(“The data returned by Sort.sort is sorted.”);
37 System.out.println(“Sort.sort took ” + elapsedTime + ” seconds to read and sort the data.”);
38
39 // Run Sort.threadedSort on the files and test to ensure the data is sorted
40 startTime = System.nanoTime();
41 String[] threadSortedData = Sort.threadedSort(files);
42 stopTime = System.nanoTime();
43 double threadedElapsedTime = (stopTime – startTime)/ 1000000000.0;
44
45 // Test to ensure the data is sorted
46 if (sortedData.length != threadSortedData.length) {
47 System.out.println(“The data return by Sort.threadedSort is missing data”);
48 throw new java.lang.IllegalStateException(“The data returned by Sort.threadedSort is not sorted”);
49 }
50 for (int i=0; i
51 if (threadSortedData[i].compareTo(threadSortedData[i+1]) > 0) {
52 System.out.println(“The data return by Sort.threadedSort is not sorted”);
53 throw new java.lang.IllegalStateException(“The data returned by Sort.threadedSort is not sorted”);
54 }
55 }
56 System.out.println(“The data returned by Sort.threadedSort is sorted.”);
57 System.out.println(“Sort.threadedSort took ” + threadedElapsedTime + ” seconds to read and sort the data.”);
58
59
60 }
61
62}
The error am getting:
run:The data returned by Sort.sort is sorted.Sort.sort took 2.072120353 seconds to read and sort the data.The data return by Sort.threadedSort is missing dataException in thread “main” java.lang.IllegalStateException: The data returned by Sort.threadedSort is not sortedat SortTest.main(SortTest.java:49)Java Result: 1BUILD SUCCESSFUL (total time: 3 seconds)
"Looking for a Similar Assignment? Get Expert Help at an Amazing Discount!"