1 Replies - 88 Views - Last Post: 07 February 2012 - 08:45 PM Rate Topic: -----

Topic Sponsor:

#1 green4g63  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 07-February 12

comparing user created objects problem.

Posted 07 February 2012 - 08:29 PM

hello,

For a project I am working on im creating two objects of type CelestialBody. these objects are given values such as name,mass,and id. The issue i am having is when i have to create a getLargestBody(); method, I am not sure on how to compare my two created objects masses. I know how to do this with an array but spec will not allow it. I believe that when called the first object should be both the largest and smallest object then the following objects are compared to what ever one is the largest.


this is the building template for the object

 public CelestialBody(String name, double mass, boolean isOrbiting,this){
	this.name = name;
	this.mass = mass;
	this.isOrbiting = isOrbiting;
	this.largestBody = largestBody;
	totalBodies++;



the get method for largest body
    public CelestialBody getLargestBody(){
	return largestBody;
    }



im just not sure if i need to add an "if" statement within my get method or how to compare the masses of the objects

Is This A Good Question/Topic? 0
  • +

Replies To: comparing user created objects problem.

#2 illuss  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 49
  • Joined: 14-April 11

Re: comparing user created objects problem.

Posted 07 February 2012 - 08:45 PM

you can create a boolean method, call it compareMasses for example, that takes two parameters:
 
public boolean compareMasses(double MassObject1, double MassObject2) {
if(MassObject1 > MassObject2) {
return 1;
} else if(MassObject1 < MassObject2) {
return -1;
}else {
return 0;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1