Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Given the following 9 files: 1- Recall the SortableObject interface from class.

ID: 3655772 • Letter: G

Question

Given the following 9 files: 1- Recall the SortableObject interface from class. + Modify the Car and Student classes to implement the SortableObject interface. - Cars should be sorted by mpg (miles per gallon) - Students s should be sorted by gpa 2- Modify the Sorter interface, BubbleSorter, SelectionSorter, TimeSorters, and TestSorter classes to operate over SortableObjects. + E.g., in TestSorters, the testSorter method should have parameters SortableObject[] a, Sorter s (rather than int[] a) + In the main method of TestSorters, call test methods using both Car arrays and Student arrays to make sure both work with all sorting algorithms. 3- Implement a class QuickSorter which implements the QuickSort algorithm. + You can use the web to find descriptions of how this algorithm works. + You should write an "in place" version of the algorithm, which means that you modify the input array, rather than creating a new array. + You can search Google for "quicksort" "in place" to find pseuo-code to implement this. + QuickSorter should implement the Sorter interface. + Add QuickSorter to the TestSorter main method to make sure it works. 4- Modify the makeArray method of TimeSorters to return an array of Car objects with mpg values in descending order + e.g., makeArray(10) would return Cars with mpgs of 10,9,...1 5- Modify the main method of TimeSorters to compare BubbleSorter, SelectionSorter, and QuickSorter on arrays with sizes from 1000,2000,3000,...,20000. + Report the running times for each call to the sort method + At the end of the main method, report the TOTAL and AVERAGE times for each algorithm. I BELIEVE I COMPLETED THE STUDENT AND CAR FILES(please double check) BUT NOW I AM UNSURE WHAT TO DO NEXT..... FILES: // TODO: Modify so Student implements the SortableObject interface. // Students should be sorted by gpa public class Student implements SortableObject { String name; double gpa; public Student(String n, double g) { name = n; gpa = g; } public String getName() { return name; } public double getGpa() { return gpa; } public String toString() { return name + " " + gpa; } @Override public double getSortValue() { // TODO Auto-generated method stub return getGpa(); } } // TODO: Modify to implement the SortableObject interface // Cars should be sorted by mpg. public class Car implements SortableObject { private String model; private double mpg; public Car(String model, double mpg) { this.model = model; this.mpg = mpg; } public String getModel() { return model; } public double getMpg() { return mpg; } public String toString() { return model + " " + mpg; } @Override public double getSortValue() { // TODO Auto-generated method stub return getMpg(); } } // done. public interface SortableObject { public abstract double getSortValue(); } // TODO Modify to operate over arrays of SortableObjects public interface Sorter { void sort(int[] a); } // TODO: Modify to operate over arrays of SortableObjects public class SelectionSorter implements Sorter { @Override public void sort(int[] a) { for (int i = 0; i < a.length; i++) { int minIdx = min(a, i); swap(a, i, minIdx); } } private int min(int[] a, int start) { // return index of minimum value // to the "right" of start int minVal = a[start]; int minIdx = start; for (int i = start; i < a.length; i++) { if (a[i] < minVal) { minVal = a[i]; minIdx = i; } } return minIdx; } private void swap(int[] a, int i, int j) { int tmp = a[i]; a[i] = a[j]; a[j] = tmp; } } // TODO Convert this class to operate over SortableObject arrays instead of int arrays. public class QuickSorter implements Sorter { @Override public void sort(int[] a) { // TODO Implement this method! You'll have to change the input type to a SortableObject[] array } } // TODO: Modify to operator over SortableObjects, rather than int arrays. public class BubbleSorter implements Sorter { @Override public void sort(int[] a) { boolean isSorted = false; while (!isSorted) { isSorted = true; for (int i = 0; i < a.length-1; i++) { if (a[i] > a[i+1]){ swap(a, i, i+1); isSorted = false; } } } } private void swap(int[] a, int i, int j) { int tmp = a[i]; a[i] = a[j]; a[j] = tmp; } } import java.util.Arrays; // TODO Modify to use SortableObject arrays. // TODO Add QuickSorter to the list of classes to be tests // TODO Test all algorithms with arrays of Students and arrays of Cars public class TestSorters { public static boolean testSorter(int[] a, Sorter s) { s.sort(a); for (int i = 0; i < a.length-1; i++) { if (a[i] > a[i+1]) { System.out.println(i + " out of order." + Arrays.toString(a)); return false; } } System.out.println("sorted!"); return true; } public static void main(String[] args) { testSorter(new int[]{5,4,3,2,1}, new BubbleSorter()); testSorter(new int[]{1,2,3,4,5}, new BubbleSorter()); testSorter(new int[]{1,-2,30,-4,15}, new BubbleSorter()); testSorter(new int[]{5,4,3,2,1}, new SelectionSorter()); testSorter(new int[]{1,2,3,4,5}, new SelectionSorter()); testSorter(new int[]{1,-2,30,-4,15}, new SelectionSorter()); } } // TODO: // 1- Modify this file to use SortableObject arrays instead of int arrays // 2- Modify makeArray to return a Car array with mpgs in decreasing order. E.g., makeArray(3) // will return an array of 3 Car objects with mpgs equal to 3,2,1. // 3- Add QuickSorter to the list of algorithms to be compared // 4- Report the TOTAL and AVERAGE running times for each algorithm. public class TimeSorters { public static long time(Sorter s, int[] a) { long start = System.currentTimeMillis(); s.sort(a); long end = System.currentTimeMillis(); return end - start; } public static void main(String[] args) { System.out.println("N BubbleTime SelectionTime"); long bubbleTotal = 0; long selectionTotal = 0; for (int size = 1000; size <= 20000; size += 1000) { long selectionTime = time(new SelectionSorter(), makeArray(size)); long bubbleTime = time(new BubbleSorter(), makeArray(size)); System.out.println(size + " " + bubbleTime + " " + selectionTime); bubbleTotal += bubbleTime; selectionTotal += selectionTime; } System.out.println("TOTAL " + bubbleTotal + " " + selectionTotal); } public static int[] makeArray(int size) { int[] a = new int[size]; for (int i = 0; i < size; i++) { a[i] = size - i; } return a; } }

Explanation / Answer

what is it man

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote