Question
I need to create a JAVA program that calculates polynomials in the form Node(coefficent, degree) such that 2^3 = (2, 3). I've added breakpoints at
I need to create a JAVA program that calculates polynomials in the form Node(coefficent, degree) such that 2^3 = (2, 3). I've added breakpoints at the TODO sections for what I'm stuck on. TODOS are to do as the statement says i.e. Returns this + coeff*x^degree * that = previous polynomial + (5x^3) * next
public interface CalculatorOperand { public T add(T that); public T subtract(T that); public T multiply(T that); }
/** * 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; // TODO: add a constructor PolyNode(int p, int l) { coeff = p; degree = l; } }
Polynomial(int coeff, int degree) { // TODO: IMPLEMENT PolyNode monomialsList = new PolyNode(0, 0); // TODO: initialize in the constructor }
/** * 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; //TODO: IMPLEMENT }
/** * 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