Question: CODE IN JAVA GenericMethods Class (10 pts.) make two static genetic methods maxElement() and minElement(). The methods take an array of objects of an arbitrary
CODE IN JAVA
GenericMethods ClassÂ- (10 pts.) make two static genetic methods maxElement() and minElement(). The methods take an array of objects of an arbitrary form and find the largest and the smallest objects correspondingly. The type of objects must be limited to those that implement Comparable interface.
- (10 pts.) Modify quicksort() implementation in such a way that it becomes a generic method and can work with objects that implement Comparable interface. make it as a static method to GenericMethods class. Make this quicksort() have a boolean parameter that determines whether the array will be sorted in ascending or descending order. See examples below:
int[] array = {3, 5, 6, 1, 2, 4}; GenericMethods.quicksort(array, true); => {1, 2, 3, 4, 5, 6} GenericMethods.quicksort(array, false); => {6, 5, 4, 3, 2, 1}
CODE PROVIDED TestsGenericMethods
import java.io.PrintStream;
public class TestsGenericMethods {
/** * All tests for Generic Methods class * @return total score for AggregationClass part of assignment */ public static void allTestsGenericMethods(PrintStream out) {
out.println(" ----Tests for all static Generic Methods ----"); testSet02GenericMethods(out); testSet03GenericMethods(out); } /** * Set of unit tests for maxElement(), minElement() * @param outputStream stream to direct output into * @return number of points earned for this unit. 0 is returned if even one of the tests failed */ public static void testSet02GenericMethods(PrintStream outputStream) { outputStream.println(" ----Test Set 2----"); Date[] dArray = new Date[7]; dArray[0] = new Date(1, 1, 2019); dArray[1] = new Date(1, 1, 1925); dArray[2] = new Date(2, 1, 2019); dArray[3] = new Date(2, 1, 2019); dArray[4] = new Date(2, 2, 2019); dArray[5] = new Date(2, 5, 2019); dArray[6] = new Date(2, 5, 2019); Double[] doubleArray = new Double[8]; doubleArray[0] = 2.0; doubleArray[1] = 22.0; doubleArray[2] = 3.0; doubleArray[3] = 234.0; doubleArray[4] = 2.0; doubleArray[5] = -5.0; doubleArray[6] = 66.0; doubleArray[7] = 10.0; // Test #1
int index1 = GenericMethods.maxElement(dArray); int index3 = GenericMethods.minElement(dArray); int index2 = GenericMethods.maxElement(doubleArray); int index4 = GenericMethods.minElement(doubleArray); if(index1 == 5 && index2 == 3) { outputStream.printf("%-80s%-10s ", "Test Set 02: Test for maxElement()", "PASSED"); } else outputStream.printf("%-80s%-10s ", "Test Set 02: Test for maxElement()", "FAILED"); // Test #2 if(index3 == 1 && index4 == 5) { outputStream.printf("%-80s%-10s ", "Test Set 02: Test for minElement()", "PASSED"); } else outputStream.printf("%-80s%-10s ", "Test Set 02: Test for minElement()", "FAILED");
} /** * Set of unit tests for quickSort() * @param outputStream stream to direct output into * @return number of points earned for this unit. 0 is returned if even one of the tests failed */ public static void testSet03GenericMethods(PrintStream outputStream) { outputStream.println(" ----Test Set 3 ----"); Date[] dArray = new Date[7]; dArray[0] = new Date(1, 1, 2019); dArray[1] = new Date(1, 1, 1925); dArray[2] = new Date(2, 1, 2019); dArray[3] = new Date(2, 1, 2019); dArray[4] = new Date(2, 2, 2019); dArray[5] = new Date(2, 5, 2019); dArray[6] = new Date(2, 5, 2019); Integer[] intArray = new Integer[8]; intArray[0] = 2; intArray[1] = 22; intArray[2] = 3; intArray[3] = 234; intArray[4] = 2; intArray[5] = -5; intArray[6] = 66; intArray[7] = 10; // Test #1 GenericMethods.quicksort(dArray, true); GenericMethods.quicksort(intArray, false); int c1 = 0; int c2 = 0; for(int i = 0; i < dArray.length-1; i++) { if(dArray[i].compareTo(dArray[i+1])> 0) { break; } c1++; } for(int i = 0; i < intArray.length-1; i++) { if(intArray[i].compareTo(intArray[i+1])< 0) { break; } c2++; } if(c1==dArray.length-1) // number of comparisons { outputStream.printf("%-80s%-10s ", "Test Set 03: Test for quicksort() - ascending", "PASSED"); } else outputStream.printf("%-80s%-10s ", "Test Set 03: Test for quicksort() - ascending", "FAILED"); // Test #2 if(c2==intArray.length-1) { outputStream.printf("%-80s%-10s ", "Test Set 02: Test for quicksort() - descending", "PASSED"); } else outputStream.printf("%-80s%-10s ", "Test Set 02: Test for quicksort() - descending", "FAILED");
} }
Step by Step Solution
There are 3 Steps involved in it
Heres the code for the GenericMethods class with the requested functionalities Java public class GenericMethods public static T maxElementT arr T max arr0 for int i 1 i arrlength i if arricompareTomax ... View full answer
Get step-by-step solutions from verified subject matter experts
