import java.io.IOException; import java.io.FileReader; import java.io.BufferedReader;…
- User
import java.io.IOException; import java.io.FileReader; import java.io.BufferedReader;
class DataHelper { private Entry[] data;
public DataHelper(int size) { data = new Entry[size]; int k = 0; try{ FileReader fr = new FileReader("StudentWork/Workday2/data.txt"); BufferedReader br = new BufferedReader(fr); String line = br.readLine(); while(line != null) { double x = Double.parseDouble(line.substring(0, line.indexOf(","))); line = line.substring(line.indexOf(",")+1); double y = Double.parseDouble(line.substring(0, line.indexOf(","))); line = line.substring(line.indexOf(",")+1); double thickness = Double.parseDouble(line); data[k] = new Entry(k+1, x, y, thickness); k++; line = br.readLine(); } br.close(); fr.close(); }catch(IOException e2){} } public Entry[] getData() { return data; }}
class Entry { //create PIV's int recordNumber; double gpsX, gpsY, thickness;
//write the constructor with the appropriate parameter list public Entry(int recordNumber, double gpsX, double gpsY, double thickness) { this.recordNumber = recordNumber; this.gpsX = gpsX; this.gpsY = gpsY; this.thickness = thickness; } //write the getThickness method public double getThickness() { return thickness; } //write the toString method public String toString() { String number = "" + recordNumber; // this for loop forces the string to contain 4 characters to keep numbers in the column, not necessary for students to use for(int i = 0; i < 4 - number.length(); i++) { number += " "; } String gps = "" + gpsX + ", " + gpsY; // this for loop forces the string to contain 16 characters to keep numbers in the column, not necessary for students to use for(int i = 0; i < 16 - gps.length(); i++) { gps += " "; } return number + "\t" + gps + "\t" + thickness + "km"; }}
public class Main { public static void main(String[] args) {
DataHelper helper = new DataHelper(50); Entry[] dataList = helper.getData(); Entry[] selectionSort = new Entry[50]; Entry[] insertionSort = new Entry[50]; //write the code to load all the values from //dataList into the selectionSort and //insertionSort arrays System.out.println(dataList[0] + "\n" + selectionSort[0] + "\n" + insertionSort[0] + "\n"); // Linear Search // Target is 4.63 for this sample System.out.println("Sorted data using a selection sort:"); // Selection Sort //print out the sorted data for(Entry e : selectionSort) { System.out.println(e); } System.out.println("\nSorted data using a insertion sort:"); // Insertion Sort //print out the sorted data for(Entry e : insertionSort) { System.out.println(e); } }}
data.txt: -76.783,-104.6,3.38 -75.809,-81.216,4.09 -76.11,-92.196,2.61 -79.732,-146.625,2.59 -78.405,-137.185,1.6 -78.816,-87.483,1.96 -71.849,-105.724,3.27 -75.165,-143.099,1.75 -81.543,-132.752,1.5 -81.555,-115.245,3.48 -75.906,-136.273,1.89 -71.903,-106.182,3.96 -80.859,-116.829,3.15 -74.766,-122.872,2.55 -80.644,-130.913,2.41 -75.14,-84.377,3.28 -74.888,-88.249,2.39 -75.484,-145.537,4.23 -79.729,-90.392,4.72 -80.784,-115.712,2.02 -77.431,-128.127,4.67 -74.757,-85.794,2.15 -77.129,-107.967,3.83 -78.18,-81.709,2.78 -75.354,-83.303,2.89 -76.682,-122.226,2.51 -71.692,-87.324,4.3 -76.737,-119.296,2.07 -77.577,-108.533,3.27 -74.626,-142.541,1.53 -72.253,-89.148,2.06 -71.988,-126.727,4.04 -75.503,-103.192,2.76 -77.386,-119.205,4.51 -81.233,-82.702,2.19 -73.401,-146.565,4.58 -81.595,-115.824,2.45 -77.994,-120.486,4.6 -79.749,-121.272,2.54 -72.895,-91.681,1.63 -79.749,-104.742,3.11 -79.267,-99.575,4.63 -79.99,-111.359,2.57 -78.706,-125.206,4.36 -80.067,-144.711,4.36 -77.393,-127.042,1.27 -80.574,-82.205,1.43 -82.037,-116.204,4.42 -76.249,-122.96,1.24 -80.757,-82.289,4.47
Write the code to load all the values from dataList into the selectionSort and insertionSort Write the code for 2 linear search algorithms in the main() method of the Main class: To find and print the item with the largest ice thickness value in the dataset. To find and print a specific item (ice thickness value of 4.63) in the dataset. If it doesn’t exist, print an appropriate message. Code the implementation of these algorithms under the comments that say 'Linear Search’.
You will need to write an algorithm that implements a Selection Sort on the dataset. Sort the data based on the ice thickness values, least to greatest. Don’t forget to use the Entry class as a reference when trying to determine how to write the algorithm. Code the implementation of these algorithms under the comments that say 'Selection Sort’. Uncomment the line(s) to print out the selectionSort array in order to test your algorithm.
Sort the data based on the ice thickness values, least to greatest. Don’t forget to use the Entry class as a reference when trying to determine how to write the algorithm. Code the implementation of these algorithms under the comments that say 'Insertion Sort’.
- Assistant
To load all the values from dataList into the selectionSort and insertionSort arrays, you can use a for loop to iterate through dataList and assign the values to the respective arrays:
for(int i = 0; i < dataList.length; i++) { selectionSort[i] = dataList[i]; insertionSort[i] = dataList[i]; }Now, implement the Linear Search algorithm to find the item with the largest ice thickness value and a specific value (4.63).
double largestThickness = -1; int indexOfLargest = -1; int indexOfTarget = -1; for(int i = 0; i < dataList.length; i++) { if(dataList[i].getThickness() > largestThickness) { largestThickness = dataList[i].getThickness(); indexOfLargest = i; } if(dataList[i].getThickness() == 4.63) { indexOfTarget = i; } } System.out.println("Largest Ice Thickness: " + dataList[indexOfLargest]); if (indexOfTarget != -1) { System.out.println("Item with 4.63 thickness: " + dataList[indexOfTarget]); } else { System.out.println("Item with 4.63 thickness not found."); }For the Selection Sort algorithm:
for (int i = 0; i < selectionSort.length - 1; i++) { int minIndex = i; for (int j = i + 1; j < selectionSort.length; j++) { if (selectionSort[j].getThickness() < selectionSort[minIndex].getThickness()) { minIndex = j; } } if (minIndex != i) { Entry temp = selectionSort[i]; selectionSort[i] = selectionSort[minIndex]; selectionSort[minIndex]= temp; } }Lastly, implement the Insertion Sort algorithm:
for (int i = 1; i < insertionSort.length; i++) { Entry key = insertionSort[i]; int j = i - 1; while (j >= 0 && insertionSort[j].getThickness() > key.getThickness()) { insertionSort[j + 1] = insertionSort[j]; j = j - 1; } insertionSort[j + 1] = key; }After implementing the algorithms, the main() method should look like:
public static void main(String[] args) { DataHelper helper = new DataHelper(50); Entry[] dataList = helper.getData(); Entry[] selectionSort = new Entry[50]; Entry[] insertionSort = new Entry[50]; for (int i = 0; i < dataList.length; i++) { selectionSort[i] = dataList[i]; insertionSort[i] = dataList[i]; } System.out.println(dataList[0] + "\n" + selectionSort[0] + "\n" + insertionSort[0] + "\n"); // Linear Search double largestThickness = -1; int indexOfLargest = -1; int indexOfTarget = -1; for(int i = 0; i < dataList.length; i++) { if(dataList[i].getThickness() > largestThickness) { largestThickness = dataList[i].getThickness(); indexOfLargest = i; } if(dataList[i].getThickness() == 4.63) { indexOfTarget = i; } } System.out.println("Largest Ice Thickness: " + dataList[indexOfLargest]); if (indexOfTarget != -1) { System.out.println("Item with 4.63 thickness: " + dataList[indexOfTarget]); } else { System.out.println("Item with 4.63 thickness not found."); } System.out.println("Sorted data using a selection sort:"); // Selection Sort for (int i = 0; i < selectionSort.length - 1; i++) { int minIndex = i; for (int j = i + 1; j < selectionSort.length; j++) { if (selectionSort[j].getThickness() < selectionSort[minIndex].getThickness()) { minIndex = j; } } if (minIndex != i) { Entry temp = selectionSort[i]; selectionSort[i] = selectionSort[minIndex]; selectionSort[minIndex]= temp; } } for (Entry e : selectionSort) { System.out.println(e); } System.out.println("\nSorted data using an insertion sort:"); // Insertion Sort for (int i = 1; i < insertionSort.length; i++) { Entry key = insertionSort[i]; int j = i - 1; while (j >= 0 && insertionSort[j].getThickness() > key.getThickness()) { insertionSort[j + 1] = insertionSort[j]; j = j - 1; } insertionSort[j + 1] = key; } for (Entry e : insertionSort) { System.out.println(e); } }With the main() method complete, it will find the largest ice thickness and a specific item using the linear search algorithm, sort the data using the selection sort and insertion sort algorithms, and print out the respective sorted results.
- User
when I run this it returns: StudentWork/Workday2/Main.java:44: error: illegal character: '\u201c' System.out.println(“Item with 4.63 thickness not found.”); ^ StudentWork/Workday2/Main.java:44: error: ';' expected System.out.println(“Item with 4.63 thickness not found.”); ^ StudentWork/Workday2/Main.java:44: error: ';' expected System.out.println(“Item with 4.63 thickness not found.”); ^ StudentWork/Workday2/Main.java:44: error: illegal character: '\u201d' System.out.println(“Item with 4.63 thickness not found.”); ^ StudentWork/Workday2/Main.java:44: error: not a statement System.out.println(“Item with 4.63 thickness not found.”); ^ 5 errors
- Assistant
There is an issue with the quotation marks used in the line
System.out.println(“Item with 4.63 thickness not found.”);. It seems like the quotation marks were copied as "smart quotes" rather than standard quotes. Simply replace the incorrect quotation marks with regular ones in the print statement. Here's the corrected line:System.out.println("Item with 4.63 thickness not found.");Make sure to review the rest of your code and replace any other instances of these "smart quotes" with regular quotes to avoid any further errors.
- User
Efficiency_Analysis.txt:
Group Members' Names: Connor Constantine
LINEAR SEARCH ALGORITHM This is the efficiency of finding a specific value in the dataset using the linear search algorithm.
To get accurate times since the time can change depending on when it’s run, you will perform the recording 10 times and take the average to determine how efficient your algorithm is. Fill in the times below.
Run #1: Run #2: Run #3: Run #4: Run #5: Run #6: Run #7: Run #8: Run #9: Run #10:
The average of running the linear search on 50 items in the dataset is _________. I predict when this algorithm runs for __________ items in the dataset, it will take ______________ to run.
SELECTION SORT ALGORITHM This is the efficiency of sorting the values in ascending order in the dataset using the selection sort algorithm. Record the times for the selection sort for each of the 10 runs.
Run #1: Run #2: Run #3: Run #4: Run #5: Run #6: Run #7: Run #8: Run #9: Run #10:
The average of running the selection sort on 50 items in the dataset is ____________. I predict when this algorithm runs for __________ items in the dataset, it will take ____________ to run.
INSERTION SORT ALGORITHM This is the efficiency of sorting the values in ascending order in the dataset using the insertion sort algorithm. Record the times for the insertion sort for each of the 10 runs.
Run #1: Run #2: Run #3: Run #4: Run #5: Run #6: Run #7: Run #8: Run #9: Run #10:
The average of running the insertion sort on 50 items in the dataset is ____________. I predict when this algorithm runs for _________ items in the dataset, it will take ____________ to run.
Main.java: public class Main { public static void main(String[] args) {
DataHelper helper = new DataHelper(50); Entry[] dataList = helper.getData(); Entry[] selectionSort = new Entry[50]; Entry[] insertionSort = new Entry[50]; //write the code to load all the values from //dataList into the selectionSort and //insertionSort arrays for(int i = 0; i < dataList.length; i++) { selectionSort[i] = dataList[i]; insertionSort[i] = dataList[i]; } System.out.println(dataList[0] + "\n" + selectionSort[0] + "\n" + insertionSort[0] + "\n"); // Linear Search double largestThickness = -1; int indexOfLargest = -1; int indexOfTarget = -1; for(int i = 0; i < dataList.length; i++) { if(dataList[i].getThickness() > largestThickness) { largestThickness = dataList[i].getThickness(); indexOfLargest = i; } if(dataList[i].getThickness() == 4.63) { indexOfTarget = i; } } System.out.println("Largest Ice Thickness: " + dataList[indexOfLargest]); if (indexOfTarget != -1) { System.out.println("Item with 4.63 thickness: " + dataList[indexOfTarget]); } else { System.out.println("Item with 4.63 thickness not found."); } // Target is 4.63 for this sample System.out.println("Sorted data using a selection sort:"); // Selection Sort for (int i = 0; i < selectionSort.length - 1; i++) { int minIndex = i; for (int j = i + 1; j < selectionSort.length; j++) { if (selectionSort[j].getThickness() < selectionSort[minIndex].getThickness()) { minIndex = j; } } if (minIndex != i) { Entry temp = selectionSort[i]; selectionSort[i] = selectionSort[minIndex]; selectionSort[minIndex]= temp; } } //print out the sorted data for(Entry e : selectionSort) { System.out.println(e); } System.out.println("\nSorted data using a insertion sort:"); // Insertion Sort for (int i = 1; i < insertionSort.length; i++) { Entry key = insertionSort[i]; int j = i - 1; while (j >= 0 && insertionSort[j].getThickness() > key.getThickness()) { insertionSort[j + 1] = insertionSort[j]; j = j - 1; } insertionSort[j + 1] = key; } //print out the sorted data for(Entry e : insertionSort) { System.out.println(e); } }}
Add the code to the linear search segment to capture the running time for that algorithm. Add the code to the selection sort segment to capture the running time for that algorithm. Add the code to the insertion sort segment to capture the running time for that algorithm. Print out the running time for each of the algorithms.
- Assistant
To measure the running time for the Linear Search, Selection Sort, and Insertion Sort algorithms, you can use
System.nanoTime()to capture the start and end times and calculate the difference.First, add the following code to capture the running time for the Linear Search segment:
// Linear Search long linearSearchStartTime = System.nanoTime(); ... if (indexOfTarget != -1) { ... } else { ... } long linearSearchEndTime = System.nanoTime(); long linearSearchTime = linearSearchEndTime - linearSearchStartTime; System.out.println("Linear Search Time: " + linearSearchTime + " nanoseconds");Next, add code to capture the running time for the Selection Sort segment:
// Selection Sort long selectionSortStartTime = System.nanoTime(); ... // print out the sorted data for (Entry e : selectionSort) { ... } long selectionSortEndTime = System.nanoTime(); long selectionSortTime = selectionSortEndTime - selectionSortStartTime; System.out.println("Selection Sort Time: " + selectionSortTime + " nanoseconds");Finally, add code to capture the running time for the Insertion Sort segment:
// Insertion Sort long insertionSortStartTime = System.nanoTime(); ... // print out the sorted data for (Entry e : insertionSort) { ... } long insertionSortEndTime = System.nanoTime(); long insertionSortTime = insertionSortEndTime - insertionSortStartTime; System.out.println("Insertion Sort Time: " + insertionSortTime + " nanoseconds");Now, you should have the running times for each of the algorithms printed out in the console. You can record these times and average them over multiple runs, as described in the Efficiency_Analysis.txt file.
- User
can you show what the code will look like now?
- Assistant
Here's the complete modified
main()method with the added code for capturing the running times for each algorithm:public static void main(String[] args) { DataHelper helper = new DataHelper(50); Entry[] dataList = helper.getData(); Entry[] selectionSort = new Entry[50]; Entry[] insertionSort = new Entry[50]; for (int i = 0; i < dataList.length; i++) { selectionSort[i] = dataList[i]; insertionSort[i] = dataList[i]; } System.out.println(dataList[0] + "\n" + selectionSort[0] + "\n" + insertionSort[0] + "\n"); // Linear Search long linearSearchStartTime = System.nanoTime(); double largestThickness = -1; int indexOfLargest = -1; int indexOfTarget = -1; for(int i = 0; i < dataList.length; i++) { if(dataList[i].getThickness() > largestThickness) { largestThickness = dataList[i].getThickness(); indexOfLargest = i; } if(dataList[i].getThickness() == 4.63) { indexOfTarget = i; } } System.out.println("Largest Ice Thickness: " + dataList[indexOfLargest]); if (indexOfTarget != -1) { System.out.println("Item with 4.63 thickness: " + dataList[indexOfTarget]); } else { System.out.println("Item with 4.63 thickness not found."); } long linearSearchEndTime = System.nanoTime(); long linearSearchTime = linearSearchEndTime - linearSearchStartTime; System.out.println("Linear Search Time: " + linearSearchTime + " nanoseconds"); System.out.println("Sorted data using a selection sort:"); // Selection Sort long selectionSortStartTime = System.nanoTime(); for (int i = 0; i < selectionSort.length - 1; i++) { int minIndex = i; for (int j = i + 1; j < selectionSort.length; j++) { if (selectionSort[j].getThickness() < selectionSort[minIndex].getThickness()) { minIndex = j; } } if (minIndex != i) { Entry temp = selectionSort[i]; selectionSort[i] = selectionSort[minIndex]; selectionSort[minIndex]= temp; } } // print out the sorted data for (Entry e : selectionSort) { System.out.println(e); } long selectionSortEndTime = System.nanoTime(); long selectionSortTime = selectionSortEndTime - selectionSortStartTime; System.out.println("Selection Sort Time: " + selectionSortTime + " nanoseconds"); System.out.println("\nSorted data using an insertion sort:"); // Insertion Sort long insertionSortStartTime = System.nanoTime(); for (int i = 1; i < insertionSort.length; i++) { Entry key = insertionSort[i]; int j = i - 1; while (j >= 0 && insertionSort[j].getThickness() > key.getThickness()) { insertionSort[j + 1] = insertionSort[j]; j = j - 1; } insertionSort[j + 1] = key; } // print out the sorted data for (Entry e : insertionSort) { System.out.println(e); } long insertionSortEndTime = System.nanoTime(); long insertionSortTime = insertionSortEndTime - insertionSortStartTime; System.out.println("Insertion Sort Time: " + insertionSortTime + " nanoseconds"); }This code includes the time tracking for the Linear Search, Selection Sort, and Insertion Sort algorithms to calculate their running times. It also prints out the running times for each algorithm in nanoseconds.