I file read a file with width height and length to a linked list of objects and now I have to find which object from the linked list has the greatest and smallest volume. I tried doing this for the greatest method but I get this error: Exception in thread "main" java.lang.ClassCastException: class java.lang.Double cannot be cast to class Box (java.lang.Double is in module java.base of loader 'bootstrap'; Box is in unnamed module of loader 'app'). Which I think it's because what I am comparing is wrong I am not sure what to compare. And this does have to be done with the compareTo method I have
UPDATE: I have change the compares to head and head.next and now it is printing the correct volume but it is printing null after it how can I remove that. And for the smaller method it is printing an extra volume line when it's only supposed to be one and a nullpointerexception message
static void largestBox(Box head){
while(head != null){
if (head.next.compareTo(head) > 0){
head = head.next;
System.out.println(head.next);
}
head = head.next;
}
}
class Box {
private double width, height, length;
public Box next;
Box(double w, double h, double l){
width=w;
height=h;
length=l;
}
double getVolume(){
return width*height*length;
}
public int compareTo(Object o){
double vol1 = this.getVolume();
double vol2 = ((Box)o).getVolume();
if (vol1>vol2)
return 1;
else if (vol1<vol2)
return -1;
else
return 0;
}
}