Answered step by step
Verified Expert Solution
Question
1 Approved Answer
program below is Successfully compiled.please define the main method for that and print the output public class Polynomial { class PolyNode { int coeff; int
program below is Successfully compiled.please define the main method for that and print the output
public class Polynomial { class PolyNode { int coeff; int degree; PolyNode next; public PolyNode(int coeff, int degree) { this.coeff = coeff; this.degree = degree; } } private PolyNode head; // points to the first Node of a Polynomial public Polynomial() // DO NOT MODIFY THIS CONSTRUCTOR { head = null; } public Polynomial(Polynomial p) // a "copy" constructor { // get the node of list PolyNode n = p.head; while (n != null) { // when we add, it automatically makes the element head, if there is // only element addTerm(n.coeff, n.degree); // go to next element n = n.next; } } /** * Creates a new Term and Node containing it and inserts it in its proper * place in this Polynomial (i.e. in ascending order by exponent) */ public void addTerm(int coeff, int degree) { if(coeff == 0) return; // create node PolyNode n = new PolyNode(coeff, degree); // if it is the first element if (head == null) { head = n; } else { // if it has same exponent as head if (head.degree == degree) { head.coeff = (head.coeff + coeff); return; } else if (head.degree < degree) { // it need to be inserted before head n.next = head; head = n; return; } // loop like p will be the previous node, and we will be comparing // p.next inside // keep previous node information is required as we need to add this // node in between PolyNode p = head; while (p.next != null) { if (degree == p.degree) { // if same exponent term is there p.coeff = (p.coeff + coeff); return; } else if (degree > p.next.degree) { // loop till we get some lower exponent position n.next = p.next; p.next = n; return; } // move to next p = p.next; } if(p.degree == degree) { p.coeff = (p.coeff + coeff); return; } // add to the last p.next = n; } } String getTerm(int coefficient, int exponent) { String s = "+"; if(coefficient < 0) { s = ""; } if (coefficient == 1 && exponent == 1) return s + "x"; else if (exponent == 0) return s + String.valueOf(coefficient); else if (coefficient == 1) return s + "x^" + String.valueOf(exponent); else if (exponent == 1) return s + String.valueOf(coefficient) + "x"; else return s + String.valueOf(coefficient) + "x^" + String.valueOf(exponent); } /** * Returns a polynomial as a String in this form: x + 3x^2 + 7x^3 + x^5 */ public String toString() { String s = ""; PolyNode node = head; String seperator = ""; // for each term, do add while (node != null) { s += seperator + getTerm(node.coeff, node.degree); node = node.next; seperator = " "; } return "(" + s + ")"; } /** * Multiply this Polynomial by another Polynomial */ public Polynomial multiply(Polynomial p) { Polynomial result = new Polynomial(); PolyNode p1 = head; // traverse for each term of p1 while (p1 != null) { PolyNode p2 = p.head; // traverse for each term of p2 while (p2 != null) { // add the new term to result polynomial result.addTerm(p1.coeff * p2.coeff, p1.degree + p2.degree); // advance p2 p2 = p2.next; } // advance p1 p1 = p1.next; } return result; } /** * Add this Polynomial and another Polynomial */ public Polynomial plus(Polynomial p) { // create a copy of original polynomial // we will add new node to directly this. Polynomial result = new Polynomial(this); PolyNode p1 = p.head; // our add Term method, add the coefficients, if exponent is same, // else it will create a new term while (p1 != null) { result.addTerm(p1.coeff, p1.degree); p1 = p1.next; } return result; } /** * subtract this Polynomial and another Polynomial */ public Polynomial minus(Polynomial p) { // create a copy of original polynomial // we will add new node to directly this. Polynomial result = new Polynomial(this); PolyNode p1 = p.head; // our add Term method, add the coefficients, if exponent is same, // else it will create a new term // we are changing the sign of coefficient here, so that it can be added while (p1 != null) { result.addTerm(-1 * p1.coeff, p1.degree); p1 = p1.next; } return result; } public boolean equals(Polynomial p) { PolyNode p1 = this.head; PolyNode p2 = p.head; while (p1 != null || p2 != null) { if((p1.coeff != p2.coeff) || (p1.degree != p2.degree)) { return false; } p2 = p2.next; p1 = p1.next; } return true; } }
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