1

I'm having a bit of trouble adding the two noted above. I have two classes. Employee and Company.

The employee class holds some information about the employees, instance variables including their name, dates, numOfSales etc etc. And also some methods such as setName, getName and so on.

The Company class creates an empty List. It then has a method which reads the list of employees from a text file - which is then how the list gets filled up.

What I need to do is: sort the list by their numOfSales. I first of all need to modify the employee class so that it implements the Comparable interface.

public class Employee implements Comparable<Company>

I'm not sure if company or employee should go in there ^ <> ? But when I try either, I get the error message that 'Employee is not abstract and does not override abstract method CompareTo in java.lang.Comparable' What am I doing wrong here? I must not changed any of the classes, they must not be changed to abstract or anything and I must not add any new ones.

I then need to write a compareTo() method, I take it in the employee class? that will allow the employees to be sorted in ascending order of the value stored by their numOfSales instance variable.

3
  • It should be public class Employee implements Comparable<Employee>, and you should implement public int compareTo(Employee other). Commented Apr 14, 2020 at 11:19
  • @Eran I have tried that also, it still gives the same error message saying that Employee is not abstract. It's probably because I haven't wrote my compare to method? Do you write the compareTo() method before or after the constructor and instance variables? Commented Apr 14, 2020 at 11:22
  • @Nicole That doesn't matter, it's a matter of style. However, it is common to first declare the instance variables, then the constructor(s), and then the methods. Commented Apr 15, 2020 at 9:07

2 Answers 2

2

If you have somekind of List<Employee> the you can use the static method Collections.sort() to sort it without implementing the Comparable interface on the Employee class. You can simply use the variant of the sort function that accepts an Comparator object which will be used to compare two instances of the Employee class.

And you don't even need to implement one on your own. You can simply use Comparator.comparingInt​(e -> e.numOfSales) to create the correct Comparator to sort your list.

So

Collections.sort(employees, Comparator.comparingInt​(e -> e.numOfSales));

would be sufficent to sort your employee list.

Sign up to request clarification or add additional context in comments.

Comments

2

An Employee should be comparable to other Employees, so it's Comparable<Employee>. You then need to implement the compareTo method, as the error message says:

public class Employee implements Comparable<Employee> {
    private int numOfSales;
    // other data members

    // Constructors, getters, setters, etc

    @Override
    public int compareTo(Employee e) {
        return Integer.compare(numOfSales, e.numOfSales);
    }
}

3 Comments

ahh so this explains the message... how would you test the method?
@Nicole create a few Employee instances with difference numOfSales values and check what compareTo returns when you call it on them.
sorry forgot to tag

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.