# Thread: Method for multiplying two polynomials

1. Member Join Date
Jul 2010
Posts
3
Rep Power
0

## Method for multiplying two polynomials

Hello everyone,
i am having problems writing a method which can multiply 2 polynomials. Can u help me out???
this is what i have done:
public Poly mult(Poly p) {
int n = getDegree();
int m = p.getDegree();
Poly result = new Poly(m+n);
int i,j;
Poly a = p;
float sum=0;
float [] temp = new float[n+m+1];

for (i=0; i<=n+m; i++){
for(j=0;j<=m+n;j++){
sum = sum+ coefficients[i] * p.getCoefficient(j);
}
int x = i+j;
result.setCoefficient(x, sum);

}
return result;

}

in the main program, it will be run as q.mult(p);
hope to hear from u soon!!!!  Reply With Quote

2. ## You should impement a method addCoefficient and make it do what its name implies. Don't use that setCoefficient method.

kind regards,

Jos  Reply With Quote

3. Member Join Date
Jul 2010
Posts
3
Rep Power
0

##   Reply With Quote

4. ##  Originally Posted by javaprgr Think of it: given the two polynomials: x^2+2*x+3 and 4*x^2+5*x+6 you have to multiply 9 different terms: x^2*4*x^2, x^2*5*x, x^2*6, 2*x*4*x*2, 2*x*5*x, 2*x*6, 3*x^2, 3*5*x, 3*6. If you look carefully several terms have the same exponent so you should add the coefficients, hence my suggesstion.

kind regards,

Jos  Reply With Quote

#### Posting Permissions

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