Results 1 to 7 of 7
  1. #1
    Navatha is offline Member
    Join Date
    Sep 2010
    Posts
    11
    Rep Power
    0

    Default java.util.Comparator Implementation

    Hi all ,
    Can you please help me understand the following question.I have been attending the interviews .I have been asked this in one of the interview.Thanks in advance.
    Assume the following code snippet exists inside a java.util.Comparator implementation.
    Fill in the method.

    /**
    * Compares o1 and o2.
    * @return -1 if o1's first name, middle name are lexically less
    * than o2's first name, middle name, 0 if o1's first name
    * and middle name are lexically the same as o2's,
    * and 1 otherwise.
    */
    public int compare(Object o1, Object o2) {
    // Write your code here.
    }

    Override the following methods for an implementation of Person:



    int java.lang.Object.hashCode()
    boolean java.lang.Object.equals(Object o)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You seem to be missing some things here including:

    • The declarations for class fields of Person,
    • Code tags (please see my signature link below) and
    • Your attempt at a solution first.


    Much luck!

  3. #3
    Navatha is offline Member
    Join Date
    Sep 2010
    Posts
    11
    Rep Power
    0

    Default

    Hi Sorry,i didn't post the first part of the question here it is
    Object Implementation
    Implement a comparator for the Person class that can be used to sort by first name and middle name for the following Interface.

    public Interface Person {
    String getFirstName();
    String getMiddeName();
    String getLastName();
    Date getBirthDate();
    Person[] getParents();
    Person[] getChildren();
    // etc.
    }

  4. #4
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by Navatha View Post
    Hi Sorry,i didn't post the first part of the question here it is
    Object Implementation
    • Implement a comparator for the Person class that can be used to sort by first name and middle name for the following Interface.

    public Interface Person {
    String getFirstName();
    String getMiddeName();
    String getLastName();
    Date getBirthDate();
    Person[] getParents();
    Person[] getChildren();
    // etc.
    }
    That helps, but...

    Quote Originally Posted by Fubarable View Post
    You seem to be missing some things here including:

    • The declarations for class fields of Person,
    • Code tags (please see my signature link below) and
    • Your attempt at a solution first.
    You seem to have also forgotten to post your attempt at a solution?

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    They're a bit out of date as well...that's a Java 1.4 Comparator!

  6. #6
    ilu341 is offline Member
    Join Date
    Nov 2010
    Posts
    1
    Rep Power
    0

    Default

    @Navatha or others found the answer, if so please post or email me [mod edit: deleted]
    Last edited by Fubarable; 11-05-2010 at 02:47 AM. Reason: moderator edit: email address deleted

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by ilu341 View Post
    @Navatha or others found the answer, if so please post or email me [mod edit: deleted]
    ilu341, please do not post your email address or request info sent by email. That's against forum rules. If you have a question, please start your own thread and ask it. You may link to this thread if desired.

Similar Threads

  1. how to use comparator
    By KidneyinaCooler in forum Advanced Java
    Replies: 2
    Last Post: 07-18-2010, 11:25 AM
  2. implements java.util.Comparator<type>
    By finn in forum New To Java
    Replies: 2
    Last Post: 05-14-2010, 07:58 PM
  3. Use different comparator for SortedSet
    By linus_k in forum New To Java
    Replies: 0
    Last Post: 11-21-2008, 03:46 PM
  4. How to search with a Comparator
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-15-2008, 08:39 PM
  5. java.util
    By Java Tutorial in forum Java Tutorial
    Replies: 1
    Last Post: 02-07-2008, 02:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •