-2

I have this method but it returns the error MyComparator is not abstract and does not overs the abstract method I have also tried using just Present instead of PresentInterface as I have already implemented the present interface in the present class. I eventually want to sort an arrayList of Presents Any help would be much appreciated thank you

public class MyComparator implements Comparable<PresentInterface>{
 // The name of the present, e.g. "one directions greatest hits"
 private String name;

 // The type of the present, e.g. music, sweets, dvds, games.
 private String type;

 //The cost of the present, e.g. £11.99
 private double cost;





/**
 * Sets the name of the present.
 * 
 * @param - name - the name of the present.
 */
 public void setName(String name) {
    this.name = name;
 }


 /**
 * Sets the type of the present.
 * 
 * @param - type - the type of the present.
 */
  public void setType(String type) {
     this.type = type;
  }


  /**
  * Sets the cost of the present.
 * 
 * @param - cost - the cost of the present.
 */
 public void setCost(double cost) {
    this.cost = cost;
  }


 /**
 * Gets the name of the present.
 * 
 * @return - the name of the present.
 */
 public String getName() {
    return name;
 }


 /**
 * Gets the type of the present.
 * 
 * @return - the type of the present.
 */
 public String getType() {
    return type;
 }


 /**
 * Gets the cost of the present.
 * 
 * @return - the cost of the present.
 */
 public double getCost() {
    return cost;
 }


 /**
 * Converts the fields in this present object to a nicer string representation.
 * 
 * @return - the present information in the form of a string: name - type - cost.
 */
  public String toString() {
    return name + " - " + type + " - " + cost;
 }
  @Override
  public double compare( Present a){
     return this.cost.compareTo(a.getCost());

    }
Amy
  • 1

2 Answers2

2

You're overriding the wrong method. It should be

@Override
public int compareTo(PresentInterface other) {
    return this.cost.compareTo(other.getCost());
}

As fge notes, the API will tell you everything that you need to know to solve this and similar questions. Please have a look at it.


If your class is to be used as a Comparator, then it should implement the Comparator interface, not the Comparable interface. And then the compare method should take two parameters. Otherwise, if the class is truly going to be a Comparable, then the Comparable interface's generic should be that of the class itself.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • And shouldn't it's parameter be PresentInterface? – MadProgrammer Jan 01 '15 at 20:49
  • I still receive the same error – Amy Jan 01 '15 at 20:49
  • @MadProgrammer: yep. Please edit the answer since it's now a community wiki. – Hovercraft Full Of Eels Jan 01 '15 at 20:49
  • @Amy: if you're using the class as a Comparator, then you're implementing the wrong interface. – Hovercraft Full Of Eels Jan 01 '15 at 20:52
  • I've changed the start to public class MyComparator implements Comparable and the compareTo also as this is what it needs to be i have looked at the API however i continue to receive the same error – Amy Jan 01 '15 at 20:54
  • @Amy: please provide more details in your question itself. Is this to be used as a Comparator or are you making a Comparable class? If so, the generic should be the same type as the class itself. – Hovercraft Full Of Eels Jan 01 '15 at 20:54
  • i have an arrayList of presents in another class that I need to order by the cost of the present – Amy Jan 01 '15 at 20:55
  • @Amy: then the Present class needs to implement the `Comparable` interface. Either that or create a Comparator, but get rid of all the useless and unnecessariy fields in the comparator. All it would need would be the `compare(...)` method, that's it. – Hovercraft Full Of Eels Jan 01 '15 at 20:57
  • I have changed it to that but the error still occurs is it due to the Present class already implementing a PresentInterface class – Amy Jan 01 '15 at 20:59
  • I currently have this public class MyComparator implements Comparable{ private double cost; @Override public double compareTo( Present other){ return this.cost.compareTo(other.getCost()); } } – Amy Jan 01 '15 at 21:00
  • Please don't post code in comments since it loses its formatting making it unreadable. Instead, post any new code to the bottom of your original question by [editing your question](http://stackoverflow.com/posts/27734507/edit). This has been requested of you in previous comments, so please comply so that you can help us help you! – Hovercraft Full Of Eels Jan 01 '15 at 21:14
0

you should implement public int compareTo( PresentInterface a) not public double compare( Present a)

hasan
  • 966
  • 2
  • 13
  • 40