I am having trouble trying to write a method to return the object that has the least volume in a generic ArrayList. These are the guidelines I was given to write the code:
min() - This method takes an ArrayList of Bounded Generic Type which only allows Shape objects and its subclasses. - The method should return the object with the minimum volume from the list of objects.
But I'm not entirely sure if I even followed it right. Is there a way I could use the Collections.min (and Collections.max since I have to write a max volume method too)? I get a bound mismatch error saying: The generic method min(Collection) of type Collections is not applicable for the arguments (ArrayList). The inferred type Shape is not a valid substitute for the bounded parameter >
My Shape class is simply an interface with a getVolume(); method where my other classes (Spheres, Ellipsoids, etc.) override this method:
public interface Shape {
public double getVolume();
}
And here is my min method (in another class with other methods) I'm having problems with:
public static <T> T min() {
ArrayList<? extends Shape> list;
T min = Collections.min(list));
return min;
compareTo()method of some sort. here's the doc for comparable docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html