0

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)
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
I2obiN
  • 181
  • 3
  • 18
  • Does the Car class implement the Comparable interface? – Vincent Ramdhanie Nov 20 '14 at 01:36
  • 9
    Why are you creating your own interface Comparable? – CBredlow Nov 20 '14 at 01:38
  • 9
    Are you defining your own Comparable interface? You should use java.lang.Comparable. – jmluy Nov 20 '14 at 01:39
  • The main issue you are having is that you are creating your own version of Comparable, instead of using the one that Java provides. If you remove your interface, then it will work fine. – CBredlow Nov 20 '14 at 01:43
  • possible duplicate of [How to implement the Java comparable interface?](http://stackoverflow.com/questions/21626439/how-to-implement-the-java-comparable-interface) – CBredlow Nov 20 '14 at 01:43
  • Okay so removed the interface. Unfortunately still getting the same runtime error/exception – I2obiN Nov 20 '14 at 01:49
  • 1
    Clean your project and recompile. – Radiodef Nov 20 '14 at 01:55
  • 2
    Hey, just del'd all class files and did a rebuild. Worked fine. I guess it was building from old class files or something :S Thanks guys, v much appreciated :) – I2obiN Nov 20 '14 at 02:00

1 Answers1

1

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.

Don Branson
  • 13,631
  • 10
  • 59
  • 101
Romski
  • 1,912
  • 1
  • 12
  • 27