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

}
jorgeAChacon
  • 319
  • 2
  • 5
  • 21
  • 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. – Ashish Patil Jun 14 '13 at 20:13
  • http://docs.oracle.com/javase/tutorial/collections/interfaces/order.html – Alexis C. Jun 14 '13 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. – Jim Garrison Jun 14 '13 at 20:18

3 Answers3

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

Community
  • 1
  • 1
Alex Brooks
  • 1,151
  • 1
  • 10
  • 39
  • 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. – jorgeAChacon Jun 14 '13 at 20:17
  • @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. – Alex Brooks Jun 14 '13 at 20:20
  • @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 – Alex Brooks Jun 14 '13 at 20:23
  • @AlexBrooks that means the `Employee` class must have to implement `Comparable` interface, which is not the purpose of the exercise, thus not a viable option for this problem. – Luiggi Mendoza Jun 14 '13 at 20:23
  • @LuiggiMendoza edited to provide an alternative option that doesn't require modifying the Employee class :) – Alex Brooks Jun 14 '13 at 20:30
  • Ok, I understand the return part it uno>dos it will return a positive number, if uno – jorgeAChacon Jun 14 '13 at 20:34
  • @user1166061 - `stringOne.compareTo(stringTwo)` returns an int. Plese read the [documentation for String](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html). If you want to compare the numeric values of the Strings, you will have to parse them first. – jahroy Jun 14 '13 at 20:36
  • @user1166061 you can simply use `String#compareTo(String)` but there are rules in your `Comparator` that establishes how they should be compared (assuming the comments are from your teacher and not from you). Based on this, your `Employee` class lacks the `category` field that is the first key to determine if the `Employee` is greater, equal or less than another `Employee` and by applying this rule you must not use `String#compareTo` but `String#equals`. – Luiggi Mendoza Jun 14 '13 at 20:36
  • 2
    @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. – Alex Brooks Jun 14 '13 at 20:37
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.

jahroy
  • 22,322
  • 9
  • 59
  • 108
-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.

Sebastian Hoffmann
  • 11,127
  • 7
  • 49
  • 77
  • 2
    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: http://stackoverflow.com/questions/12331656/phone-numbers-string-or-int – chessbot Jun 14 '13 at 20:18
  • 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. – jahroy Jun 14 '13 at 20:20
  • @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... – Sebastian Hoffmann Jun 14 '13 at 20:24
  • 1
    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. – jahroy Jun 14 '13 at 20:26
  • "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 -.- – Sebastian Hoffmann Jun 14 '13 at 20:29
  • But the result is something completely different than comparing the actual values the strings represent – Sebastian Hoffmann Jun 14 '13 at 20:31
  • Still, this answer doesn't address that either (in fact, none of the answers here provide that info). – Luiggi Mendoza Jun 14 '13 at 20:34