• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Current Object confusion

 
Roscoe Jb
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1807
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And Welcome to JavaRanch!
 
Roscoe Jb
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ... ...
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show us what your Polynomial class constructors look like?

edit: and what is MyDouble?
 
Roscoe Jb
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the polynomial class (though much work needed)




And this is mydouble:

 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic