So, I have been at this now for a few hours and near as I can tell I have the interface done right as well as the compareTo
method but I get a ClassCastException
whenever I try to run the program (although it builds fine). Can anyone spot where I'm going wrong?
import java.util.*;
public class assignment7 {
public static void main(String[] args) {
// Q1 Test ======================================
Car c1 = new Car("96-D-0003", "Toyota", 20000, 800);
Car c2 = new Car("96-D-0002", "Nissan", 25000, 1000);
if (c1.compareTo(c2) <= 0) {
System.out.println(c1 + "\n" + c2);
} // Test compareTo method
else {
System.out.println(c2 + "\n" + c1);
}
System.out.println(c1.hashCode()); // Test hashCode method
c1.toString(); // Test toString method
System.out.print(c1); // Print the string
//End Q1 Test ===================================
// Question 2 ===================================
Car ca[] = {
new Car("96-D-005", "Toyota", 20000, 800),
new Car("96-D-004", "Toyota", 20000, 800),
new Car("96-D-003", "Toyota", 20000, 800),
new Car("96-D-002", "Toyota", 20000, 800),
new Car("96-D-001", "Toyota", 20000, 800),
}; // New array of cars
// Sort Array
Arrays.sort(ca);
//End Q2 =======================================
}
}
interface Comparable<T> {
public int compareTo(T ob);
}
final class Car implements Comparable<Car> {
private final String reg;
private final String make;
private final int mileage;
private final double value;
Car(String r, String m, int mi, int v) {
reg = r;
make = m;
mileage = mi;
value = v;
}
public String reg() {
return reg;
}
public String make() {
return make;
}
public int mileage() {
return mileage;
}
public double value() {
return value;
}
public String toString() {
return "(" + reg + ", " + make + ", " + mileage + ", " + value + ")";
} // New String method
public int compareTo(Car c) throws ClassCastException { // Cars are compared by reg number
if (reg == null) {
return -1;
} // No reg car
else {
return (reg.compareTo(c.reg));
} // New compare method to compare car regs
}
}
Exception output is as follows;
Exception in thread "main" java.lang.ClassCastException: Car cannot be cast to java.lang.Comparable
at java.util.ComparableTimSort.countRunAndMakeAscending(ComparableTimSort.java:290)
at java.util.ComparableTimSort.sort(ComparableTimSort.java:157)
at java.util.ComparableTimSort.sort(ComparableTimSort.java:146)
at java.util.Arrays.sort(Arrays.java:472)
at assignment7.main(assignment7.java:33)
Your problem is that you have declared your own version of the Comparable
interface. Your Car
is implementing your version of Comparable
whilst the Arrays.sort
is using the java.lang.Comparable
version. Delete your implementation of the Comparable
interface, re-compile and run.
This was my output after the fix:
(96-D-0002, Nissan, 25000, 1000.0)
(96-D-0003, Toyota, 20000, 800.0)
1747585824
(96-D-0003, Toyota, 20000, 800.0)
Please also take time to format your code properly (it's better for you and for others). Also, your class name should start with a capital letter.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.