Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. ## Adding polynomials in an ArrayList?

I have an ArrayList terms with various polynomial terms inside. This is what is inside:

Values of this: 8.00x^2+1.00+4.00x^2+2.00
So there are 4 elements inside the array. Each one has their own coefficient and exponent.

I want to do a loop that adds the terms with like exponents and store them in another List P3. I want P3 to have this:

Values of P3: 12.00x^2+3.00
I have tried a for loop that compares the first element of the array with the second, third... and so on. If the exponents match, then I add the coefficients and add a new term with the new coefficient and the exponent to P3.

I also put another condition that if the term does not match any other term, then simply add that term into P3.

However, the output I get adds each term over and over. This is the result I get:

Values of P3: 8.00x^2+12.00x^2+8.00x^2+3.00+4.00x^2
This is the full code of the loop I have done so far. Could anyone tell me what I'm doing wrong?

```for(int i = 0; i <= this.terms.size() - 1; i++)
{
for(int j = i + 1; j <= this.terms.size() - 1; j++)
{
if(this.terms.get(i).getExponent() == this.terms.get(j).getExponent())
{
double newCoeff = this.terms.get(i).getCoefficient() + this.terms.get(j).getCoefficient();
}

else if(this.terms.get(i).getExponent() > this.terms.get(j).getExponent())
{
double newCoeff = this.terms.get(i).getCoefficient();
int newExp = this.terms.get(i).getExponent();
}
}
}```

--- Update ---

Now I see that it is comparing each and everyone if the terms and doing an action depending on the comparison. For example, at the start it compares 8x^2 with 1x^0 and doing this line of code:

```else if(this.terms.get(i).getExponent() > this.terms.get(j).getExponent())
{
double newCoeff = this.terms.get(i).getCoefficient();
int newExp = this.terms.get(i).getExponent();
}```

How can I make it so that only the ones that have like terms add and the other ones are added only once?  Reply With Quote

3. ## Re: Adding polynomials in an ArrayList?

Given:
Values of this: 8.00x^2 + 1.00 + 4.00x^2 + 2.00
This is the result I get:
Values of P3: 8.00x^2 + 12.00x^2 + 8.00x^2 + 3.00 + 4.00x^2
And this is what you want:
Values of P3: 12.00x^2 + 3.00
Can you post a small program that compiles, executes and shows the problem?  Reply With Quote

4. ## Re: Adding polynomials in an ArrayList? Originally Posted by jean28 ...
How can I make it so that only the ones that have like terms add and the other ones are added only once?
Well, you have to keep track of that somehow.

The following pseudo-code might indicate one way to do the deed, assuming that all of the exponents are greater than or equal to zero:

```// Initially, assume that all Exponent values are >= 0

Repeat the following loop as i goes from zero to the size of the polynomial
BEGIN LOOP // Outer loop

IF the Exponent of term i is greater than or equal to zero THEN

Add term i to the output list.

Repeat the following loop as j goes from i+1 to the size of p:
BEGIN LOOP // Inner loop
IF the Exponent of term j is equal to the Exponent of term i THEN

Add the Coefficient of term j to the Coefficient of term i.
Set the Exponent of term j equal to -1.

END IF
END LOOP // End of inner loop

END IF

END LOOP // End of outer loop```

See? Once a term has been added, it will be disqualified from further consideration. Would this work? Are there any disadvantages to this method?

Maybe a simpler way: Once the coefficient of term j has been added to that of term i in the output list, just remove term j from the input list. (you wouldn't need the "IF" statement inside the inner loop). Would that work? If not, why not? If so, would it be "better" than the first method?

Other ways???

Cheers!

Z  Reply With Quote

5. ## Re: Adding polynomials in an ArrayList? Originally Posted by Zaphod_b Well, you have to keep track of that somehow.

The following pseudo-code might indicate one way to do the deed, assuming that all of the exponents are greater than or equal to zero:

```// Initially, assume that all Exponent values are >= 0

Repeat the following loop as i goes from zero to the size of the polynomial
BEGIN LOOP // Outer loop

IF the Exponent of term i is greater than or equal to zero THEN

Add term i to the output list.

Repeat the following loop as j goes from i+1 to the size of p:
BEGIN LOOP // Inner loop
IF the Exponent of term j is equal to the Exponent of term i THEN

Add the Coefficient of term j to the Coefficient of term i.
Set the Exponent of term j equal to -1.

END IF
END LOOP // End of inner loop

END IF

END LOOP // End of outer loop```

See? Once a term has been added, it will be disqualified from further consideration. Would this work? Are there any disadvantages to this method?

Maybe a simpler way: Once the coefficient of term j has been added to that of term i in the output list, just remove term j from the input list. (you wouldn't need the "IF" statement inside the inner loop). Would that work? If not, why not? If so, would it be "better" than the first method?

Other ways???

Cheers!

Z
I did that loop that you wrote here:

```for(int i = 0; i <= this.terms.size() - 1; i++)
{
if(this.terms.get(i).getExponent() >= 0)
{
for(int j = i + 1; j <= this.terms.size() - 1 ; j++)
{
if(this.terms.get(j).getExponent() == this.terms.get(i).getExponent())
{
}
}
}
}```

Unfortunately, the output is not correct:

Values of this: 8.00x^2+1.00+4.00x^2+2.00
Values of P3: 8.00x^2+12.00x^2+1.00+3.00+4.00x^2+2.00
So the 8x^2 , 1, and 2 are surprlus.

I thould try to remove them as you said before.  Reply With Quote

6. ## Re: Adding polynomials in an ArrayList? Originally Posted by jean28 I did that loop that you wrote here:
...

Unfortunately, the output is not correct:
...

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  Reply With Quote

7. ## Re: Adding polynomials in an ArrayList? 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
{
PolynomialImp P3 = new PolynomialImp();

for(Term t: P2)
{
}
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)
{
}
}
if(temp.size() == 1)
{
}

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
{  Reply With Quote