Solved

How do you implement a Comparable interface into 2 subclasses?

Posted on 2006-11-29
7
881 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
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 30 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 5

Assisted Solution

by:gkishoreji
gkishoreji earned 30 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 30 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
allswap challenge 6 96
Python Assistance 7 80
Java - Why doesn't this JFrame work 3 58
web services creation SOAP vs REST 5 38
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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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…

785 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