Comparable interface

C

Chanchal

hi ,
if i am implelenting the comparable interface, and the parameter
passed is not an object which is comparable to the calling object,
what value shall the function return. Or is there any otherway i
should handle that situation.

Kindly advice,

Thanks in advance..

Chanchal
 
A

Andrew Thompson

Chanchal wrote:
...
if i am implelenting the comparable interface, and the parameter
passed is not an object which is comparable to the calling object,
what value shall the function return. Or is there any otherway i
should handle that situation.

The code might throw an IllegalArgumentException or a ClassCastException.

Some other things you might do to help the *reader*, are to:
- Put a single upper case letter at the start of every sentence.
- Always write the word 'I' as upper case.
- Put a question mark (?) at the the end of questions.
- Capitalise core J2SE classes correctly (e.g. Comparable).

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200704/1
 
C

Chanchal

hello Andrew,

this is the code i have written


public int compareTo(Object obj){
ReportObject rObj;
if(obj instanceof ReportObject ){
rObj = (ReportObject)obj;

if(this.positionNumber < rObj.getPositionNumber()){
return -1;
}else if(this.positionNumber == rObj.getPositionNumber()){
return 0;
}else if(this.positionNumber > rObj.getPositionNumber()){
return 1;
}

}else{
throw new ClassCastException();

}
return -2;
}

in this case if i do not return some thing even in the case of
throwing the exception, i have to retrn some value. otherwise the
class wont compile. i have returned a -2 in that case. but i do not
think that's the correct way of doing it. what shall be done in this
case
 
A

Andrew Thompson

Chanchal wrote:
...
this is the code i have written

Please read my original message again, carefully.
If there is anything that I did not make clear about
helping the reader, feel free to ask.
...
in this case if i do not return some thing even in the case of
throwing the exception, i have to retrn some value. otherwise the
class wont compile.

I do not quite understand what the problem is. Your
code was almost there. Here is an example of code
that compiles, and runs..

<sscce>
class ReportObject implements Comparable {

int positionNumber;

ReportObject(int positionNumber) {
this.positionNumber = positionNumber;
}

public int compareTo(Object obj){
ReportObject rObj;
if(obj instanceof ReportObject ){
rObj = (ReportObject)obj;

if(this.positionNumber < rObj.getPositionNumber()){
return -1;
}else if(this.positionNumber == rObj.getPositionNumber()){
return 0;
}else if(this.positionNumber > rObj.getPositionNumber()){
return 1;
}

}else{
throw new ClassCastException(this +
" is not comparable to " + obj );
}
return -2;
}

public int getPositionNumber() {
return positionNumber;
}

public String toString() {
return "ReportObject: " + getPositionNumber();
}

public static void main(String[] args) {
ReportObject reportObject1 = new ReportObject(1);
ReportObject reportObject2 = new ReportObject(2);
Object object = new Object();

try {
System.out.println( reportObject2.compareTo(reportObject1) );
System.out.println( reportObject1.compareTo(reportObject2) );
System.out.println( reportObject2.compareTo(reportObject2) );
System.out.println( reportObject2.compareTo(object) );
} catch(ClassCastException cce) {
System.out.println( cce.getMessage() );
}
}
}
</sscce>

HTH

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200704/1
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Andrew Thompson schreef:
Chanchal wrote:
..

Please read my original message again, carefully.
If there is anything that I did not make clear about
helping the reader, feel free to ask.
..

public int compareTo(Object obj){
ReportObject rObj;
if(obj instanceof ReportObject ){
rObj = (ReportObject)obj;

if(this.positionNumber < rObj.getPositionNumber()){
return -1;
}else if(this.positionNumber == rObj.getPositionNumber()){
return 0;
}else if(this.positionNumber > rObj.getPositionNumber()){
return 1;
}

}else{
throw new ClassCastException(this +
" is not comparable to " + obj );
}
return -2;
}

Funny, I would have expected a compiler error here about unreachable
code. The last line is unreachable, right?

Ah, no, it isn’t. At least, the compiler cannot know: obj can be an
instance of ReportObject, and the getPositionNumber() method can vary
such that none of the inner conditions fits.

Anyway, why not simply

public int compareTo(Object obj){
ReportObject rObj = (ReportObject)obj;
if(this.positionNumber < rObj.getPositionNumber()){
return -1;
}else if(this.positionNumber == rObj.getPositionNumber()){
return 0;
}else if(this.positionNumber > rObj.getPositionNumber()){
return 1;
}
}

The JVM will throw the ClassCastException, no need to do that yourself.

And oh yes, you might try to venture into generics, in which case the
problem will go away all by itself:

class ReportObject implements Comparable<? super ReportObject> {

....

public int compareTo(ReportObject obj){
if(this.positionNumber < obj.getPositionNumber()){
return -1;
}else if(this.positionNumber == obj.getPositionNumber()){
return 0;
}else if(this.positionNumber > obj.getPositionNumber()){
return 1;
}
}

....

}

H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGLKswe+7xMGD3itQRAlkEAJ9toexDQy7jxIoKcEZ9Klz8F7QtewCeMbwT
T0zzFTarIAEWEokIjgPF3Ds=
=DlXb
-----END PGP SIGNATURE-----
 
D

Daniel Pitts

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Andrew Thompson schreef:








Funny, I would have expected a compiler error here about unreachable
code. The last line is unreachable, right?

Ah, no, it isn't. At least, the compiler cannot know: obj can be an
instance of ReportObject, and the getPositionNumber() method can vary
such that none of the inner conditions fits.

Anyway, why not simply

public int compareTo(Object obj){
ReportObject rObj = (ReportObject)obj;
if(this.positionNumber < rObj.getPositionNumber()){
return -1;
}else if(this.positionNumber == rObj.getPositionNumber()){
return 0;
}else if(this.positionNumber > rObj.getPositionNumber()){
return 1;
}

}

The JVM will throw the ClassCastException, no need to do that yourself.

And oh yes, you might try to venture into generics, in which case the
problem will go away all by itself:

class ReportObject implements Comparable<? super ReportObject> {

...

public int compareTo(ReportObject obj){
if(this.positionNumber < obj.getPositionNumber()){
return -1;
}else if(this.positionNumber == obj.getPositionNumber()){
return 0;
}else if(this.positionNumber > obj.getPositionNumber()){
return 1;
}

}

...

}

You might ALSO venture into using some basic logic.

if (a < b) {
return -1;
} else if (a > b) {
return 1;
}
return 0;


it is highly (100%) likely that if (!(a < b) && !(a > b)), then a ==
b. At least with primitive data types in Java.
 
D

Daniel Pitts

You might ALSO venture into using some basic logic.

if (a < b) {
return -1;} else if (a > b) {
return 1;
}

return 0;

it is highly (100%) likely that if (!(a < b) && !(a > b)), then a ==
b. At least with primitive data types in Java.

Actually, I think I'm wrong.
NaN isn't == NaN, is it?
In any case, comparable should throw an exception in that case too, as
you can't order NaN.
Actually, if you are using floating point values, you can use
Double.compareTo or Float.compareTo.

Indeed, I would probably do this:

public int compareTo(ReportObject obj) {
return Integer.compareTo(this.positionNumber,
obj.getPositionNumber());
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top