Question: Using linked lists write code to add, multiply, and evaluate polynomials Node Class: package poly; /** * This class implements a linked list node that

Using linked lists write code to add, multiply, and evaluate polynomials

Node Class:

package poly;

/**

* This class implements a linked list node that contains a Term instance.

*

* @author runb-cs112

*

*/

public class Node {

/**

* Term instance.

*/

Term term;

/**

* Next node in linked list.

*/

Node next;

/**

* Initializes this node with a term with given coefficient and degree,

* pointing to the given next node.

*

* @param coeff Coefficient of term

* @param degree Degree of term

* @param next Next node

*/

public Node(float coeff, int degree, Node next) {

term = new Term(coeff, degree);

this.next = next;

}

}

Term Class:

public class Term {

/**

* Coefficient of term.

*/

public float coeff;

/**

* Degree of term.

*/

public int degree;

/**

* Initializes an instance with given coefficient and degree.

*

* @param coeff Coefficient

* @param degree Degree

*/

public Term(float coeff, int degree) {

this.coeff = coeff;

this.degree = degree;

}

/* (non-Javadoc)

* @see java.lang.Object#equals(java.lang.Object)

*/

public boolean equals(Object other) {

return other != null &&

other instanceof Term &&

coeff == ((Term)other).coeff &&

degree == ((Term)other).degree;

}

/* (non-Javadoc)

* @see java.lang.Object#toString()

*/

public String toString() {

if (degree == 0) {

return coeff + "";

} else if (degree == 1) {

return coeff + "x";

} else {

return coeff + "x^" + degree;

}

}

}

/**

* Returns the sum of two polynomials - DOES NOT change either of the input polynomials.

* The returned polynomial MUST have all new nodes. In other words, none of the nodes

* of the input polynomials can be in the result.

*

* @param poly1 First input polynomial (front of polynomial linked list)

* @param poly2 Second input polynomial (front of polynomial linked list

* @return A new polynomial which is the sum of the input polynomials - the returned node

* is the front of the result polynomial

*/

public static Node add(Node poly1, Node poly2) {

/** COMPLETE THIS METHOD **/

// FOLLOWING LINE IS A PLACEHOLDER TO MAKE THIS METHOD COMPILE

// CHANGE IT AS NEEDED FOR YOUR IMPLEMENTATION

return null

)

/**

* Returns the product of two polynomials - DOES NOT change either of the input polynomials.

* The returned polynomial MUST have all new nodes. In other words, none of the nodes

* of the input polynomials can be in the result.

*

* @param poly1 First input polynomial (front of polynomial linked list)

* @param poly2 Second input polynomial (front of polynomial linked list)

* @return A new polynomial which is the product of the input polynomials - the returned node

* is the front of the result polynomial

*/

public static Node multiply(Node poly1, Node poly2) {

/** COMPLETE THIS METHOD **/

// FOLLOWING LINE IS A PLACEHOLDER TO MAKE THIS METHOD COMPILE

// CHANGE IT AS NEEDED FOR YOUR IMPLEMENTATION

return null

)

/**

* Evaluates a polynomial at a given value.

*

* @param poly Polynomial (front of linked list) to be evaluated

* @param x Value at which evaluation is to be done

* @return Value of polynomial p at x

*/

public static float evaluate(Node poly, float x) {

/** COMPLETE THIS METHOD **/

// FOLLOWING LINE IS A PLACEHOLDER TO MAKE THIS METHOD COMPILE

// CHANGE IT AS NEEDED FOR YOUR IMPLEMENTATION

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!