Results 1 to 7 of 7
Thread: Java Question :D
 03122010, 04:29 PM #1Member
 Join Date
 Mar 2010
 Posts
 8
 Rep Power
 0
Java Question :D
// method in adding Polynomials that will return c = a + b
public Polynomial add(Polynomial b) {
Polynomial a = this;
Polynomial c = new Polynomial(0, Math.max(a.deg, b.deg));
for (int i = 0; i <= a.deg; i++) c.coef[i] += a.coef[i];
for (int i = 0; i <= b.deg; i++) c.coef[i] += b.coef[i];
c.deg = c.degree();
return c;
}
//method in subracting Polynomials that will return c = (a  b)
public Polynomial subtract(Polynomial b) {
Polynomial a = this;
Polynomial c = new Polynomial(0, Math.max(a.deg, b.deg));
for (int i = 0; i <= a.deg; i++) c.coef[i] += a.coef[i];
for (int i = 0; i <= b.deg; i++) c.coef[i] = b.coef[i];
c.deg = c.degree();
return c;
}
//method in Multiplying Polynomails that will return c = (a * b)
public Polynomial multiply(Polynomial b) {
Polynomial a = this;
Polynomial c = new Polynomial(0, a.deg + b.deg);
for (int i = 0; i <= a.deg; i++)
for (int j = 0; j <= b.deg; j++)
c.coef[i+j] += (a.coef[i] * b.coef[j]);
c.deg = c.degree();
return c;
}
Just want to ask whats the purpose of Polynomial a = "this" < why this .
 03122010, 04:34 PM #2Member
 Join Date
 Nov 2009
 Posts
 96
 Rep Power
 0
this referrs to the object itself, so it is a binding to operate with that instance of the object and another Polynomial object.
 03122010, 04:39 PM #3Member
 Join Date
 Mar 2010
 Posts
 8
 Rep Power
 0
in the source code, why he used "this"?
instead of other.
 in the source code, why he used "this"?
instead of other.
There's no absolute need to use this in the code above if you reference the object's data directly:
Java Code:public Polynomial add(Polynomial b) { //Polynomial a = this; Polynomial c = new Polynomial(0, Math.max(deg, b.deg)); for (int i = 0; i <= deg; i++) c.coef[i] += coef[i]; for (int i = 0; i <= b.deg; i++) c.coef[i] += b.coef[i]; c.deg = c.degree(); return c; }
Oh, please look at my signature below and add edit your posts above to add code tags to your pasted code. It will make reading your posts much easier.
Much luck.
 03122010, 04:46 PM #5
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,000
 Blog Entries
 7
 Rep Power
 23
You could've written, say, the add( ... ) method like this:
Java Code:public Polynomial add(Polynomial b) { Polynomial c = new Polynomial(0, Math.max(deg, b.deg)); for (int i = 0; i <= deg; i++) c.coef[i] += coef[i]; for (int i = 0; i <= b.deg; i++) c.coef[i] += b.coef[i]; c.deg = c.degree(); return c; }
kind regards,
Jos
 03122010, 05:01 PM #6Member
 Join Date
 Mar 2010
 Posts
 8
 Rep Power
 0
Thanks Guys :D Got it.
Sorry just new in java.
Thanks Sir Jos & Faburable . :)
*Idk to close this thread. lol *

Similar Threads

Java question
By TGH in forum New To JavaReplies: 12Last Post: 11272009, 03:05 PM 
Java Question Bank
By mgm2010 in forum New To JavaReplies: 2Last Post: 07312009, 06:45 PM 
question about java rmi
By hakimade in forum Advanced JavaReplies: 1Last Post: 07012009, 07:15 AM 
Java Question
By Jay1.1 in forum New To JavaReplies: 11Last Post: 05012008, 04:04 PM 
New Comer, Java C# question
By lmei007 in forum Advanced JavaReplies: 0Last Post: 02042008, 05:54 PM
Bookmarks