Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making your own class 'Comparable'

I followed a tutorial, but failed to make my Country class Comparable for my BST.

Main:

BinarySearchTree A = new BinarySearchTree();
Country a = new Country("Romania", "Bucharest", 1112);
A.insert(a);

Country class:

public int compareTo(Object anotherCountry) throws ClassCastException {
    if (!(anotherCountry instanceof Country))
        throw new ClassCastException("A Country object expected.");
    String anotherCountryName = ((Country) anotherCountry).getName();  
    int i = this.name.compareTo(anotherCountryName);
    if(i < 0){
        return -1;
    } else {
        return 0;
    }
}

error:

@Override
public int compareTo(Object anotherCountry) throws ClassCastException {
    if (!(anotherCountry instanceof Country))
      throw new ClassCastException("A Country object expected.");
    String anotherCountryName = ((Country) anotherCountry).getName();  
    return this.name.compareTo(anotherCountryName);

Description Resource    Path    Location    Type

Name clash: The method compareTo(Object) of type Country has the same erasure as compareTo(T) of type Comparable but does not override it Country.java /Lab2_prob 4/src line 17 Java Problem

Description Resource    Path    Location    Type
The method compareTo(Object) of type Country must override or implement a supertype method  Country.java    /Lab2_prob 4/src    line 17 Java Problem

and class:

public class Country implements Comparable<Country>{
    private String name;
    private String capital;
    private int area;

Description Resource    Path    Location    Type

The type Country must implement the inherited abstract method Comparable.compareTo(Country) Country.java /Lab2_prob 4/src line 2 Java Problem

like image 872
Bogdan M. Avatar asked Oct 24 '12 14:10

Bogdan M.


People also ask

How do you create a comparable class?

To make an object comparable, the class must implement the Comparable interface. negative , if this object is less than the supplied object. zero , if this object is equal to the supplied object. positive , if this object is greater than the supplied object.

What are comparable objects?

A comparable object is capable of comparing itself with another object. The class itself must implements the java. lang. Comparable interface to compare its instances. Consider a Movie class that has members like, rating, name, year.

Is comparable an interface or a class?

Java Comparable interface is used to order the objects of the user-defined class. This interface is found in java. lang package and contains only one method named compareTo(Object). It provides a single sorting sequence only, i.e., you can sort the elements on the basis of single data member only.


2 Answers

Your Country class should implement Comparable:

public class Country implements Comparable<Country>

Then your compareTo method should look like this:

@Override
public int compareTo(Country anotherCountry) {
    return this.name.compareTo(anotherCountry.getName());
}

Note the signature of compareTo. The parameter can (and must) be of type Country, not Object. This is required because of the generic type parameter on Comparable. The upside is you don't have to check the type anymore. The downside is you can only compare Country to other Country objects (or its subtypes), but in most cases this is what you want anyway. If not you have to change the type parameter, e.g. if you use Comparable<Object> the signature of compareTo can be Object again. You can read more about generics here.

like image 55
André Stannek Avatar answered Oct 21 '22 15:10

André Stannek


a Comparable should return:

a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

However, your code is returning only -1 or 0, which is not correct; this implies that this can be less than the other object, or equal, but not greater!

There is no need to modify the values returned by name.compareTo() - you can just return them directly.

like image 42
DNA Avatar answered Oct 21 '22 16:10

DNA