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: 3859953 • Letter: P

Question

Polynomial Addition and Subtraction

This is Java

Write a program that adds and subtracts two polynomials. Use the following interface:

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.

}

#### Imp...#### It should Create and array or ArrayList of nodes, each node holding a term of the polynomial

Your code must use the Demo provided below.

Explanation / Answer

public interface PolynomialInterface {
PolynomialInterface add(PolynomialInterface other);
PolynomialInterface subtract(PolynomialInterface other);
String toString();
}
import java.util.ArrayList;
public class ArrayWithExponentAsIndexPolynomial implements PolynomialInterface
{
int polynomial[];
int highExp;
ArrayWithExponentAsIndexPolynomial()
{
polynomial=new int[200];
}
ArrayWithExponentAsIndexPolynomial(String pol)
{
polynomial=new int[200];
highExp=0;
int co=0;//Coefficient
int exp=0;//exponent
for(int i=0;i<pol.length();i++)
{
co=0;
exp=0;
while(pol.charAt(i)!='x' && pol.charAt(i)!='X' )
{   
if(pol.charAt(i)=='-')
{
i++;
while(i<pol.length())
{
if(pol.charAt(i)!='x' && pol.charAt(i)!='X' )
{
String sub=pol.substring(i,i+1);
co=co*10+Integer.parseInt(sub);
}
else
break;
i++;
}
co=co*-1;   
}
else if (pol.charAt(i)=='+')
{
i++;
}
else
{
String sub=pol.substring(i,i+1);
co=co*10+Integer.parseInt(sub);
i++;
}
if(i>=pol.length())
break;
}
i++;
if(i==pol.length())
{
if(pol.charAt(i-1)=='x' || pol.charAt(i-1)=='X')
exp=1;
}
i++;
if(i<pol.length())
while(pol.charAt(i)!='-' && pol.charAt(i)!='+' )
{
String sub=pol.substring(i,i+1);
exp=exp*10+Integer.parseInt(sub);
i++;
if(i>=pol.length())
break;
}
if(highExp<exp)
highExp=exp;
addATerm(exp,co);
i--;
}
}
void addATerm(int exp,int co)
{
polynomial[exp]=co;
}
int getHigh()
{
return highExp;
}
public PolynomialInterface add(PolynomialInterface other)
{
int high;
ArrayWithExponentAsIndexPolynomial temp=new ArrayWithExponentAsIndexPolynomial();
ArrayWithExponentAsIndexPolynomial otherPoly=(ArrayWithExponentAsIndexPolynomial)other;
if(this.getHigh()<otherPoly.getHigh())
{
high=otherPoly.getHigh();
temp.highExp=otherPoly.getHigh();
}
else
{
high=this.getHigh();
temp.highExp=this.getHigh();
}
for(int i=0;i<=high;i++)
{
if(this.polynomial[i]!=0 && otherPoly.polynomial[i]!=0)
{
temp.polynomial[i]=this.polynomial[i]+otherPoly.polynomial[i];
}
else if (this.polynomial[i]==0 && otherPoly.polynomial[i]!=0)
{
temp.polynomial[i]=otherPoly.polynomial[i];
}
else if (this.polynomial[i]!=0 && otherPoly.polynomial[i]==0)
{
temp.polynomial[i]=this.polynomial[i];
}
}
return temp;
}
public PolynomialInterface subtract(PolynomialInterface other)
{
int high;
ArrayWithExponentAsIndexPolynomial temp=new ArrayWithExponentAsIndexPolynomial();
ArrayWithExponentAsIndexPolynomial otherPoly=(ArrayWithExponentAsIndexPolynomial)other;
if(this.getHigh()<otherPoly.getHigh())
{
high=otherPoly.getHigh();
temp.highExp=otherPoly.getHigh();
}
else
{
high=this.getHigh();
temp.highExp=this.getHigh();
}
for(int i=0;i<=high;i++)
{
if(this.polynomial[i]!=0 && otherPoly.polynomial[i]!=0)
{
temp.polynomial[i]=this.polynomial[i]-otherPoly.polynomial[i];
}
else if (this.polynomial[i]==0 && otherPoly.polynomial[i]!=0)
{
temp.polynomial[i]=0-otherPoly.polynomial[i];
}
else if (this.polynomial[i]!=0 && otherPoly.polynomial[i]==0)
{
temp.polynomial[i]=this.polynomial[i];
}
}
return temp;
}

public String toString()
{
String poly="";
for(int i=this.getHigh();i>=0;i--)
{
if(polynomial[i]!=0)
{
if(i==1)
{
if(polynomial[i]<0)
poly=poly+"-"+polynomial[i]*-1+"x";
else
poly=poly+polynomial[i]+"x";
}
else if(i!=0)
{
if(polynomial[i]<0)
poly=poly+"-"+polynomial[i]*-1+"x^"+i;
else
{
if(i!=this.getHigh())
poly=poly+"+"+polynomial[i]+"x^"+i;
else
poly=poly+polynomial[i]+"x^"+i;
}
}
else
{
if(polynomial[i]<0)
poly=poly+"-"+polynomial[i]*-1;
else
poly=poly+"+"+polynomial[i];
}

}
}
return poly;
}
}
public class ArraySortedPolynomial implements PolynomialInterface
{
ArraySortedPolynomial()
{
}
ArraySortedPolynomial(String pol)
{
}
public PolynomialInterface add(PolynomialInterface other) {
return null;
}
public PolynomialInterface subtract(PolynomialInterface other) {
return null;
}
}
public class LinkedListInArrayPolynomial implements PolynomialInterface
{
LinkedListInArrayPolynomial(String pol)
{
}
public PolynomialInterface add(PolynomialInterface other) {
return null;
}
public PolynomialInterface subtract(PolynomialInterface other) {
return null;
}
}
public class PlynomialDemo
{
public static void main(String[] args)
{
String s1 = "4x^14-3x^12+4x^4+78";
String s2 = "-4x^4-3x^12+4x^17-78";
PolynomialInterface exAsIndex1 = new ArrayWithExponentAsIndexPolynomial(s1);
PolynomialInterface exAsIndex2 = new ArrayWithExponentAsIndexPolynomial(s2);
PolynomialInterface exAsIndex3;
exAsIndex3 = exAsIndex1.add(exAsIndex2);
System.out.println("First test is with array index as exponent. " );
System.out.println("exAsIndex1 = " + exAsIndex1);
System.out.println("exAsIndex2 = " + exAsIndex2);
System.out.println("exAsIndex3 = exAsIndex1.add(exAsIndex2) " + exAsIndex3);
exAsIndex3 = exAsIndex1.subtract(exAsIndex2);
System.out.println("exAsIndex3 = exAsIndex1.subtract(exAsIndex2) " + exAsIndex3);
System.out.println();
PolynomialInterface sortA1 = new ArraySortedPolynomial(s1);
PolynomialInterface sortA2 = new ArraySortedPolynomial(s2);
PolynomialInterface sortA3;
sortA3 = sortA1.add(sortA2);
System.out.println("Second test is sorted array of terms.");
System.out.println("sortA1 = " + sortA1);
System.out.println("sortA2 = " + sortA2);
System.out.println("sortA3 = sortA1.add(sortA2) " + sortA3);
sortA3 = sortA1.subtract(sortA2);
System.out.println("sortA3 = sortA1.subtract(sortA2) " + sortA3);
System.out.println();
PolynomialInterface link1 = new LinkListPolynomial(s1);
PolynomialInterface link2 = new LinkListPolynomial(s2);
PolynomialInterface link3;
System.out.println("Third test is linked list of terms.");
System.out.println("link1 = " + link1);
System.out.println("link2 = " + link2);
link3 = link1.add(link2);
System.out.println("sum of link1 and link2 = " + link3);
link3 = link1.subtract(link2);
System.out.println("link1 minus link2 = " + link3);
System.out.println();
PolynomialInterface linkInArray1 = new LinkedListInArrayPolynomial(s1);
PolynomialInterface linkInArray2 = new LinkedListInArrayPolynomial(s2);
PolynomialInterface linkInArray3 = new LinkedListInArrayPolynomial(s2);
System.out.println("Forth test is linked list of terms in an array.");
System.out.println("linkInArray1 = " + linkInArray1);
System.out.println("linkInArray2 = " + linkInArray2);
linkInArray3 = linkInArray1.add(linkInArray2);
System.out.println("sum of linkInArray1 and linkInArray2 = " + linkInArray3);
linkInArray3 = linkInArray1.subtract(linkInArray2);
System.out.println("linkInArray1 minus linkInArray2 = " + linkInArray3);
}
}