0

I am currently working on an assignment and I am almost done except for one part.

I need to sort some of the objects in the array based on whether they are in Accounting or Information Systems. I setup the compareTo method but I do not believe I did it right or else I just don't know how to call on it as I did and it is not doing what I thought it would do.

This is driving me nuts as it is the last thing I have to do and I can't figure it out, any help would be great.

Here is the Comparable Inteface

public interface Comparable {


public abstract int compareTo(Employee c);


}//end comparable

Here is my code in the Department class:

public class Department implements Comparable  {

public int compareTo(Employee e){
    int compareAge = ((Employee) e).getAge();

    return e.getAge() - compareAge;
}//end


}//end department

Here is my code for the main method:

public class Company {


public static void main(String [] args){

    Employee[] e = new Employee[13];
    PrimeAgeChecker p = new PrimeAgeChecker();
    Department d = new Department();

    e[0] = new Employee("Counting Guru",55,"Accounting");
    e[1] = new Employee("Counting Pro",45,"Accounting");
    e[2] = new Employee("Counting Savvy",40,"Accounting");
    e[3] = new Employee("Counting Novice",25,"Accounting");
    e[4] = new Employee("Sales Guru",50,"Marketing");
    e[5] = new Employee("Sales Pro",48,"Marketing");
    e[6] = new Employee("Sales Savvy",38,"Marketing");
    e[7] = new Employee("Hiring Guru",58,"Human Resrouces");
    e[8] = new Employee("Hiring Pro",47,"Human Resrouces");
    e[9] = new Employee("Hacking Pro",47,"Information Systems");
    e[10] = new Employee("Hacking Guru",51,"Information Systems");
    e[11] = new Employee("Hacking Savvy",38,"Information Systems");
    e[12] = new Employee("Hacking Novice",23,"Information Systems");


    for(int i = 0;i<e.length;i++){
        if(e[i].getDept().equals("Accounting") || e[i].getDept().equals("Information Systems")){
            d.compareTo(e[i]);
            System.out.println(e[i]);
        }//end if
        else{
        System.out.println(e[i] + " " + p.isPrime(e[i]));
        }

    }//end 





}//end main
}//end company

P.S. Sorting the ages of Employees code HAS to go into the Department Class as per the assignment instructions

5
  • Note: in an interface, the modifiers public and abstract are implied, no need to specify them Commented Mar 17, 2014 at 22:47
  • write that the class implements Comparable<Employee> Commented Mar 17, 2014 at 22:48
  • I get this error warning when I do that. "The type Comparable is not generic; it cannot be parameterized with arguments <Employee>" Commented Mar 17, 2014 at 22:49
  • 1
    Comparable is an interface from the java.lang package. Defining your own Comparable class is a very bad idea - it will only lead to confusion for anyone (including you) reading your code. Name it something else, or use the java.lang.Comparable interface instead. Commented Mar 17, 2014 at 22:56
  • Seems to be a remarkably precise duplicate of stackoverflow.com/a/21659849 Commented Mar 17, 2014 at 23:14

2 Answers 2

1

There it is wrong :

int compareAge = ((Employee) e).getAge();
return e.getAge() - compareAge;

If you look closely, this always returns 0, because this ((Employee) e).getAge(); is same as e.getAge()

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

7 Comments

Ah yeah I see that now. I'm not sure what to change that to though.
Uh, just `Integer.compare(age, e.age) is enough; no need to cast, no need to use setters
@MrTimotheos - probably think it through. Why you compare employee age in department instance? It does not make sense :)
The only problem is that here is not an age variable in the Department class yet the assignment wants me to write a method that sorts the ages in Department class
@libik I understand but that's what the assignment wants me to do and It's making my head swirl. I have no idea how to approach this problem.
|
1

Firstly, you should implement compareTo() in class Employee, not in Department.

In class Employee, here is how the method should read:

if (this.getDept().compareTo(other.getDept()) != 0) 
    return this.getDept().compareTo(other.getDept());
return this.getAge() - e.getAge();

5 Comments

it would probably not work, because class Department does not have age (or at least it should not have one :D )
Yeah it won't. that's the tricky part. How do I compare two employee's ages and then sort them in ascending order? It's a puzzle, I tell ya.
@libik fixed as per your comment.
@La-comadreja I appreciate that, as that would be the simple solution. However my assignment explicitly states to sort the Employees by their age in the Department class
yes, so you should call the compareTo() method in class Employee from class Department. That's what I think they mean.

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.