public abstract class Main implements Comparable {
public static void main(String[] args) {
Integer[] intArray = {1,2,3,4,5,6,7,8,9,10};
String[] stringArray = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"};
java.util.Date[] dateArray = {};
for (int j = 0; j < 10 ; j++)
dateArray[j] = new java.util.Date();
/* Code to call max method and display the largest value of these */
}
public static Object max (Comparable[] a){
Object tempObj = new Object();
for (int i = 0; i < a.length - 1; i++){
if ((a[i]).compareTo(a[i+1]) > 0 )
tempObj = a[i];
else
tempObj = a[i+1];
}
return tempObj;
}
public int compareTo(Object o) {
if (/*this*/ > o)
return 1;
else if (/*this*/ < o)
return -1;
else
return 0;
}
}
While it may be easier to write it in a generic max(a, b) format, one of the requirements is for it to be written this way. I can't find a way to reference the value of the object that is actually calling the compareTo method.
Concerns:
Instead just use the compareTo method that you know your Comparable objects have. You know that each item in the a array, no matter what type of item it is, implements Comparable, and so you can call this method directly on the item:
For example,
a[i].compareTo(a[i+1])
You know of course that the simplest solution is to just call java.util.Arrays.sort(a) on the array a and just take the 0th item.