Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Implementing the compareTo method?

  1. #1
    Member
    Join Date
    Feb 2011
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Implementing the compareTo method?

    I have an assignment that's asking me to create a method which "Implements the compareTo method provided by the Comparable interface. Allows two objects to be compared based on sortKey(sortKey is just an int value 1,2,or 3)." What exactly is this asking me to do? Am I supposed to use "implements Comparable" in the class? If so, what functionality does this implemented method provide for me to help me sort objects such as strings in alphabetical order?

    I hope someone can help.

    Thanks


  2. #2
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Implementing the compareTo method?

    I am not 100% sure on this, but yes I believe you have to add "implements Comparable" to the class name.

    What this does is requires your class to have the method "compareTo". If you say your class implements Comparable and then don't add a compareTo method, you will encounter an error.

    What I am not sure of is whether you have to override the method by adding your own code to compare the objects, or if the method will automatically compare the objects for you. Now that I think about it a bit more, I believe you have to write you own code to compare the two objects. You may want to take a look at this, it will be helpful (hopefully).

    Hopefully this helps1
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,662
    Thanks
    45
    Thanked 1,960 Times in 1,934 Posts

    Default Re: Implementing the compareTo method?

    Have you read the API doc for the Comparable interface? Most of your questions are answered there.
    Go to this site and Find Comparable in lower left, click on link for doc in main window.
    Java Platform SE 6

Similar Threads

  1. Help Me with my Program CompareTo!!!!
    By WantHelp in forum What's Wrong With My Code?
    Replies: 14
    Last Post: July 7th, 2011, 12:54 PM
  2. Replies: 3
    Last Post: June 1st, 2011, 12:47 AM
  3. Implementing RSS Feeds
    By madhu_sushmi in forum Java Networking
    Replies: 0
    Last Post: April 1st, 2010, 07:57 PM
  4. Implementing Semaphores
    By bananasplitkids in forum Threads
    Replies: 1
    Last Post: March 27th, 2010, 04:35 AM
  5. Replies: 6
    Last Post: October 20th, 2009, 06:33 AM