Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] problem with my function

Started by omegasyphon Apr 26, 2003 at 6:40 PM 3 replies 950+ views
Original Post
omegasyphon
omegasyphon
here is my problem i have this function that will multiply numbers together and keep them sorted in a linked list.
    
public Polynomial multiplyPoly( Polynomial rhs)
  {
    LinkedListIterator itr = myPoly.first();
    LinkedListIterator ritr = rhs.getPoly().first();
    Polynomial poly  = new Polynomial();
    Polynomial temp  = new Polynomial();

    while (ritr.isValid())
    {
      while (itr.isValid())
      {
                 temp.addTerm(((Term)itr.retrieve()).multiply((Term)ritr.retrieve()));
        itr.advance();
      }
       poly.addpoly(temp);
      ritr.advance();
      itr = myPoly.first();
    }

    return poly;
  }
    
now addpoly just adds 2 polynomials together and returns the sum as a polynomial. now i can call addpoly just fine in main. but when i call it in the function above it returns nothing. im not sure why? [edited by - omegasyphon on April 26, 2003 7:42:35 PM]
Malone1234
Malone1234
Post your addpoly() function. What is the return type? If it''s void then I don''t knwo what the problem is, but if it returns a Polynomial object, then you haven''t assigned that to anything.
omegasyphon
omegasyphon
ok heres the add function. it returns a polynomial object.


  

public Polynomial addPoly(Polynomial rhs)
{
LinkedListIterator itr = myPoly.first();
LinkedListIterator ritr = rhs.getPoly().first();
Polynomial npoly = new Polynomial();
while (itr.isValid() && ritr.isValid())
{
if (((Term)itr.retrieve()).compareTo(ritr.retrieve())==-1)
{
npoly.addTerm((Term)itr.retrieve());
itr.advance();
}
else if (((Term)ritr.retrieve()).compareTo(itr.retrieve())==-1)
{
npoly.addTerm((Term)ritr.retrieve());
ritr.advance();
}
else if (((Term)itr.retrieve()).compareTo(ritr.retrieve())==0)
{
npoly.addTerm(((Term)itr.retrieve()).add((Term)ritr.retrieve()));
itr.advance();
ritr.advance();
}
}

return npoly;
}
Jiim
Jiim
You need to change that poly.addPoly() line in your multiplyPoly() function to poly=poly.addPoly(). Should work then if there isn''t anything else wrong with it.

Jiim Jonez
--
www.develop-games.com
omegasyphon
omegasyphon
this works:
poly=poly.addpoly(temp);

but only when i do this after i create the poly

poly.addterm(new Term(0,0));

ok i fixed the problem. it finally works


[edited by - omegasyphon on April 29, 2003 7:11:54 PM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.