Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

i need help in converting the return polynomial in descending order with respect to the degrees and to remove terms with 0 coefficients. e.g. if

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

i need help in converting the return polynomial in descending order with respect to the degrees and to remove terms with 0 coefficients. e.g. if i put in 4x^5 -2x^3+2x +3 as polynomial 1 and the opposite -4x^5 +2x^3-2x -3 as polynomial 2, it should print the sum as 0.0, not 0.0 + 0.0x + 0.0x^3 + 0.0x^5.

so basically we need to remove the 0 coefficients and return 0 as the total sum equations like the example. and have the returned answer for both add and multiply in descending order with respect to the degree. Thank you!

__________________________________

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

Node polySum = null;

Node ptr2 = poly2;

Node ptr1 = poly1;

while (ptr1 != null) {

polySum = combine(ptr1.term.coeff, ptr1.term.degree, polySum);

ptr1 = ptr1.next;

}

while (ptr2 != null) {

polySum = combine(ptr2.term.coeff, ptr2.term.degree, polySum);

ptr2 = ptr2.next;

}

return deleteZeros(revSort(polySum));

}

__________________________________

public static Node revSort(Node head) {

if (head == null || head.next == null) {

return head;

}

Node currList = head.next;

Node reversedList = head;

reversedList.next = null;

while (currList != null) {

Node temp = currList;

currList = currList.next;

temp.next = reversedList;

reversedList = temp;

}

return reversedList;

}

__________________________

private static Node combine(float coeff, int degree, Node polyCom) {

if(coeff == 0) {

return polyCom;

}

Node polyA = new Node(coeff, degree, null);

if (polyCom == null) {

return polyA;

}

else {

//if(coeff==0) {

// return polyCom;

//}

if (polyCom.term.degree == degree) {

polyCom.term.coeff = (polyCom.term.coeff + coeff);

return polyCom;

}

else if (polyCom.term.degree

polyA.next = polyCom;

polyCom = polyA;

return polyCom;

}

Node ptr = polyCom;

while (ptr.next != null) {

if (degree == ptr.term.degree) {

ptr.term.coeff = (ptr.term.coeff + coeff);

return polyCom;

}

else if (degree > ptr.next.term.degree) {

polyA.next = ptr.next;

ptr.next = polyA;

return polyCom;

}

ptr = ptr.next;

}

if(ptr.term.degree == degree) {

ptr.term.coeff = (ptr.term.coeff + coeff);

return polyCom;

}

ptr.next = polyA;

return polyCom;

}

}

________________________

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

Node polyMul = null;

while(poly1.next != null) {

Node start = poly2;

while(start != null) {

polyMul = combine(poly1.term.coeff * start.term.coeff, poly1.term.degree + start.term.degree, polyMul);

start = start.next;

}

poly1 = poly1.next;

}

return revSort(polyMul);

}

__________________

private static Node deleteZeros(Node listHead) {

Node headPtr=listHead;

/*while(headPtr!=null) {

// if(headPtr.term.coeff==0) {

// listHead=listHead.next;

// headPtr.next=headPtr.next.next;

//}

if(headPtr.next.term.coeff==0) {

while(headPtr.next.next.term.coeff==0) {

headPtr.next.next=headPtr.next.next.next;

}

}

else if(headPtr.next.next.term.coeff==0 && headPtr.next.next==null) {

//headPtr.next=headPtr;

headPtr=null;

}

else if(headPtr.next.next!=null) {

headPtr.next.next=headPtr.next;

headPtr=headPtr.next;

}

else {

//headPtr.next=headPtr.next.next;

headPtr.next.next=headPtr.next;

headPtr=headPtr.next;

}

}

if(listHead==null) {

return null;

}

else {

return listHead;

}

}*/

__________________

206 21 Node polySumnull; Node ptr2 poly2; Node ptr1 polyl; 23 while (ptrinull) 25 26 27 28 29 polySumcombine(ptrl.term.coeff, ptrl.term.degree, polySum) ptr1-ptr1.next; while (ptr2-null) t polySum-combine(ptr2.term.coeff, ptr2.term.degree, polySum); ptr2ptr2.next; 31 32 return polySum; 37 private static Node combine(float coeff, int degree, Node polyCom) if(coeff0) 39 return polyCom; Node polyA new Node(coeff, degree, null); if (polyComnull) 42 43 45 47 49 51 return polyA; else if (polyCom.term.degreedegree) polyCom.term.coeff (polyCom.term.coeff coeff); return polyCom; else if (polyCom.term.degree "); return (Integer.parseInt(sc1.nextLine())) 24 25 26 27 public static void add) throws IOException System.out.print("Enter the file containing the polynomial to add "); sc2 new Scanner(new File(sc1.nextLine))); poly2 Polynomial.read(sc2); System.out.println("nPolynomial.toString(poly2)n") System.out.println("Sum: "+ 29 30 34 Polynomial.toString(Polynomial.add (poly1,poly2)) "n"); 35 37O public static void multiply() throws IOException 38 40 42 System.out.print("Enter the file containing the polynomial to multiply sc2 = new Scanner(new File (sc1.nextLine())); poly2 Polynomial.read (sc2); System.out.println("In" +Polynomial.toString(poly2)"n") System.out.println("Product:"+ 43 Polynomial.toString (Polynomial.multiply(poly1,poly2))" "); 47 public static void evaluate() 48 49 50 51 52 53 540 public static void main(String[] args) throws IOException throws IOException t System.out.print("Enter the evaluation point x ); float x = Float . parseFloat(sc1.nextLine()); System.out.println("Value at"x+ ":"Polynomial.evaluate(poly1,x) "n"); sc1 new Scanner(System.in); System.out.print("Enter the name of the polynomial file ); sc2 new Scanner(new File(sc1.nextLine())); 56 58 59 60 polylPolynomial.read (sc2); System.out.println("n" Polynomial.toString(poly1) +"n"); 62 63 int choice getChoice) while (choice!QUIT) if (choice 1 1I choice > QUIT) 65 System.out.println("ItIncorrect choice "choice); elsef 67 68 69 70 switch (choice) case ADD: add(); break; case MULTIPLY: multiply; break; case EVALUATE:evaluate(); break; default: break; choice getChoice) 76 78 1 package poly; 2 30/kok 4 This class implements a linked list node that contains a Term instance. 6 @author runb-cs112 9 public class Node 10 k Term instance. 12 13 Term term; 15 160 / 17 18 19 20 210 / k Next node in linked list. Node next; * Initializes this node with a term with given coefficient and degree, k pointing to the given next node. 23 @param coeff Coefficient of term k @param degree Degree of term k @param next Next node 25 26 27 28 29 public Node (float coeff, int degree, Node next) 30 31 32 term= new Term (coeff, degree) this.next next; 35 1 package poly 2 4 This class implements a term of a polynomial 6 @author runb-cs112 9 public class Term 10 110 /k 12 13 14 15 160 17 18 19 20 210 k Coefficient of term. public float coeff; Degree of term. k/ public int degree; * Initializes an instance with given coefficient and degree. 23 24 25 26 27 public Term(float coeff, int degree) 28 29 30 31 32 * (non-Javado) * @param coeff Coefficient *@param degree Degree this. coeff = coeff; this.degree-degree; * @see java.lang . Object#equals(java.lang.0bject) 34 A35public boolean equals (0bject other) return other != null && other instanceof Term && coeff((Term)other).coeff && degree((Term)other).degree; 36 37 38 39 40 41 420 43 /* (non-lavados) * @see java.lang . Object#tost ring() 45public String toString) if (degree0) } else if (degree 46 47 48 49 50 51 52 53 54 return coeff ; return coeff + "x"; return coeff + "X^" + degree; 1) { else 206 21 Node polySumnull; Node ptr2 poly2; Node ptr1 polyl; 23 while (ptrinull) 25 26 27 28 29 polySumcombine(ptrl.term.coeff, ptrl.term.degree, polySum) ptr1-ptr1.next; while (ptr2-null) t polySum-combine(ptr2.term.coeff, ptr2.term.degree, polySum); ptr2ptr2.next; 31 32 return polySum; 37 private static Node combine(float coeff, int degree, Node polyCom) if(coeff0) 39 return polyCom; Node polyA new Node(coeff, degree, null); if (polyComnull) 42 43 45 47 49 51 return polyA; else if (polyCom.term.degreedegree) polyCom.term.coeff (polyCom.term.coeff coeff); return polyCom; else if (polyCom.term.degree "); return (Integer.parseInt(sc1.nextLine())) 24 25 26 27 public static void add) throws IOException System.out.print("Enter the file containing the polynomial to add "); sc2 new Scanner(new File(sc1.nextLine))); poly2 Polynomial.read(sc2); System.out.println("nPolynomial.toString(poly2)n") System.out.println("Sum: "+ 29 30 34 Polynomial.toString(Polynomial.add (poly1,poly2)) "n"); 35 37O public static void multiply() throws IOException 38 40 42 System.out.print("Enter the file containing the polynomial to multiply sc2 = new Scanner(new File (sc1.nextLine())); poly2 Polynomial.read (sc2); System.out.println("In" +Polynomial.toString(poly2)"n") System.out.println("Product:"+ 43 Polynomial.toString (Polynomial.multiply(poly1,poly2))" "); 47 public static void evaluate() 48 49 50 51 52 53 540 public static void main(String[] args) throws IOException throws IOException t System.out.print("Enter the evaluation point x ); float x = Float . parseFloat(sc1.nextLine()); System.out.println("Value at"x+ ":"Polynomial.evaluate(poly1,x) "n"); sc1 new Scanner(System.in); System.out.print("Enter the name of the polynomial file ); sc2 new Scanner(new File(sc1.nextLine())); 56 58 59 60 polylPolynomial.read (sc2); System.out.println("n" Polynomial.toString(poly1) +"n"); 62 63 int choice getChoice) while (choice!QUIT) if (choice 1 1I choice > QUIT) 65 System.out.println("ItIncorrect choice "choice); elsef 67 68 69 70 switch (choice) case ADD: add(); break; case MULTIPLY: multiply; break; case EVALUATE:evaluate(); break; default: break; choice getChoice) 76 78 1 package poly; 2 30/kok 4 This class implements a linked list node that contains a Term instance. 6 @author runb-cs112 9 public class Node 10 k Term instance. 12 13 Term term; 15 160 / 17 18 19 20 210 / k Next node in linked list. Node next; * Initializes this node with a term with given coefficient and degree, k pointing to the given next node. 23 @param coeff Coefficient of term k @param degree Degree of term k @param next Next node 25 26 27 28 29 public Node (float coeff, int degree, Node next) 30 31 32 term= new Term (coeff, degree) this.next next; 35 1 package poly 2 4 This class implements a term of a polynomial 6 @author runb-cs112 9 public class Term 10 110 /k 12 13 14 15 160 17 18 19 20 210 k Coefficient of term. public float coeff; Degree of term. k/ public int degree; * Initializes an instance with given coefficient and degree. 23 24 25 26 27 public Term(float coeff, int degree) 28 29 30 31 32 * (non-Javado) * @param coeff Coefficient *@param degree Degree this. coeff = coeff; this.degree-degree; * @see java.lang . Object#equals(java.lang.0bject) 34 A35public boolean equals (0bject other) return other != null && other instanceof Term && coeff((Term)other).coeff && degree((Term)other).degree; 36 37 38 39 40 41 420 43 /* (non-lavados) * @see java.lang . Object#tost ring() 45public String toString) if (degree0) } else if (degree 46 47 48 49 50 51 52 53 54 return coeff ; return coeff + "x"; return coeff + "X^" + degree; 1) { else

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Pro Oracle Fusion Applications Installation And Administration

Authors: Tushar Thakker

1st Edition

1484209834, 9781484209837

More Books

Students also viewed these Databases questions

Question

Which of the following statements regarding ALU is Correct?

Answered: 1 week ago