Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Polynomial Using LinkedList class of Java Language Description: Implement a poly

ID: 3603701 • Letter: P

Question

  Polynomial Using LinkedList class of Java Language  Description: Implement a polynomial class using a LinkedList defined in Java   (1) Define a polynomial that has the following methods for Polynomial  a. public Polynomial()           POSTCONDITION: Creates a polynomial represents 0  b. public Polynomial(double a0)          POSTCONDITION: Creates a polynomial has a single x^0 term with coefficient a0  c. public Polynomial(Polynomial p)          POSTCONDITION: Creates a polynomial is the copy of p   d. public void add_to_coef(double amount, int exponent)       POSTCONDITION: Adds the given amount to the coefficient of the specified exponent.      Note: the exponent is allowed to be greater than the degree of the polynomial          example: if p = x + 1, after p.add_to_coef(1, 2), p = x^2  + x + 1             e.  public void assign_coef(double coefficient, int exponent)      POSTCONDITION: Sets the coefficient for the specified exponent.      Note: the exponent is allowed to be greater than the degree of the polynomial     f. public double coefficient(int exponent)       POSTCONDITION: Returns coefficient at specified exponent of this polynomial.       Note: the exponent is allowed to be greater than the degree of the polynomial         e.g. if p = x + 1; p.coeffcient(3) should return 0  g. public double eval(double x)      POSTCONDITION: The return value is the value of this polynomial with the given value for the variable x.      Do not use power method from Math, which is very low efficient   h. public boolean equals (Object p)          POSTCONDITION: return true if p is a polynomial and it has same terms as this polynomial  i. public string toString()          POSTCONDITION: return the polynomial as a string like “2x^2 + 3x + 4”                  Important only non-zero terms   j. public Polynomial add(Polynomial p)      POSTCONDITION:                 this object and p are not changed                    return a polynomial that is the sum of   p and this polynomial    k. public Polynomial multiply(Polynomial p)      POSTCONDITION:       this object and p should not be changed      returns a new polynomial obtained by multiplying this term and p. For example, if this polynomial is      2x^2 + 3x + 4 and p is 5x^2 - 1x + 7, then at the end of this function, it will return the polynomial 10x^4 + 13x^3 + 31x^2 + 17x + 28.   (2)Implement    2-1 using   LinkedList defined in Java        a. Define as a nested static class in Polynomial,   which has           a. two instance variables:                  double coeff                 int exp           b. cnstructor (int exp, double coeff)                              b. Polynomial has the following instance variables:           LinkedList<Term> first; //the first term of a linked list of terms which are sorted in decresing order of exponent                 2-2 or you can define your own linked list        a. Define TermNode as a nested static class in Polynomial,   which has           a. three instance variables:                  double coeff                 int exp                 Term next;           b. cnstructor (int exp, double coeff, TermNode nextTerm )                           b. Polynomial has the following instance variables:           TermNode first; //the first term of a linked list of terms which are sorted in decresing order of exponent              

Explanation / Answer

public class PolynomialTerm

{

    private int exp;

    private int coe;

     

    public PolynomialTerm(int exp, int coe)

    {

       this.exp = exp;

       this.coe = coe;

    }

    public int getExp() {

        return exp;

    }

    public int getCoe() {

        return coe;

    }

     

     

         

    public int value(int x)

    {

        return (int) Math.pow(x,exp)*coe;

    }

     

    public PolynomialTerm Plus(PolynomialTerm term )

    {

          

        if(term.getExp() == this.getExp())

        {

           term = new PolynomialTerm(exp, (this.coe+term.coe));

            

        }

        return term;

    }

     

    public PolynomialTerm Times(PolynomialTerm term)

    {

        return new PolynomialTerm(term.exp+this.getExp(),term.coe*this.getCoe());

    }

     

    public PolynomialTerm Derivative()

    {

        return new PolynomialTerm(exp-1,exp*coe);

    }

    @Override

    public String toString() {

        return "PolynomialTerm{" + coe+"x^"+ exp + '}';

}   

}

public class PolynomialTerm

{

    private int exponent;

    private int coefficient;

     

    public PolynomialTerm(int exp, int coe)

    {

       this.exponent = exp;

       this.coefficient = coe;

    }

    public int getExponent()

    {

        return exponent;

    }

    public int getCoefficient()

    {

        return coefficient;

    }

     

     

         

    public int value(int x)

    {

        return (int) Math.pow(x,this.getExponent())*this.getCoefficient();

    }

     

    public PolynomialTerm Plus(PolynomialTerm term )

    {

          

        if(term.getExponent() == this.getExponent())

        {

           term = new PolynomialTerm(this.getExponent(), (this.getCoefficient()+term.getCoefficient()));

            

        }

        return term;

    }

     

    public PolynomialTerm Times(PolynomialTerm term)

    {

        return new PolynomialTerm(term.getExponent()+this.getExponent(),term.getCoefficient()*this.getCoefficient());

    }

     

    public PolynomialTerm Derivative()

    {

        return new PolynomialTerm(this.getExponent()-1,this.getExponent()*this.getCoefficient());

    }

    @Override

    public String toString()

    {

        return "PolynomialTerm{" + coefficient+"x^"+ exponent + '}';

    }

     

}