Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Application: Implementing and Analyzing a Threaded Sort Algorithm Once a software application has been implemented and released, that is by no means the end of

Application: Implementing and Analyzing a Threaded Sort Algorithm

Once a software application has been implemented and released, that is by no means the end of development. There are all kinds of causes requiring software to be changed, from users reporting bugs, through changes in third-party software, to updates for improved performance. A critical skill of software developers is the ability to understand and update a programs algorithm. For this Assignment, you will update an existing Java program using the NetBeans IDE. You will use the NetBeans Integrated Development Environment to make changes to an existing NetBeans Java Project. This existing program correctly sorts the data without using threads and contains a method (threadedSort) that you will update to sort the data using threads.

To prepare:

Read this weeks Resources.

Download and Install NetBeans: Visit the NetBeans download page from this weeks Resources. Download the Java SE bundle. (Note: Select the most recent release version, not a Beta version.) Run the downloaded package to install it. Choose the default options for installation.

Unzip and open the Unthreaded Program:

Download and unzip the file Week2_Project.zip. T

his will create a folder containing a NetBeans Project with the non-threaded java program.

Note the location of this folder. Launch the NetBeans IDE. Click the Open Project... button (Or click the File drop-down menu and select Open Project).

Navigate to the project folder, select it, and click Open Project.

The Java Project contains three class files: MergeSort.java, Sort.java, and SortTest.java. Double-click each file in the Project section (at the left of the NetBeans IDE window) to display the files contents. Each of the Java class files contains comments explaining the purpose of the class and the purpose of the class methods.

Modify the Java program by adding threads, and analyze the performance of both the threaded and non-threaded versions.

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 (13 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.

ERROR RECEIVED WHEN I RUN THE PROGRAM

MEGASORT.JAVA FILE

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

SORT.JAVA FILE

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 data = new ArrayList(); 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]);

} }

SORTTEST.JAVA FILE

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 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(); 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 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."); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Graph Database Modeling With Neo4j

Authors: Ajit Singh

2nd Edition

B0BDWT2XLR, 979-8351798783

More Books

Students also viewed these Databases questions

Question

Is the style consistent?

Answered: 1 week ago

Question

600 lb 20 0.5 ft 30 30 5 ft

Answered: 1 week ago