Question
JAVA LANGUAGE ONLY I need help in converting the return polynomial in descending order with respect to the degrees and to remove terms with 0
JAVA LANGUAGE ONLY
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.
In Conclusion 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 e.g. 4x^5 -2x^3+2x +3 instead of 3+ 2x -2x^3 +4x^5.
Thank you!
I Will Thumbs Up Good Work
THE NECESSARY METHODS ARE BELOW.
__________________________________
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 < 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;
}
}*/
__________________
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