Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: ClassCastException: Vehicle cannot be cast to java.base/java.lang.Comparable

  1. #1
    Junior Member
    Join Date
    Apr 2019
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default ClassCastException: Vehicle cannot be cast to java.base/java.lang.Comparable

    package HRT;
     
    import java.util.TreeSet;
     
    public class Vehicle {
        String type ;
         double speed;
     
     
        public Vehicle() {
            type = "bicycle";
            speed = 20.0 ;
        }
        public Vehicle(String type, double speed) {
            this.type= type ;
            this.speed=speed;
        }
     
     
     
    public static String setType(String type)   {
        return type;
    }
    public static double setSpeed(double speed) {
        return speed;
     
    }
    public void speedUp() {
        speed = speed *2;
     
    }
    public void speedDown() {
        speed = speed/2 ;
     
    }
     
    }
     
    package HRT;
     
    import java.util.TreeSet;
     
     
    public static void partB() {
             String[] typeArray = {"car", "train", "plane", "bicycle",};
             double[] speedArray = {60, 180, 1000, 20};
             TreeSet vehicles = new TreeSet<>();
             for(int i = 0; i < typeArray.length; i++) {
             vehicles.add(new Vehicle(typeArray[i], speedArray[i]));
             }
            }
     
    public static void main(String[] args) { 
            Exercise4.partB();
        }   
    }


    How can I modify the class Vehicle to make the program run correctly ?
    I don't understand why i get this ClassCastException
    can anyone help please ?

  2. #2
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    277
    My Mood
    Amused
    Thanks
    8
    Thanked 19 Times in 19 Posts

    Default Re: ClassCastException: Vehicle cannot be cast to java.base/java.lang.Comparable

    Vehicle class need to implement Comparable interface
    Whatever you are, be a good one

  3. #3
    Junior Member
    Join Date
    Apr 2019
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ClassCastException: Vehicle cannot be cast to java.base/java.lang.Comparable

    Is this correct?


    package HRT;
     
    import java.util.TreeSet;
     
    public class Vehicle implements Comparable {
    	String type ;
    	 double speed;
     
     
    	public Vehicle() {
    		type = "bicycle";
    		speed = 20.0 ;
    	}
    	public Vehicle(String type, double speed) {
    		this.type= type ;
    		this.speed=speed;
    	}
     
     
     
    public static String setType(String type)	{
    	return type;
    }
    public static double setSpeed(double speed) {
    	return speed;
     
    }
    public void speedUp() {
    	speed = speed *2;
     
    }
    public void speedDown() {
    	speed = speed/2 ;
     
    }
     
    public int compareTo(Object temp) {
    	Vehicle other = (Vehicle) temp;
    	if (speed > other.speed)
    	{
    		return 1;
    	}
    	else if(speed < other.speed) {
    		return -1;
    	}
    	else return 0;
     
     
    }
     
    }

  4. #4
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    277
    My Mood
    Amused
    Thanks
    8
    Thanked 19 Times in 19 Posts

    Default Re: ClassCastException: Vehicle cannot be cast to java.base/java.lang.Comparable

    Any errors ? Is the output is desired output ?
    Whatever you are, be a good one

  5. #5
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: ClassCastException: Vehicle cannot be cast to java.base/java.lang.Comparable

    First, your Vehicle class is designed incorrectly.

    1. Your setters are not setting anything but returning something. Setters should not be static
    and should have a return type of void.

    2. You need to add some getters to properly get the value. They should not be static either.

    3. Implementing Comparable is fine and I believe you did it okay. But it is not required. You could also
    have defined a Comparator for Vehicle and passed it to the TreeSet constructor as follows:


    Comparator<Vehicle> comp = Comparator.comparing(Vehicle::getSpeed);
    // then later
    TreeSet<Vehicle> set = new TreeSet<>(comp);

    Regards,
    Jim

Similar Threads

  1. Replies: 17
    Last Post: February 7th, 2019, 12:01 PM
  2. Replies: 2
    Last Post: November 12th, 2012, 03:24 PM
  3. Comparable is implemented, but still " java.lang.Comparable" error appears
    By knowNothing23 in forum What's Wrong With My Code?
    Replies: 14
    Last Post: June 20th, 2012, 04:32 PM
  4. java.lang.String cannot be cast to java.util.Hashtable
    By ashin12 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: April 1st, 2012, 06:17 AM
  5. Replies: 0
    Last Post: December 18th, 2010, 08:08 AM

Tags for this Thread