0

I was given the code below as Homework. I am being asked to implement comparator for employee objects. The compare method returns an int. However, none of the methods that I have in my employee class return an int if you compare them. Can anyone give me some guidance on how the compare method is supposed to work? Thank you

import java.util.*;
import java.io.*;
public class EmployeeClient
{
//A Comparator for Employees
// Primary key   : Employee category - Salaried > Weekly > Daily
// Secondary key : Employee gross pay
private static class EmployeeComparator implements Comparator
{
  public int compare(Object one, Object two)
  {   
      Employee uno = (Employee) one;
      Employee dos = (Employee) two;

  }
}

public abstract class Employee {

private String idNumber;
private double payRate;

//Accessor: Return the id number of employee
public String getidNumber()
{
    return idNumber;
}

//Accessor: Return the payrate of the employee
public double getpayRate()
{
    return payRate;
}

public String toString()
{
    return getidNumber()+" "+getpayRate();
}

public abstract double grossPay();

}
3
  • There is no method/field for Employee category. Then you can just compare Employee gross pay and return -1, 0 or 1 based on comparing pay value. Commented Jun 14, 2013 at 20:13
  • docs.oracle.com/javase/tutorial/collections/interfaces/… Commented Jun 14, 2013 at 20:14
  • 1
    What the comparator returns is an int so that ordering (less-than, equal, greater-than) can be established between two object instances. The crucial attribute that is given in the comments for the Comparator is what properties of Employee determine the ordering relationship? You are told to order on employee category first and then gross pay, but Employee has no "category" attribute so the problem is incompletely specified. Commented Jun 14, 2013 at 20:18

3 Answers 3

3

I would suggest checking out this link.

The goal of a custom comparator is to take two objects and determine how they relate to one another. Consider it an ordering problem. Say I have two bags with red and blue marbles in them. If I want to do a comparison of the bags in terms of blue marbles, it is the same as asking which bag has more/less blue marbles in it.

The return value of the compare function determines which object is valued higher than the other. let ret be the value after comparing the two objects.

  • ret > 0 means the left object is valued higher than the right
  • ret < 0 means the right object is valued higher than the left
  • ret == 0 means the two objects are valued the same

Hope this helps.

Edit:

In the case you are not allowed or unable to modify the Employee class, do the comparisons in your compare function in your first set of code. For example, if you wanted to compare two integers int a and int b, you would do:

a.compareTo(b)

or

Integer.compare(a, b)

  • If a > b : either method would return a value greater than 0
  • If a < b : either method would return a value less than 0
  • If a == b : either method would return 0

Simply apply this idea to your code

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

9 Comments

I was going to use int comparison =uno.compareTo(dos); and return comparison,but my employee class does not have a compareTo method and I do not think I can add it.
@user1166061 That is a perfectly viable option. It will require you to write a compareTo() function in your Employee class. You will need to think about how you want to 'order' your employees (i.e. by ID number or by payrate). Also think about what happens when the ID number is the same.
@user1166061 If you are not allowed to make a compareTo() function in Employee, then you can do the comparison in the compare function in your first set of code. Just use your get functions defined in Employee to determine how to order the Employees
@AlexBrooks that means the Employee class must have to implement Comparable<Employee> interface, which is not the purpose of the exercise, thus not a viable option for this problem.
@user1166061 It's the same thing for strings as presented for integers. The String object has a compare() and compareTo() that works in the same way. The result regards lexicographical order of the string. But as Luiggi pointed out, it seems your focus should be on the salary and how your teacher wants you to manipulate it to rank the Employees.
|
0

Actually, all of the fields in your Employee class return ints when compared.

Also, each Employee method returns an object that can be compared using compareTo() to return an int.

Your Employee class has two fields:

String idNumber;
double payRate;

Both String and Double (the object form of double) have a compareTo() method that returns an int.

So... Your EmployeeComparator.compare() method could simply return:

one.getidNumber().compareTo(two.getidNumber());

Here's how you could rewrite your Comparator class:

private static class EmployeeComparator implements Comparator<Employee>
{
  public int compare(Employee empOne, Employee empTwo)
  {   
      return empOne.getidNumber().compareTo(empTwo.getidNumber());
  }
}

The comparator above bases comparison strictlly on employee id number.

But... It should be obvious how to modify it to consider other fields.

Comments

-2

Well it always depends on what property you want to sort. For generic entities like these I would suggest its internal id. BUT: The Employee class is actually poorly designed. getIdNumber() should return an int not a String. If you can redesign the class, do it! If not parse the string using Integer.parseInt(), then youll have an int for comparsion.

7 Comments

Your point about String is not necessarily true. The employee ID might happen to be numeric, but you're not going to be doing any numeric operations on it (most likely) as the number is probably intended purely as a key for employee lookup and not for arithmetic. Similar discussion for phone numbers: stackoverflow.com/questions/12331656/…
How on earth do you know the requirements for Employee.idNumber? Where does anybody say it has to be a number? Speculating on the design has no place here... It has nothing to do with writing a comparator.
@chessbot Commonly ints are used for ids. There are cases where strings are useful, these cases are as you already mentioned more complex keys like phone numbers or GUIDs. But for 90% of the use cases ints are just fine. As the property is named id NUMBER its logical to assume that its numeric...
It seems ridiculous to critique his design based on assumptions about the property name or "what happens 90% of the time". Plus this question has nothing to do with how to implement an employee ID number. It's about comparators. I find it perfectly reasonable that an employee ID could contain a letter.
"However, none of the methods that I have in my employee class return an int if you compare them" No its not? I think the OP knows how to subtract ints, but is confused about what to compare. I still consider my answere legit in the context of the question -.-
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.