Lish
Lish

Reputation: 307

Can't figure out a compilation error for simple Java app

This is the error I am getting: Error: The type Customer must implement the inherited abstract method java.lang.Comparable.compareTo(Customer)

I'm comparing it to some lab work that I did and it looks exactly the same, yet that compiled just fine. I'm not sure what's going on here.

Here is the code segment in question which, incidentally, was written by my professor:

class Customer implements Comparable<Customer>
{
 String name;
 double purchase;
 double rebate;

public Customer(String n, double p, double r)
{ name=n;
  purchase=p;
  rebate=r;
 } //end Constructor

 public Customer(String n)
 { 
  name=n;
 } //end Constructor

 public String toString()
 {
  return String.format("%-20s%10.2f%10.2f", name, purchase, rebate);
 }

 /*
  Here, define the method comparedTo of the Comparable
  interface so that an array of Customer objects can
  be sorted by customer name
 */
 public int comparedTo(Customer a)
{
return this.name.compareTo(a.name);
}   //end comparedTo

} //end class Customer

Oh, and here are the inclusions the professor included:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

Your help is greatly appreciated!

Upvotes: 1

Views: 160

Answers (2)

Isaac Truett
Isaac Truett

Reputation: 8874

As Steven correctly pointed out, the name of the method you were attempting to implement was misspelled. A great way to catch errors like that is the @Override annotation, like this:

@Override
public int comparedTo(Customer a)

This tells the compiler that you intended comparedTo() to override a method declared in a parent class. When the compiler doesn't find a comparedTo() method to override, it will tell you. As of Java 1.6, @Override can also be used on methods declared in interfaces.

Upvotes: 0

Steven Jeuris
Steven Jeuris

Reputation: 19120

comparedTo should be compareTo.

The error says it all. ;p

Upvotes: 3

Related Questions