2

I'm trying to write a class, TelemeterTester, which implements the Telemeter class. Telemeter extends Comparator. I keep getting this runtime error...

TelemeterTester.java:12: error: cannot find symbol
  return e1.compareTo(e2);
           ^
  symbol:   method compareTo(Comparable)
  location: variable e1 of type Comparable
  where Comparable is a type-variable:
  Comparable extends Object declared in class TelemeterTester
1 error  

Here is the code for Telemeter interface

/** @param <E> the type on which distance and order are defined
 *
 */
 public interface Telemeter<E> extends Comparator<E> {

   /**
    * Returns the distance between e1 and e2.
    *
    * @param e1 the first object
    * @param e2 the second object
    * @return the distance between e1 and e2
    *
    */
   double distance(E e1, E e2);
 }

Here is my code for TelemeterTester class, which implements Telemeter

public class TelemeterTester<Comparable> implements Telemeter<Comparable>{

   private TelemeterTester() {}

   public double distance(Comparable e1, Comparable e2) {
      return 0;
   }

   public int compare(Comparable e1, Comparable e2) {

      return e1.compareTo(e2);
   }

}

Can anyone explain to me what I'm doing wrong? I don't understand generics very well and I've been stuck on variations of this error for a few hours now.

ordanj
  • 361
  • 1
  • 8
  • 19

3 Answers3

2

The syntax here

public class TelemeterTester<Comparable> implements Telemeter<Comparable>{  

just like

public interface Telemeter<E> extends Comparator<E> {

declares a type parameter called Comparable, just like the second declares a type parameter called E. It doesn't use Comparable as a type argument. Therefore, this method

public int compare(Comparable e1, Comparable e2) {
    return e1.compareTo(e2);
}

uses an unbounded type for its parameters. An unbounded type is basically Object and Object does not declare a compareTo(..) method.

You should read

before you proceed.

Community
  • 1
  • 1
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • So the interface basically says that it can take any Object as a parameter. In TelemeterTester, I'm trying to specify that the objects should implement Comparable. How can I do this? – ordanj Jun 06 '14 at 05:03
  • @ordanj So `Telemeter` is a generic class that has no bounds on the generic type. You also state that a `Telemeter` of any type `E` is also a `Comparator`. In other words, you can use a `Telemeter` to compare two `E`s (those `E`'s can be anything). – Sotirios Delimanolis Jun 06 '14 at 05:07
  • Ohhh, compareTo() is a method of comparable, not comparator. So I will get a type that extends Comparator. So I have to define the comparing rules myself? – ordanj Jun 06 '14 at 05:14
2

Your TelemeterTester should be defined as below:

  public class TelemeterTester<E extends Comparable<E>> implements Telemeter<E> {
    private TelemeterTester() {
    }

    public int compare(E o1, E o2) {
        return o1.compareTo(o2);
    }

    @Override
    public double distance(E e1, E e2) {
        return 0;
    }
}

and referred as :

   TelemeterTester<Integer> t = new TelemeterTester<Integer>();
   System.out.println(t.compare(2, 2));

remember type Integer implements Comparable<Integer>

Shailesh Aswal
  • 6,632
  • 1
  • 20
  • 27
0

"Can anyone explain to me what I'm doing wrong?" Your tester is of type Comparable, which extends the Telemeter, which is also of the type Comparable.

What you really want is that the test extends a generic object, which we normally name E and you want this object to extend the class Comparable of Es. If you replace E with Integer, for example, it will make more sense:

public class TelemeterTester> implements Telemeter

"I don't understand generics very well and I've been stuck on variations of this error for a few hours now."

Josh Bloch has awesome videos on Youtube where he explains PECS :) It will make sense once you see them.

https://www.youtube.com/watch?v=V1vQf4qyMXg

Your code should look like this:

import java.util.*;

/** @param <E> the type on which distance and order are defined
*
*/
interface Telemeter<E> extends Comparator<E> {

  /**
   * Returns the distance between e1 and e2.
   *
   * @param e1 the first object
   * @param e2 the second object
   * @return the distance between e1 and e2
   *
   */
  double distance(E e1, E e2);
}

class TelemeterTester<E extends Comparable<E>> implements Telemeter<E> {

    private TelemeterTester() {}

    @Override
    public int compare(E o1, E o2)
    {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public double distance(E e1, E e2)
    {
        // TODO Auto-generated method stub
        return 0;
    }

 }

Replace the TODO with your own code.

Alexandre Santos
  • 8,170
  • 10
  • 42
  • 64