**Add a toString method to the Polynomials class below. The method should support the following examples. (Java)

• 5 2+4 −25×2+4x−2 creates the string “5x^2 +4x^1 -2”

• −50 3+ 2+3−50×3+x2+3 creates the string “-50x^3 +1x^2 +3”

• 4 +2 5−3 2−104x+2×5−3×2−10 creates the string “2x^5 -3x^2 +4x^1 -10”

**Also add another constructor that takes a polynomial as a string, parses it and creates the polynomial accordingly. The string contains the polynomial, with each term separated by a space.

——————————————

// Polynomials.java

public class Polynomials {

public Polynomials() {

}

public void addTerm(int power, double coefficient) {

if (power

throw new IllegalArgumentException(“Power cannot be negative”);

}

return; // exit from function

}

// or if head contains the same power, adding to the coefficient

return; // exit from function

}

while (temp.getNext() != null) {

if (temp.getNext().getPower() == power) {

temp.getNext().setCoefficient(

temp.getNext().getCoefficient() + coefficient);

return;  }

temp = temp.getNext();

}

temp.setNext(new PolynomialNode(power, coefficient));

}

public void removeTerm(int power) {

// if head is null, doing nothing

return;

}

return;

}

// otherwise taking reference to PolynomialNode

while (temp.getNext() != null) {

if (temp.getNext().getPower() == power) {

// removing next node of temp

temp.setNext(temp.getNext().getNext());

break;// exit from function

}

temp = temp.getNext();

}

}

public int getDegree() {

int maxDegree = 0;

// looping and finding highest power

for (PolynomialNode node = head; node != null; node = node.getNext()) {

int power = node.getPower();

if (maxDegree

maxDegree = power;

}

}

return maxDegree;

}

public double getCoefficient(int power) {

// looping and finding the node with given power

for (PolynomialNode node = head; node != null; node = node.getNext()) {

if (power == node.getPower()) {

// found, returning coefficient

return node.getCoefficient(); }            }

return 0;

}

public double evaluate(double x) {

double result = 0.0;

// looping through the nodes and evaluating the polynomial

for (PolynomialNode node = head; node != null; node = node.getNext()) {

result += node.getCoefficient() * Math.pow(x, node.getPower());

}

return result;

}

Polynomials resultingPolynomial = new Polynomials();

for (PolynomialNode node = head; node != null; node = node.getNext()) {

}

for (PolynomialNode node = poly.head; node != null; node = node

.getNext()) {

return resultingPolynomial;      } }

class PolynomialNode {

int power;

double coefficient;

PolynomialNode next;

public PolynomialNode(int power, double coefficient) {

this.power = power;

this.coefficient = coefficient;

next = null; }

public int getPower() {

return power; }

public void setPower(int power) {

this.power = power; }

public double getCoefficient() {

return coefficient;   }

public void setCoefficient(double coefficient) {

this.coefficient = coefficient; }

public PolynomialNode getNext() {

return next; }

public void setNext(PolynomialNode next) {

this.next = next;      }}