Results 1 to 4 of 4
  1. #1
    GPB
    GPB is offline Member
    Join Date
    Feb 2010
    Posts
    20
    Rep Power
    0

    Default what am i doing wrong here?

    I'm using an arraylist to multiply polynomials, but it doesn't work correctly:

    Java Code:
        public void times (Poly b) {
            for ( int p_a = 0 ; p_a < polynomial.size() ; p_a++ ) {
                Term temp = polynomial.get(p_a);
                for ( int p_b = 0 ; p_b < b.polynomial.size() ; p_b++ ) {
                    polynomial.set(p_a,temp.times(b.polynomial.get(p_b)));
                }
            }
        }
    Java Code:
        public Term times (Term term) {
            Term temp = new Term(coefficient *= term.coefficient, exponent += term.exponent);
            return temp;
        }
    ex. (x^2+3x-1)*(7-x^2+2x^3)
    The correct answer would be 2x^5+5x^4-5x^3+8x^2+21x-7,
    but in stead it gives me -14x^7-42x^6+14x^5

    I believe this is because it updates polynomial(p_a) after each loop and uses that new value in the next loop. Which is odd, because I put in a temp Term for exactly that reason.

  2. #2
    GPB
    GPB is offline Member
    Join Date
    Feb 2010
    Posts
    20
    Rep Power
    0

    Default

    Oh I think I got it already, I never add the terms, I just change them.

  3. #3
    GPB
    GPB is offline Member
    Join Date
    Feb 2010
    Posts
    20
    Rep Power
    0

    Default

    Hmm no, I'm still stuck.

  4. #4
    GPB
    GPB is offline Member
    Join Date
    Feb 2010
    Posts
    20
    Rep Power
    0

Similar Threads

  1. Replies: 4
    Last Post: 06-11-2013, 01:37 AM
  2. Please help, what am I doing wrong?
    By mmac1218 in forum New To Java
    Replies: 2
    Last Post: 09-01-2009, 11:21 PM
  3. what am i doing wrong here??
    By tornbacchus in forum New To Java
    Replies: 19
    Last Post: 04-16-2009, 03:54 AM
  4. What did i do wrong
    By jpnym15 in forum New To Java
    Replies: 8
    Last Post: 11-17-2008, 10:07 AM
  5. right or wrong
    By jot321 in forum New To Java
    Replies: 7
    Last Post: 09-25-2008, 11:45 AM

Posting Permissions

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