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

MrTimotheos
  • 937
  • 1
  • 10
  • 18
  • Note: in an interface, the modifiers `public` and `abstract` are implied, no need to specify them – fge Mar 17 '14 at 22:47
  • write that the class implements Comparable – La-comadreja Mar 17 '14 at 22:48
  • I get this error warning when I do that. "The type Comparable is not generic; it cannot be parameterized with arguments " – MrTimotheos Mar 17 '14 at 22:49
  • 1
    [`Comparable`](http://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html) 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. – Bohemian Mar 17 '14 at 22:56
  • Seems to be a remarkably precise duplicate of http://stackoverflow.com/a/21659849 – Marco13 Mar 17 '14 at 23:14

2 Answers2

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()

libik
  • 22,239
  • 9
  • 44
  • 87
  • Ah yeah I see that now. I'm not sure what to change that to though. – MrTimotheos Mar 17 '14 at 22:50
  • 1
    Uh, just `Integer.compare(age, e.age) is enough; no need to cast, no need to use setters – fge Mar 17 '14 at 22:51
  • @MrTimotheos - probably think it through. Why you compare employee age in department instance? It does not make sense :) – libik Mar 17 '14 at 22:53
  • 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 – MrTimotheos Mar 17 '14 at 22:54
  • @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. – MrTimotheos Mar 17 '14 at 22:54
  • @MrTimotheos - So you want sorted employees separately for each department, right? – libik Mar 17 '14 at 22:55
  • Well if they are in a particular department, yes. Only Information Systems and Accounting. The others I don't need to sort. – MrTimotheos Mar 17 '14 at 22:56
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();
La-comadreja
  • 5,627
  • 11
  • 36
  • 64
  • it would probably not work, because class Department does not have age (or at least it should not have one :D ) – libik Mar 17 '14 at 22:50
  • 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. – MrTimotheos Mar 17 '14 at 22:51
  • @libik fixed as per your comment. – La-comadreja Mar 17 '14 at 22:54
  • @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 – MrTimotheos Mar 17 '14 at 22:56
  • yes, so you should call the compareTo() method in class Employee from class Department. That's what I think they mean. – La-comadreja Mar 17 '14 at 22:56