0

I'm running into this error: cannot find symbol - method sort(java.util.ArrayList)

I'm trying to sort an ArrayList and print it.

Here is the code, I'm also overriding the compareTo methods in classes HockeyPlayer, Professor, Parent and GasStation. Thanks. P

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Collections;
/**
 * Class Employees.
 */
public class Employees
{
    private ArrayList<Employee> employeeList;

    /**
     * Constructor for objects of class Employees
     */
    public Employees()
    {
        employeeList = new ArrayList<Employee>();
        //Creating 5 each types of Employees
        employeeList.add(new HockeyPlayer("Wayne Gretzky", 894));
        employeeList.add(new HockeyPlayer("Who Ever", 0));
        employeeList.add(new HockeyPlayer("Brent Gretzky", 1));
        employeeList.add(new HockeyPlayer("Pavel Bure", 437));
        employeeList.add(new HockeyPlayer("Jason Harrison", 0));

        employeeList.add(new Professor("Albert Einstein", "Physics"));
        employeeList.add(new Professor("Jason Harrison", "Computer Systems"));
        employeeList.add(new Professor("Richard Feynman", "Physics"));
        employeeList.add(new Professor("BCIT Instructor", "Computer Systems"));
        employeeList.add(new Professor("Kurt Godel", "Logic"));

        employeeList.add(new Parent("Tiger Woods", 1));
        employeeList.add(new Parent("Super Mom", 168));
        employeeList.add(new Parent("Lazy Larry", 20));
        employeeList.add(new Parent("Ex Hausted", 168));
        employeeList.add(new Parent("Super Dad", 167));

        employeeList.add(new GasStation("Joe Smith", 10));
        employeeList.add(new GasStation("Tony Baloney", 100));
        employeeList.add(new GasStation("Benjamin Franklin", 100));
        employeeList.add(new GasStation("Mary Fairy", 101));
        employeeList.add(new GasStation("Bee See", 1));
    }

    /**
     * Display the list of employee
     */
    public void displayEmployees()
    {
        Iterator <Employee> it = employeeList.iterator();
        while(it.hasNext()) {
            Employee e = it.next();
            System.out.println(e);

        }
    }
    /**
     * Display the list of employee sorted
     */
    public void displaySortedEmployees()
    {
        **Collections.sort(employeeList);**
        Iterator <Employee> it = employeeList.iterator();
        while(it.hasNext()) {
            Employee e = it.next();
            System.out.println(e);

        }
    }
}

I added: implements comparable to Employee class and it compiles now, but I need to compare the different sublclasses : HockeyPlayer, Parent and on.... When calling the method this is the new error message: java.lang.ClassCastException, Professor cannot be cast to HockeyPlayer

Here is one of the subclasses:

/**
 * Class HockeyPlayer.
 */
public class HockeyPlayer extends Employee implements Employable, Comparable<Employee>
{
    private       int     numberOfGoals;
    private       double  overTimePayRate ;

    /**
     * Constructor for objects of class Hockeyplayer
     */
    public HockeyPlayer(String name, int numberOfGoals)
    {
        super(name);
        overTimePayRate = 0.0;
        this.numberOfGoals = numberOfGoals;
    }

    /**
     * @return     overTimePayRate 
     */
    @Override
    public double getOverTimePayRate()
    {
        return overTimePayRate;
    }
    @Override
    public String   getDressCode()
    {
        return "jersey";
    }
    @Override
    public boolean  isPaidSalary()
    {
        return true;
    }
    @Override
    public boolean  postSecondaryEducationRequired()
    {
        return false;
    }
    @Override
    public String   getWorkVerb()
    {
        return "play";
    }
    @Override
    public boolean equals(Object that)
    {
        if(this == that){
            return true;
        }
        if(!(this instanceof HockeyPlayer)) {
            return false;
        }
        HockeyPlayer h = (HockeyPlayer) that;
        if(this.numberOfGoals == h.numberOfGoals) {
            return true;
        }
        return false;
    }
    @Override
    public int compareTo(Employee that)
    {
        if(this == that) {
            return 0;
        }

        HockeyPlayer h = (HockeyPlayer) that;

        if(this.numberOfGoals > h.numberOfGoals) {
            return +1;
        }
        else {
            return -1;
        }
    }
}

3 Answers 3

7

My guess is that you haven't declared that Employee implements Comparable<Employee>, so public static <T extends Comparable<? super T>> void sort(List<T> list) isn't applicable... but it's hard to say as you haven't posed your Employee class.

That's certainly the error message I get when I try your code with:

class Employee {}

but it compiles when I use:

class Employee implements Comparable<Employee> {

    // Obviously incorrect implementation, only used for demonstrating
    // that the code will now compile.
    public int compareTo(Employee other) {
        return 0;
    }
}

Assuming the various other classes are subclasses of Employee, you'll need to work out how you compare (say) a HockeyPlayer and a Parent. Don't forget that any employee needs to be comparable to any other employee. In my experience inheritance and comparisons (whether for equality or sorting) rarely work cleanly...

If you can post a short but complete program demonstrating the problem, that would really help.

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

4 Comments

As an alternative to implementing Comparable<?>, there is an overloaded sort method that takes a Comparator<?> as a 2nd parameter. Which you choose depends on whether Employee is naturally comparable to others of its kind, or is only so for the purposes of the method you posted.
@Mac: Good point. I like the flexibility of providing a comparator separately. I wish the various hash/equality-based types in Java took something similar, as they do in .NET.
Thanks I updated my post, Jon you were exactly right and now I'm running into problem ClassCastException
@user879181: Yes, you would - because you're assuming that a hockey player is only being compared with a hockey player. You shouldn't assume that, as it's not the case...
1

Your Employee class should implement the interface Comparable<Employee>, not just have all its child classes implement the method compareTo(Employee).

Comments

0

My guess is that ypu have declared that Employee implements comparable, since you used the @Override annotation. Also if you had not done this I thing you would be getting a compile time error, not a run time error. I'm a bit confused here though because your question title says compile error, but the body of your text says you get an exception. Even if your employee class implements comparable, if your subclasses overrides the implementation of compareTo(), you can have problems if you are not careful. When the sort method calls the compareTo() method of your hockeyPlayers with a non-hockeyPlayer parameter, you get the cast exception you are experiencing. I suspect if you drop the @Override annotation and change the signature to "public int compareTo(HockeyPlayer that)", then it will work because it will use the inherited compareTo() when comparing against non-hockeyPlayer employees, and the other one when comparing against hockeyPlayers. Even though a hockeyPlayer is-a Employee, Java will use the most specific of an overloaded method if multiple overloaded methods are applicable. In short, I think you want to overload here, not override.

Also, not what you asked but I thought it worth asking:

if(this == that){
            return true;
        }

Do you really mean to use == here?

Comments

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.