Originally Posted by

**Zaphod_b**
...

I did not want to take it on myself to solve your problem, I merely wanted to suggest that there are a number of ways to approach the problem. If one of my ways doesn't suit your sensibilities, try something else.

My pseudo-code was intended to suggest a way of thinking about it. Rather than implementing it, I was kind of hoping that you would *think* about it and see that the correct step inside the inner loop would be to add the new coefficient to the term already in the output list that had the same exponent. Then mark that term in the original list so that it won't be added again. (I didn't mean to mislead you, and I'm sorry if you thought that my pseudo-code was actually supposed to be a valid solution.)

I mean, if there were only one way to solve a problem, life is easy: Figure it out (discover it or have someone tell you about it) and then apply it. Problem solved---on to the next.

The main thing is that once you have broken out of the box in which you were trapped (no solution) to thinking about ways of solving, then, there are lots of things to try.

Cheers!

Z

It is okay. I already got it

@Override
public Polynomial add(Polynomial P2)
{
PolynomialImp P3 = new PolynomialImp();
for(Term t: P2)
{
this.addTerm(t);
}
for(int i = this.degree(); i >= 0; i--)
{
ArrayList<Term> temp = new ArrayList<Term>(5);
for(int j = 0; j <= this.terms.size() - 1; j++)
{
if(this.terms.get(j).getExponent() == i)
{
temp.add(this.terms.get(j));
}
}
if(temp.size() == 1)
{
P3.addTerm(temp.get(0));
}
else if(temp.size() > 1)
{
double coeff = 0;
//temp.get(0).getCoefficient() + temp.get(1).getCoefficient();
int expo = temp.get(0).getExponent();
for(int j = 0; j <= temp.size() - 1; j++)
{
coeff += temp.get(j).getCoefficient();
}
if(coeff == 0)
{
}
else
{
P3.addTerm(new TermImp(coeff,expo));
}
}
}
return P3;
}