Implement Polynomial Linked List multiply method which multiply two polynomial and returns the product as a new polynomial. - It’s an instance method which called by a polynomial and takes second polynomial as the parameter pl. It should correctly multiply two polynomials together and returns this product polynomial.
//please see the given code below
public class PolynomialLinkedlist{ private static class PNode{ private int coe; private int exp; private PNode next; public PNode(int c, int e){ this(c, e, null); } public PNode(int c, int e, PNode n){ coe = c; exp = e; next = n; } public void setCoe(int c){ coe = c;} public void setExp(int e){ exp = e;} public void setNext(PNode n){ next = n;} public int getCoe(){ return coe;} public int getExp(){ return exp;} public PNode getNext(){ return next;} } private PNode head; public PolynomialLinkedlist(){ head = null; } private void add(int c, int e){ PNode tempn = new PNode(c, e, head); head = tempn; } public void removeDuplicate(){ if (head == null) return; PNode lookUp, checkPrev; lookUp = checkPrev = head; while (lookUp.getNext() != null){ while (checkPrev.getNext() != null){ if (checkPrev.getNext().getExp() == lookUp.getExp()){//found a duplicate //lookUp.coe += checkPrev.next.coe; lookUp.setCoe(lookUp.getCoe() + checkPrev.getNext().getCoe()); checkPrev.setNext(checkPrev.getNext().getNext()); } else checkPrev = checkPrev.getNext(); } lookUp = lookUp.getNext(); checkPrev = lookUp; } } public PolynomialLinkedlist add(PolynomialLinkedlist pl){ PNode addTerm = this.head; PolynomialLinkedlist ans = new PolynomialLinkedlist(); for (int i = 0; i < 2; i++){ while (addTerm != null){ ans.add(addTerm.getCoe(), addTerm.getExp()); addTerm = addTerm.next; } addTerm = pl.head; } ans.removeDuplicate(); return ans; } public PolynomialLinkedlist multiply(PolynomialLinkedlist pl){ PolynomialLinkedlist ans = new PolynomialLinkedlist(); return ans; } public void print(){ if (head == null){ System.out.println(); return; } removeDuplicate(); PNode temp = head; while (temp.getNext() != null){ if (temp.getCoe() != 0){ System.out.print("(" + temp.getCoe() + ")"); if (temp.getExp() != 0){ System.out.print("X^" + temp.exp); } System.out.print(" + "); } temp = temp.next; } if (temp != null){ System.out.print(temp.coe); if (temp.exp != 0) System.out.print("X^" + temp.exp); System.out.println(); } } public static void main(String argc[]){ PolynomialLinkedlist pn1 = new PolynomialLinkedlist(); PolynomialLinkedlist pn2 = new PolynomialLinkedlist(); pn1.add(1, 2); pn1.add(2, 3); pn2.add(1, 0); pn2.add(5, 1); pn2.add(-6, 1); pn2.add(4, 2); System.out.print("Polynomial 1: "); pn1.print(); System.out.print("Polynomial 2: "); pn2.print(); PolynomialLinkedlist sum = pn1.add(pn2); PolynomialLinkedlist prod = pn1.multiply(pn2); System.out.print("Sum: "); sum.print(); System.out.print("Product: "); prod.print(); } }
Screenshot of the code that is to be added(rest the entire program is the same and untouched):
The Output Obtained:
The Code Used:
public class PolynomialLinkedlist{ private static class PNode{ private int coe; private int exp; private PNode next; public PNode(int c, int e){ this(c, e, null); } public PNode(int c, int e, PNode n){ coe = c; exp = e; next = n; } public void setCoe(int c){ coe = c;} public void setExp(int e){ exp = e;} public void setNext(PNode n){ next = n;} public int getCoe(){ return coe;} public int getExp(){ return exp;} public PNode getNext(){ return next;} } private PNode head; public PolynomialLinkedlist(){ head = null; } private void add(int c, int e){ PNode tempn = new PNode(c, e, head); head = tempn; } public void removeDuplicate(){ if (head == null) return; PNode lookUp, checkPrev; lookUp = checkPrev = head; while (lookUp.getNext() != null){ while (checkPrev.getNext() != null){ if (checkPrev.getNext().getExp() == lookUp.getExp()){//found a duplicate //lookUp.coe += checkPrev.next.coe; lookUp.setCoe(lookUp.getCoe() + checkPrev.getNext().getCoe()); checkPrev.setNext(checkPrev.getNext().getNext()); } else checkPrev = checkPrev.getNext(); } lookUp = lookUp.getNext(); checkPrev = lookUp; } } public PolynomialLinkedlist add(PolynomialLinkedlist pl){ PNode addTerm = this.head; PolynomialLinkedlist ans = new PolynomialLinkedlist(); for (int i = 0; i < 2; i++){ while (addTerm != null){ ans.add(addTerm.getCoe(), addTerm.getExp()); addTerm = addTerm.next; } addTerm = pl.head; } ans.removeDuplicate(); return ans; } public PolynomialLinkedlist multiply(PolynomialLinkedlist pl){ PolynomialLinkedlist ans = new PolynomialLinkedlist(); //this will store our answer. PNode temp1 = this.head; //this will be used to iterate through the first polynomial. while(temp1 != null) { PNode temp2 = pl.head; //this will be used to iterate through the second polynomial. //now when two polynomials are multiplied every term in the first polynomial is multiplied with every term with every other term //of the second polynomial. That's why we have used a nested loop. while(temp2!=null) { //Now when two independent terms of a polynomial are multiplied two things happens: //the coefficient gets multiplied. //and the exponents are added. ans.add(temp1.getCoe()*temp2.getCoe(),temp1.getExp()+temp2.getExp()); temp2 = temp2.next; //iterating through the second polynomial. } temp1 = temp1.next; //iterating through the first polynomial. } ans.removeDuplicate(); //removing the duplicates. return ans; } public void print(){ if (head == null){ System.out.println(); return; } removeDuplicate(); PNode temp = head; while (temp.getNext() != null){ if (temp.getCoe() != 0){ System.out.print("(" + temp.getCoe() + ")"); if (temp.getExp() != 0){ System.out.print("X^" + temp.exp); } System.out.print(" + "); } temp = temp.next; } if (temp != null){ System.out.print(temp.coe); if (temp.exp != 0) System.out.print("X^" + temp.exp); System.out.println(); } } public static void main(String argc[]){ PolynomialLinkedlist pn1 = new PolynomialLinkedlist(); PolynomialLinkedlist pn2 = new PolynomialLinkedlist(); pn1.add(1, 2); pn1.add(2, 3); pn2.add(1, 0); pn2.add(5, 1); pn2.add(-6, 1); pn2.add(4, 2); System.out.print("Polynomial 1: "); pn1.print(); System.out.print("Polynomial 2: "); pn2.print(); PolynomialLinkedlist sum = pn1.add(pn2); PolynomialLinkedlist prod = pn1.multiply(pn2); System.out.print("Sum: "); sum.print(); System.out.print("Product: "); prod.print(); } }
I hope you like the solution. In case of any doubts regarding the solution feel free to ask it in the comment section. If you like the solution please give a thumbs up.
Get Answers For Free
Most questions answered within 1 hours.