Code Comments
Programming Forum and web based access to our favorite programming groups.Can anyone help?All I want to do the following in one of my program:- Object p=.................;//some object corresponding to my program.But I couldn't do if(p.compareTo(object k)<=0) ................ CompareTo methods is for integer but I want to do it for objects,what should I do to handle that problem??
Post Follow-up to this messageOn Mar 24, 2:49 pm, Andrew Marcus <mainhoonanja...@gmail.com> wrote: > Can anyone help?All I want to do the following in one of my program:- > > Object p=.................;//some object corresponding to my > program.But I couldn't do > > if(p.compareTo(object k)<=0) > ................. > > CompareTo methods is for integer but I want to do it for objects,what > should I do to handle that problem?? You can measure them them in any way where result is int - color, size, time of creation, level of love, distance from hell. Anything is good. Only one thing is required - result should be stable. It's not appropriate if in one comparison one object is bigger them second one and then, in another comparison, they are equal. Alex. http://www.myjavaserver.com/~alexfromohio/
Post Follow-up to this message
Andrew Marcus wrote:
> Can anyone help?All I want to do the following in one of my program:-
>
> Object p=.................;//some object corresponding to my
> program.But I couldn't do
>
> if(p.compareTo(object k)<=0)
> .................
>
> CompareTo methods is for integer but I want to do it for objects,what
> should I do to handle that problem??
class YourObject extends Object {
public YourObject() {
}
public int compareTo(Object that) {
if(this < that)
return -1;
else(this > that)
return 1;
return 0;
}
}
Post Follow-up to this messageAndrew Marcus wrote: > Can anyone help?All I want to do the following in one of my program:- > > Object p=.................;//some object corresponding to my > program.But I couldn't do > > if(p.compareTo(object k)<=0) > ................. > > CompareTo methods is for integer but I want to do it for objects,what > should I do to handle that problem?? You have (at least) three problems here, two technical and one conceptual. The first technical problem is simple: Object does not have a compareTo method. It may well be the case that the object `p' refers to comes from a more specialized class that does in fact implement compareTo, but you haven't let the compiler know about it. By declaring `p' as a reference to plain-vanilla unspecialized Objects, you have told the compiler that only the methods of the Object class itself are accessible through `p'. Solution 1: Declare `p' as a reference to the more specialized class you're actually using. Solution 2: Declare `p' as a reference to Comparable, meaning that it can refer to objects from all classes that implement the Comparable interface (and are thus known to provide a compareTo method). Other solutions are possible. The second technical problem is also simple: `object k' is not valid as an argument list in a method call. If you mean that `k' refers to an Object (note the capitalization, by the way), just write `p.compareTo(k)'. As in the first case, it may make more sense to declare `k' as a reference to something more specific than plain Object. The conceptual problem is harder to cope with, because I'm not sure just where it originates. You say "CompareTo methods is for integer," and it's not clear to me just what you mean by this. Easy things first: The method you're talking about is compareTo, not CompareTo or compareto or comPareto; letter case has meaning in Java. But it's the second part of the claim that puzzles me. I imagine that you might mean - compareTo returns an int value -- true -- and that it is therefore only applicable to integer values. That's not the case! compareTo compares one object to another and uses an int value to report the outcome: a negative, zero, or positive result to indicate that `this' compares "less than," "equal to," or "greater than" the argument. The int value returned from compareTo is just a way of encoding what compareTo discovered internally. It is as arbitrary as "One if by land and two if by sea," and its int-ness implies nothing about the nature of the two objects being compared. You can write a Planet class that compares Earth to Mars on the basis of their mean distance from Sol; the returned value will be a negative int (Earth is closer than Mars), but it does not follow that either Earth or Mars is an int! - compareTo works with Integer objects (note the case) -- also true. The Integer class implements Comparable, and therefore it provides a compareTo method. But Integer is not the only class that has compareTo! So do all the other core numeric classes like Double and Short; so do String and Charset and Calendar and RoundingMode and a host of other classes. You are perfectly free to implement compareTo in your own classes; it is not the exclusive property of Integer. ... or maybe you mean something else I haven't figured out. If so, you can try asking your question again and explaining more about what troubles you -- but I suspect you may have more success if you spend more time reading your Java textbook. -- Eric.Sosman@sun.com
Post Follow-up to this messageAndrew Marcus wrote: > Can anyone help?All I want to do the following in one of my program:- > > Object p=.................;//some object corresponding to my > program.But I couldn't do > > if(p.compareTo(object k)<=0) > ................. > > CompareTo methods is for integer but I want to do it for objects,what > should I do to handle that problem?? Oh, you got such good help in both groups to which you multi-posted. It is somewhat rude to those helpful folks that you multi-posted, though. You should at worst have cross-posted, but really one of the groups would have sufficed. -- Lew
Post Follow-up to this messageOn Mon, 24 Mar 2008 11:49:15 -0700 (PDT), Andrew Marcus <mainhoonanjaane@gmail.com> wrote, quoted or indirectly quoted someone who said : > >CompareTo methods is for integer but I want to do it for objects,what >should I do to handle that problem?? see http://mindprod.com/jgloss/comparable.html -- Roedy Green Canadian Mind Products The Java Glossary http://mindprod.com
Post Follow-up to this messageAll objects that implement the comparible interface have this. I think that compareTo is actualy a protected method of object. http://groups.google.com/group/java...eloupment?hl=en
Post Follow-up to this messageChase Preuninger wrote: > All objects that implement the comparible interface have this. I > think that compareTo is actualy a protected method of object. Why do you think it is a protected method of Object? Unlike clone and finalize, which definitely are protected methods of Object, compareTo does not appear in the Object API documentation. See, for example, http://java.sun.com/j2se/1.5.0/docs...ang/Object.html It does not even appear in the distributed source code for Object. Patricia
Post Follow-up to this messageChase Preuninger wrote: > All objects that implement the comparible interface have this. I > think that compareTo is actualy a protected method of object. A few moments with the Javadoc should correct your thinking. -- Eric Sosman esosman@ieee-dot-org.invalid
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.