Results 1 to 7 of 7
  1. #1
    thisisIT is offline Member
    Join Date
    Mar 2010
    Posts
    8
    Rep Power
    0

    Default 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 .

  2. #2
    sky
    sky is offline Member
    Join Date
    Nov 2009
    Posts
    96
    Rep Power
    0

    Default

    this referrs to the object itself, so it is a binding to operate with that instance of the object and another Polynomial object.

  3. #3
    thisisIT is offline Member
    Join Date
    Mar 2010
    Posts
    8
    Rep Power
    0

    Default

    in the source code, why he used "this"?
    instead of other.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    in the source code, why he used "this"?
    instead of other.
    instead of other what?

    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;
    }
    but I'll bet that this is being used for cleanliness and symmetry.

    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.

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by thisisIT View Post
    in the source code, why he used "this"?
    instead of other.
    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;
    }
    ... 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+b

    kind regards,

    Jos

  6. #6
    thisisIT is offline Member
    Join Date
    Mar 2010
    Posts
    8
    Rep Power
    0

    Default

    Thanks Guys :D Got it.
    Sorry just new in java.
    Thanks Sir Jos & Faburable . :)

    *Idk to close this thread. lol *

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by thisisIT View Post
    Thanks Guys :D Got it.
    Sorry just new in java.
    Thanks Sir Jos & Faburable . :)
    There is absolutely nothing wrong with being new, with asking questions and trying to learn.

    *Idk to close this thread. lol *
    In Thread Tools above, check out marking thread as "solved"

Similar Threads

  1. Java question
    By TGH in forum New To Java
    Replies: 12
    Last Post: 11-27-2009, 02:05 PM
  2. Java Question Bank
    By mgm2010 in forum New To Java
    Replies: 2
    Last Post: 07-31-2009, 06:45 PM
  3. question about java rmi
    By hakimade in forum Advanced Java
    Replies: 1
    Last Post: 07-01-2009, 07:15 AM
  4. Java Question
    By Jay-1.1 in forum New To Java
    Replies: 11
    Last Post: 05-01-2008, 04:04 PM
  5. New Comer, Java C# question
    By lmei007 in forum Advanced Java
    Replies: 0
    Last Post: 02-04-2008, 04:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •