**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 {

      // head PolynomialNode

      private PolynomialNode head;

      public Polynomials() {

            // initializing head to null

            head = null;

      }

      public void addTerm(int power, double coefficient) {

            if (power

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

            }

            // if head is null, adding as head node

            if (head == null) {

                  head = new PolynomialNode(power, coefficient);

                  return; // exit from function

            }

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

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

                  head.setCoefficient(head.getCoefficient() + coefficient);

                  return; // exit from function

            }

            PolynomialNode temp = head;

            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

            if (head == null) {

                  return;

            }

            // if head has target power, removing head

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

                  head = head.getNext();

                  return;

            }

            // otherwise taking reference to PolynomialNode

            PolynomialNode temp = head;

            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;

      }

      public Polynomials addPolynomial(Polynomials poly) {

            Polynomials resultingPolynomial = new Polynomials();

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

                  resultingPolynomial.addTerm(node.getPower(), node.getCoefficient());

            }

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

                        .getNext()) {

                  resultingPolynomial.addTerm(node.getPower(), node.getCoefficient()); }

            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;      }}



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *