Java

Moderators: zibadian
Number of threads: 7818
Number of posts: 18218

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
compareTo help Posted by vstar on 19 Mar 2011 at 8:22 AM
I am new to java and need help with CompareTo. I've created a class of object called planet, each planet has 4 variables: Distance from sun, radius, mass, and number of moons.

How do I make it so that when I say Jupiter.compareTo(Saturn) It tells me which one has the larger radius, as opposed to comparing any of the other variables.


Thanks for any help.
Report
Re: compareTo help Posted by silveredge52 on 23 Mar 2011 at 11:37 AM
Hey,
You write a compareTo() method in the planet class. That method has an argument of a planet object. That method will compare the variable this.radius with the method argument's radius and return a value, often an int, that describes the relationship.
Typical return values:
The value 0 if this Planet's radius is equal to the argument Planet's radius; a value less than 0 if this Planet's radius is numerically less than the argument Planet's radius; and a value greater than 0 if this Planet's radius is numerically greater than the argument Planet's radius.

regards, se52
Report
Re: compareTo help Posted by vstar on 27 Mar 2011 at 5:25 PM
Thank you.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.