I cannot figure out what is happening here. I'm trying to create a general max method to handle various array types, but I don't understand where I am trying to cast Integer to Box. I am very new here, so any help would be appreciated.
The one thing that I can thing of is the Object max = 0
line that holds that max value. I chose Object because it can hold anything (as far as I know), but setting it to 0
at first doesn't make much sense, but I can't think of any other way that would work.
EDIT: One more thing, when i test it with the Doubles, it throws the same error, but with java.lang.Integer cannot be cast to java.lang.Double
but this, again, doesn't make sense to me. Where am I trying to cast these things? It must be max
?
Here is my code:
package q3;
import java.util.Random;
public class Biggest {
public static Object max(Comparable[] x) {
Object max = 0;
for (int i = 0; i < x.length; i++) {
System.out.println("Comparing " + x[i] + " to " + max);
if (x[i].compareTo(max) > 0) {
max = x[i];
}
}
return max;
}
public static void main(String[] args) {
Integer[] ai = new Integer[10];
Double[] ad = new Double[10];
fillInts(ai);
fillDoubs(ad);
System.out.println("Max ints: " + max(ai));
//System.out.println("Max doubles: " + max(ad, ad.length));
///// boxes/////////
Box[] boxes = new Box[5];
fillBoxes(boxes);
System.out.println(max(boxes));
}
private static void fillBoxes(Box[] boxes) {
// random class for each of the dimension
Random a = new Random();
Random b = new Random();
Random c = new Random();
int l, w, h;
for (int i = 0; i < boxes.length; i++) {
l = a.nextInt(30);
w = b.nextInt(30);
h = c.nextInt(30);
boxes[i] = new Box(l, w, h);
}
}
private static void fillDoubs(Double[] ad) {
Random r = new Random();
for (int i = 0; i < ad.length; i++) {
ad[i] = 1.0 * r.nextDouble();
}
}
private static void fillInts(Integer[] ai) {
Random r = new Random();
for (int i = 0; i < ai.length; i++) {
ai[i] = r.nextInt(100);
}
}
}