?
Solved

How do you implement a Comparable interface into 2 subclasses?

Posted on 2006-11-29
7
Medium Priority
?
900 Views
Last Modified: 2008-01-09
Hello,

This is a continuation of:
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_22075902.html#a18034968


I asked how you implement the Comparable interface into a class.  The answer that I received was correct, but now i have a new (similar) question.

You see, i am building a tree data structure containing "dogs".  So, i needed to incorporate a comparable "function" into my "dog" class.  

The problem is that, i am not building only one tree structure, i am making 2.

One of my trees is a red black tree that should be built completely off of the dog's name, and the answer in the other question addressed this.

However, my other data structure is built by using a different "Comparable" feature of "dog" -- that is, I need to be able to compare the dogCOUNT.



So, basically what I am asking is: say i have a class, called Dog.  How do i go about implementing a Comparable subclass that will compare based off names, if specified to do so, or based off of dogCOUNT, if specified to do so.  

I assume that the method signatures would have to be different, but I dont know how to do this.

Thanks,


0
Comment
Question by:alexthecodepoet
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 10

Accepted Solution

by:
ADSLMark earned 1640 total points
ID: 18040692
How about this approach:

You have a superclass Dog and two subclasses DogByName, DogByCount, which both extend from the Dog class. You implement the Comparable interface for both the subclasses and all common functionality is implemented in the Dog class. You keep your tree structure of Dogs, but when you add objects you should add either DogByName or DogByCount instances. Dynamically Java will determine the correct class and use the appropriate compare function.

Mark
0
 
LVL 12

Assisted Solution

by:enachemc
enachemc earned 120 total points
ID: 18040700
You implement no Comparable interface, but use 2 different implementations of Comparator<Dog>
0
 

Author Comment

by:alexthecodepoet
ID: 18042373
Hi Mark,

I guess i should have been more clear... dogs have: name and count: variables --so creating 2 seperate classes isn't really an option.


And enachmc, what do you mean?

Thanks,

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Assisted Solution

by:gkishoreji
gkishoreji earned 120 total points
ID: 18042647
while initializing ur comparator class, u pass some value to the constructor to indicate whether to sort based on count or name...
depending on the initialized value from the comparator, do the comparison.
0
 
LVL 23

Assisted Solution

by:Ajay-Singh
Ajay-Singh earned 120 total points
ID: 18042840
Use java.util.Comparator interface


public class NameComparator implements Comparator<Dog> {
    public int compare(Dog x, Dog y) {
        return x.getName().compareTo(y.getName());
    }
}


public class CountComparator implements Comparator<Dog> {
    public int compare(Dog x, Dog y) {
        return x.getCount() - y.getCount();
    }
}
0
 
LVL 12

Expert Comment

by:enachemc
ID: 18043224
I mean what Ajay-Singh explained above - use the comparators when building the trees.
0
 

Author Comment

by:alexthecodepoet
ID: 18043380
Hi Guys (and gals?)

Thanks for all of the help.  It turns out that Mark's solution actually did the trick.  But, i appreciate all of the other feedback.  Ajay-Singh, i think that it is awesome that you even went to the extent of pasting code. Mark's solution just hit a little bit closer to the bullseye...

much thanks to everyone though
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses
Course of the Month12 days, 4 hours left to enroll

752 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