aspose file tools*
The moose likes Beginning Java and the fly likes Current Object confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Current Object confusion" Watch "Current Object confusion" New topic
Author

Current Object confusion

Roscoe Jb
Greenhorn

Joined: Mar 27, 2010
Posts: 4
Hello all, first time poster and slowly but surely learning java. i have a question for a program im trying to create. im told that one of the methods should be:

this method takes one parameter (a Polynomial). It will return a new Polynomial that is equal to the sum of the current object and the parameter. (Do not modify the current object.)

here is what i have



and....im stuck. i dont understand, the current object? what would be considered the current object? Thank you all
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
The current object is the one that's calling the method including all of its fields and such. So assuming your class has an int field called intField, you could add two like so:

Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1732
    
  12

And Welcome to JavaRanch!


When you do things right, people won't be sure you've done anything at all.
Roscoe Jb
Greenhorn

Joined: Mar 27, 2010
Posts: 4
pete stein wrote:The current object is the one that's calling the method including all of its fields and such. So assuming your class has an int field called intField, you could add two like so:



and if i had 3 current objects? (if im reading what you told me correctly)



Janeice DelVecchio wrote:And Welcome to JavaRanch!


thank you ive heard good things about this ranch, glad i came here
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
It may be helpful to take what Pete said above and add a bit more context.Your program would then create two instances of MyClass, one for your current
object, call it 'co', and the second called 'pin' that you want to add to 'co' (pass
in to co's add() method). This would look like lines 09-11 above, but you have
to put the main program around it, of course.

Jim ... ...


BEE MBA PMP SCJP-6
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Roscoe Jenkins wrote:
and if i had 3 current objects? (if im reading what you told me correctly)




Sorry no, I must not have communicated this well. You can only have one current object at any one time, but here that object has 3 fields.
Md Ibrahim
Greenhorn

Joined: Mar 24, 2010
Posts: 14
hi Roscoe,

First of all understand that Object is a behavioral representation of a Class at runtime,

Eg:

Assume You have written a Class name Vehicle.Java and an attribute say Speed

public Class Vehicle {

private int Speed;

public getSpeed() {

this.speed
}

public setSpeed(Speed pSpeed){
this.speed =pSpeed
}

}
This is the physical Calss which you have written and eligible to compile.

But Creating Objects out of it happens at runtime,

Vehicle aCar = new Vehicle();
aCar.setSpeed(100)

Vehicle aByke = new Vehcle();
aByke.setSpeed(50);

Now aCar and aByke will have their own speed attribute values at runtime since they are
Objects at runtime. They only stay till the Objects Live.

So now ask yourself this question of How it makes sense by telling we can add two objects
like below.
In the below code this keyword refers to current object and pVehicle the passed in Parameter Object of same type.

# public Vehicle add(Vehicle pVehicle) {
# return new (this + pVehicle) ; // And what in the world this means to be?? :-)
# }


we cant say aCar + aByke.(adding two objects)


But Yes we can add Primitive dataType objects as given an example by pete stein

Do write back if you are still in cofusion to differentiate between object and a Class

By,
Ibrahim




Roscoe Jb
Greenhorn

Joined: Mar 27, 2010
Posts: 4
Wow all these replies! Thank you all!! this is what i am left with right now:



i believe this is what the requirement for the method is referring to, add the a, b, c of the current object with the a, b, c of the polynomial passed through. i believe this is correct?

I am also supposed to input a junit test with this, though i am completely confused how to do it with this method. ive done it with others, just not this one. im not exactly sure how to even test a method like this, assuming its done correctly. any ideas please? Thanks again
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Can you show us what your Polynomial class constructors look like?

edit: and what is MyDouble?
Roscoe Jb
Greenhorn

Joined: Mar 27, 2010
Posts: 4
This is the polynomial class (though much work needed)




And this is mydouble:

pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Roscoe Jb wrote:This is the polynomial class (though much work needed)



As I thought. Wouldn't you rather use this constructor above that takes three MyDouble objects as parameters rather than your current attempt where you combine the new a, b, and c all into one MyDouble object? Which makes more sense?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Current Object confusion