Question

Write a class Polynomial that stores a polynomial such as

p(x)=5x10+9x7x10p(x)=5 x^{10}+9 x^7-x-10

as a linked list of terms. A term contains the coefficient and the power of xx. For example, you would store p(x)p(x) as

(5,10),(9,7),(1,1),(10,0)(5,10),(9,7),(-1,1),(-10,0)

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 p(x)×p(x)p(x) \times p(x)

Polynomial q = p.multiply(p);
q.print();

Solution

Verified
Answered 6 months ago
Answered 6 months ago
Step 1
1 of 3

Lets 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

Create an account to view solutions

More related questions

1/4

1/7