This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
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
posted
0
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:
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
posted
0
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
posted
0
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
posted
0
Roscoe [surl='http://jenkins-ci.org/' class='api' title='Continuous Integration Engine wrote:Jenkins[/surl]]
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
posted
0
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
posted
0
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 junittest 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
posted
0
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
posted
0
This is the polynomial class (though much work needed)
And this is mydouble:
pete stein
Bartender
Joined: Feb 23, 2007
Posts: 1561
posted
0
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?