Related questions with answers
Question
Write a class Polynomial that stores a polynomial such as
as a linked list of terms. A term contains the coefficient and the power of . For example, you would store as
Supply methods to add, multiply, and print polynomials. Supply a constructor that makes a polynomial from a single term. For example, the polynomial p can be constructed as
Polynomial p = new Polynomial(new Term(-10, 0));
p.add(new Polynomial(new Term(-1, 1)));
p.add(new Polynomial(new Term(9, 7)));
p.add(new Polynomial(new Term(5, 10)));
Then compute
Polynomial q = p.multiply(p);
q.print();
Solution
VerifiedAnswered 6 months ago
Answered 6 months ago
Step 1
1 of 3Lets first create the Term class.
public class Term {
private final int power;
private final int coeff;
public int getPower() {
return power;
}
public int getCoeff() {
return coeff;
}
public Term(int coeff, int power) {
this.power = power;
this.coeff = coeff;
}
public Term multiply(Term other) {
return new Term(coeff * other.coeff,power+other.power);
}
@Override
public String toString() {
if (power == 0) {
return String.valueOf(coeff);
}
return coeff + "x^" + power;
}
}
Create an account to view solutions
By signing up, you accept Quizlet's Terms of Service and Privacy Policy
Create an account to view solutions
By signing up, you accept Quizlet's Terms of Service and Privacy Policy
More related questions
- linear algebra
- computer science
- differential equations
1/4
- linear algebra
- computer science
- differential equations
1/7