Solved

How do you implement a Comparable interface into 2 subclasses?

Posted on 2006-11-29
7
878 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
7 Comments
 
LVL 10

Accepted Solution

by:
ADSLMark earned 410 total points
Comment Utility
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 30 total points
Comment Utility
You implement no Comparable interface, but use 2 different implementations of Comparator<Dog>
0
 

Author Comment

by:alexthecodepoet
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 5

Assisted Solution

by:gkishoreji
gkishoreji earned 30 total points
Comment Utility
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 30 total points
Comment Utility
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
Comment Utility
I mean what Ajay-Singh explained above - use the comparators when building the trees.
0
 

Author Comment

by:alexthecodepoet
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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 will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

772 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

12 Experts available now in Live!

Get 1:1 Help Now