Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

CompareTo for Objects
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??

Report this thread to moderator Post Follow-up to this message
Old Post
Andrew Marcus
03-25-08 12:35 AM


Re: CompareTo for Objects
On 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/

Report this thread to moderator Post Follow-up to this message
Old Post
Alex.From.Ohio.Java@gmail.com
03-25-08 12:35 AM


Re: CompareTo for Objects
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;

}

}



Report this thread to moderator Post Follow-up to this message
Old Post
Jeff Higgins
03-25-08 12:35 AM


Re: CompareTo for Objects
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??

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

Report this thread to moderator Post Follow-up to this message
Old Post
Eric Sosman
03-25-08 12:35 AM


Re: CompareTo for Objects
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??

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

Report this thread to moderator Post Follow-up to this message
Old Post
Lew
03-25-08 03:33 AM


Re: CompareTo for Objects
On 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

Report this thread to moderator Post Follow-up to this message
Old Post
Roedy Green
03-25-08 09:59 AM


Re: CompareTo for Objects
All 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

Report this thread to moderator Post Follow-up to this message
Old Post
Chase Preuninger
03-31-08 03:07 AM


Re: CompareTo for Objects
Chase 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

Report this thread to moderator Post Follow-up to this message
Old Post
Patricia Shanahan
03-31-08 03:07 AM


Re: CompareTo for Objects
Chase 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

Report this thread to moderator Post Follow-up to this message
Old Post
Eric Sosman
03-31-08 03:08 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Java Help archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 08:46 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.