Question
Part A: Coding I need to add few methods related to the given codes below. Please help! 3. Implement Polynomial Linked List add method which
Part A: Coding
I need to add few methods related to the given codes below.
Please help!
3. Implement Polynomial Linked List add method which adds two polynomials in standard form and returns sum polynomial which also has to be in standard form. - Rewrite the instance method that takes a second Polynomial Linked List, and then adds all terms in the caller Polynomial and all terms in the second Polynomial Linked List together into another sum Polynomial. Both caller and parameter Polynomials are expected to be in standard form, the method should return the sum Polynomial also in standard form. A standard form polynomial has no duplicate terms (each terms exponent is unique) and the exponent is in descending order.
4. Implement Polynomial Linked list multiply method, similar to add, it returns the result polynomial in standard form.
Given Codes:
/** * * Student: * */ public class A1LinkedList{ public static void main(String argc[]){ PolynomialLinkedlist sum, prod; PolynomialLinkedlist p1 = new PolynomialLinkedlist(2,3); PolynomialLinkedlist p2 = new PolynomialLinkedlist(3,2); p3 = p1.add(p2); p1 = new PolynomialLinkedlist(3,2); p2 = new PolynomialLinkedlist(1,0); p4 = p1.add(p2); sum = p3.add(p4); prod = p3.multiply(p4); sum.print(); prod.print(); p1.print(); p2.print(); } } 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 first; private PNode last; public PolynomialLinkedlist(){ first = last = null; } public PolynomialLinkedlist(int c, int e){ PNode tempn = new PNode(c, e); first = last = tempn; } public void print(){ if (first == null){ System.out.println(); return; } PNode temp = first; String ans = ""; while (temp != null){ if (temp.getCoe() > 0) { if (temp != first) ans = ans + " + "; ans = ans + temp.getCoe(); } else if (temp.getCoe()Part A-coding 1. Implement Singly Linked List detectLoop which check whether the linked list contains a loop. It's an instance method that takes no parameter. It returns true if the linked list contains a loop that goes on forever if tracing the list from head. It returns false if it does not contain a loop 2. Implement Doubly Linked List add method which add an element to a specific position it's an instance method that takes a position and an element, then adds the element to this specific position and shifts the element currently at this position and any subsequent elements to the right. It throws an exception if the position is out of bound. It traverses the list from header if the position is closer to the header and traverses the list from trailer otherwise. 3. Implement Polynomial Linked List add method which adds two polynomials in standard form and returns sum polynomial which also has to be in standard form. Rewrite the instance method that takes a second Polynomial Linked List, and then adds all terms in the caller Polynomial and all terms in the second Polynomial Linked List together into another sum Polynomial. Both caller and parameter Polynomials are expected to be in standard form, the method should return the sum Polynomial also in standard form. A standard form polynomial has no duplicate terms (each term's exponent is unique) and the exponent is in descending order. Extra Credit: Implement Polynomial Linked list multiply method, similar to add, it returns the result polynomial in standard form
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