Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Polynomial Addition and Subtraction This is Java Write a program that adds and s

ID: 3827875 • Letter: P

Question

Polynomial Addition and Subtraction

This is Java

Write a program that adds and subtracts two polynomials. Implement the algorithm four different ways. The first two implementations will use arrays and the third will use pointers. The forth is a set of linked lists in an array. Use the following interface for the 4 classes:

public interface PolynomialInterface

{

PolynomialInterface add(PolynomialInterface other);

// Effect: Adds value to owner of addPolynomial method.

// Postcondition: Return value = this + value.

PolynomialInterface subtract(PolynomialInterface other);

// Effect: Subtracts value from owner of addPolynomial method.

// Postcondition: Return value = this - value. void readPolynomial();

// Postcondition: polynomial read.

String toString();

// Postcondition: polynomial converted to string. }

The class must be able to read and print polynomials. Example: 4X4 + X3 - 3 + 4X4 - 2X3 + 4X 8X4 - X3 + 4X 3

The four ways to implement the requirements:

2- Create an array or ArrayList holding coefficients with the array indexes as exponents

3- Create and array or ArrayList of nodes, each node holding a term of the polynomial

4- Use a linked list of terms using pointers.

Polynomials are linked lists in one static array. Implementations 2, 3, and 4 require a class that will encapsulate a polynomial term unique to that particular implementation. The string from the constructor is a polynomial that each implementation must take apart and store each term in sorted order. All three implementations will follow the same basic algorithm to add two sorted polynomials. One array is used to store multiple polynomial instances and the free store. This array must be declared static so that it is available to all polynomial instances. There are two challenges in the first implementation. The first is converting the polynomial string given in the constructor into the terms of the polynomial. The second is taking the internal representation of the polynomial and converting it back to a string in the toString() method. The other three implementations will modify slightly the code from the first implementation for their constructor and toString() methods. Your code must use the Demo provided below.

Explanation / Answer

You can try this code for adding polynomial using linked list.it Is working fine
Source code:
public class Node{
int coef;
int exp;
Node next;
Node(int c,int e,Node n){
coef=c;
exp=e;
next=n;
}
Node(int c,int e){
coef=c;
exp=e;
}

}
public class LinkedPoly{
static String exponent="";
Node head;
Node current;
LinkedPoly(){
head=null;
}
public void createList(int c,int e){
head=new Node(c,e,head);
}
public static LinkedPoly add(LinkedPoly list1,LinkedPoly list2){
LinkedPoly addList=new LinkedPoly();
Node temp1=list1.head;
Node temp3=temp1;
Node temp2=list2.head;
Node temp4=temp2;
while(temp1.next!=null){
while(temp2.next!=null){
if(temp1.exp==temp2.exp){
addList.createList((temp1.coef+temp2.coef),temp1.exp);
exponent+=temp1.exp;
}
temp2=temp2.next;
}
temp1=temp1.next;
temp2=temp4;
addList.print();
}
String[] array=exponent.split("");

while(temp3!=null){
boolean exponentPresent = false;
for(int i=1;i<array.length;i++){
if(temp3.exp==Integer.parseInt(array[i])){
exponentPresent = true;
}
}
if (!exponentPresent) {
addList.createList(temp3.coef,temp3.exp);
}
temp3=temp3.next;
}
while(temp4!=null){
boolean exponentPresent = false;
for(int i=1;i<array.length;i++){
if(temp4.exp==Integer.parseInt(array[i])){
exponentPresent = true;
}
}
if (!exponentPresent) {
addList.createList(temp4.coef,temp4.exp);
}
temp4=temp4.next;
}

return addList;
}
public void print() {
current = head;
System.out.print(current.coef + "x^" + current.exp);
while (current.next != null) {
current = current.next;
System.out.print(" + " + current.coef + "x^" + current.exp);
}
System.out.println();
}
public static void main (String args[]){
LinkedPoly l1=new LinkedPoly();
l1.createList(3,2);
l1.createList(5,1);
l1.createList(3,0);
LinkedPoly l2=new LinkedPoly();
l2.createList(4,3);
l2.createList(5,1);
l2.createList(2,0);
LinkedPoly l3=add(l1,l2);
l3.print ();
}