Results 1 to 7 of 7
Thread: Java Question :D
- 03-12-2010, 03:29 PM #1
Member
- 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 .
- 03-12-2010, 03:34 PM #2
Member
- 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.
- 03-12-2010, 03:39 PM #3
Member
- Join Date
- Mar 2010
- Posts
- 8
- Rep Power
- 0
in the source code, why he used "this"?
instead of other.
-
instead of other what?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:
but I'll bet that this is being used for cleanliness and symmetry.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.
- 03-12-2010, 03:46 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,373
- Blog Entries
- 7
- Rep Power
- 17
You could've written, say, the add( ... ) method like this:
... but the difference are futile; in the original version the variable 'a' is just an alias for 'this'; maybe it meets the comments a bit better, i.e. add polynomials a+bJava 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
- 03-12-2010, 04:01 PM #6
Member
- 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: 11-27-2009, 02:05 PM -
Java Question Bank
By mgm2010 in forum New To JavaReplies: 2Last Post: 07-31-2009, 06:45 PM -
question about java rmi
By hakimade in forum Advanced JavaReplies: 1Last Post: 07-01-2009, 07:15 AM -
Java Question
By Jay-1.1 in forum New To JavaReplies: 11Last Post: 05-01-2008, 04:04 PM -
New Comer, Java C# question
By lmei007 in forum Advanced JavaReplies: 0Last Post: 02-04-2008, 04:54 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks