In: Computer Science
In java:
4. Using non-generic techniques, implement a static method
getMax() that takes an array of type Comparable and returns the
maximum element in the array.
(i.e. "public static Comparable getMax(Comparable []
anArray)").
5. Using the generic techniques to specify super-class
relationships, implement a type safe version of the method in 4
named getMaxGen().
public static Comparable getMax(Comparable [] anArray) {
Comparable max = anArray[0];
for(int i=0; i<anArray.length; i++) {
if(anArray[i].compareTo(max) > 0) {
max = anArray[i];
}
}
return max;
}
public static <T extends Comparable<T>> T getMaxGen(T [] anArray) {
T max = anArray[0];
for(int i=0; i<anArray.length; i++) {
if(anArray[i].compareTo(max) > 0) {
max = anArray[i];
}
}
return max;
}
************************************************** Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.