Question
Creating a polynomial linked list program for adding/subtracting/multiplying polynomials? I've attached the code below with TODO with the parts I am stuck on: /** *
Creating a polynomial linked list program for adding/subtracting/multiplying polynomials?
I've attached the code below with TODO with the parts I am stuck on:
/** * Polynomial Class which implements the CalculatorOperand interface. * Maintains polynomials as an ordered linked list, with monomials arranged by decreasing degree */
public class Polynomial implements CalculatorOperand {
private class PolyNode { int coeff; int degree; PolyNode next;
PolyNode(int c, int d) { coeff = c; degree = d; } }
private PolyNode monomialsList; // TODO: initialize in the constructor Polynomial(int coeff, int degree) { // TODO: IMPLEMENT PolyNode newNode = new PolyNode(coeff, degree); }
/** * Returns this + coeff*x^degree * that; does not modify this or that. Assumes coeff is nonzero. */ // NOTE: normally, this would be private, but leave it public so we can test it public Polynomial addTimesMonomial (Polynomial that, int coeff, int degree) { return null; // TODO: IMPLEMENT; READ THE ASSIGNMENT AND IMPLEMENT add FIRST }
/** * Returns this+that; does not modify this or that */ public Polynomial add (Polynomial that) { return null;// TODO: IMPLEMENT }
/** * Returns this-that; does not modify this or that */ public Polynomial subtract (Polynomial that) { return null; // TODO: IMPLEMENT }
/** * Returns this*that; does not modify this or that */ public Polynomial multiply (Polynomial that) { return null; }
/** * Prints the polynomial the way a human would like to read it * @return the human-readable string representation */ public String toString () { if (monomialsList.next == null) return "0";
String ret = monomialsList.next.coeff<0 ? "-" : ""; for (polynode p = monomialsList.next; p!=null; { if (p.degree 0 || (p.coeff! =1 && p.coeff! =-1)) ret=ret + java.lang.math.abs(p.coeff) ;> 0) ret = ret + "x"; if (p.degree > 1) ret = ret + "^" + p.degree; if (p.next != null) ret = ret + (p.next.coeff<0 ? " - " : " + "); } return ret; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started