Write a definition for a generic method named signSort and two utility methods n
ID: 3637812 • Letter: W
Question
Write a definition for a generic method named signSort and two utility methods named onlyLess and onlyMore, so they work with the following utility method and main method to generate an array of Integers between -100 and 100, then print it, then rearrange answer so that all of the Integers less than 0 are before all of the Integers greater than or equal to 0, but otherwise in the original order, and print answer again.
The utility method and main method are as follow:
/**
getRandomList() loads the array with random numbers
@returns an array of 50 Integers between -100 and 100
*/
public static Integer[] getRandomList() {
Integer[] list = new Integer[50];
for (int i=0; i<50; i++)
list[i] = (int)(Math.random()*201) - 100;
return list;
}
// Note: import java.util.*; is assumed
public static void main (String[] args) {
Integer[] answer = getRandomList();
Integer[] less = new Integer[answer.length];
Integer[] more = new Integer[answer.length];
System.out.println(Arrays.toString(answer)+" ");
signSort(answer, new Integer(0), less, more);
System.out.println(Arrays.toString(answer));
}
-The public static void signSort method is a Genericmethod, with a type that must be Comparable or inherit from a Comparable class. It has four parameters.
1) An array of the Generic type elements representing the original array
2) A Generic type parameter representing the zero value.
3) An array of the Generic type elements representing the array of values less than zero.
4) An array of the Generic type elements representing the array of values greater than or equal to zero.
5) signSort calls onlyLess and onlyMore
-The public static onlyLess method returns an int and is a Genericmethod, with a type that must be Comparable or inherit from a Comparable class. It has three parameters.
1) An array of the Generic type elements representing the original array
2) A Generic type parameter representing the zero value.
3) An array of the Generic type elements representing the array of values less than zero.
4) It returns the actual number of elements in the array of values less than zero.
-The public static onlyMore method returns an int and is a Genericmethod, with a type that must be Comparable or inherit from a Comparable class. It has three parameters.
1)An array of the Generic type elements representing the original array
2) A Generic type parameter representing the zero value.
3) An array of the Generic type elements representing the array of values greater than or equal to zero.
4) It returns the actual number of elements in the array of values greater than or equal to zero.
Explanation / Answer
public static void signSort(C[] comparables, C compareTo, C[] less, C[] more) { int sizeOfLess = onlyLess(comparables, compareTo, less); int sizeOfMore = onlyMore(comparables, compareTo, more); int comparablesCounter = 0; for (int x = 0; xRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.