Solved

How do you implement the Comparable interface inside of a custom class?

Posted on 2006-11-28
5
228 Views
Last Modified: 2010-03-31
Hello,

Thanks for taking the time to help.  Allow me to explain the situation:

I have a "dog" class.  Somehow, I need to create a subclass that extends the java's "Comparable" class so that "dog"s can be compared.  

The reason I need to do this is because I have a tree that is going to be built up of dogs.  However, the tree's "find" feature needs to be able to access the Comparable "something (i dont know what you would call this)" in order to traverse the tree.

So, can someone please explain to me how I would go about implementing the Comparable "(something, i think you cal it a subclass?)" into my "dog" class so that I can achieve my desired outcome?


Thanks for the help,

an aspiring java learner
0
Comment
Question by:alexthecodepoet
  • 2
  • 2
5 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 250 total points
ID: 18034963
What aspect(s) of dog are you comparing on. The name? If so

public int compareTo(Object other) {
    return name.compareTo((Dog)other).name);
}
0
 
LVL 23

Accepted Solution

by:
Ajay-Singh earned 250 total points
ID: 18034968
public class Dog implements Comparable<Dog> {
    private String m_name;

    public int compareTo(Dog o) {
        return m_name.compareTo(o.m_name); // assuming that you want to compare based on name        
    }
}
0
 

Author Comment

by:alexthecodepoet
ID: 18035051
Awesome, these are perfect!!

Thanks guys, especially for your timeliness.  

I appreciate the help very very much!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 18035080
:-)
0
 

Author Comment

by:alexthecodepoet
ID: 18040573
Hi Guys,

I posted a followup question, perhaps you can help?

Thanks

http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_22076950.html
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now