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

I am needed to be able to explain what is happening with code for each class. Th

ID: 3828047 • Letter: I

Question

I am needed to be able to explain what is happening with code for each class. This is an polynomial add or subtract using arrays with exponents,linked list, and sorted array.

Polynomial 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.
   }

Array with exponents:

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

//Convert the polynomial string into linked list of polynomial terms

for(int i=0;i<pol.length();i++)

{

co=0;

exp=0;

//Find coefficient

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++;//skip x

if(i==pol.length())

{

if(pol.charAt(i-1)=='x' || pol.charAt(i-1)=='X')

exp=1;

}

i++;//skip ^

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--;

}

}

// stores the coefficient at index(exp)

void addATerm(int exp,int co)

{

// store the coefficient at index(exp)

polynomial[exp]=co;

}

int getHigh()

{

return highExp;

}

@Override

//Adds two polynomials and returns the resultant polynomial

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;

}

@Override

//Substracts one polynomial from another and returns the resultant polynomial

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="";

//Convert the linked list into polynomial string

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;

}

Array with Sorted:

public class ArraySortedPolynomial implements PolynomialInterface

{

ArraySortedPolynomial()

{

}

ArraySortedPolynomial(String pol)

{

}

@Override

public PolynomialInterface add(PolynomialInterface other) {

// TODO Auto-generated method stub

return null;

}

@Override

public PolynomialInterface subtract(PolynomialInterface other) {

// TODO Auto-generated method stub

return null;

}

  

}

LinkedList Poly:

class Node

{

int co;

int exp;

Node nextNode;

Node(int c, int e)

{

co=c;

exp=e;

}

void setNext(Node next)

{

nextNode=next;

}

Node getNext()

{

return nextNode;

}

int getCoefficient()

{

return co;

}

int getExponent()

{

return exp;

}

}

public class LinkListPolynomial implements PolynomialInterface

{

Node root;

LinkListPolynomial()

{

root=null;

}

LinkListPolynomial(String pol)

{

int co=0;//Coefficient

int exp=0;//exponent

//Convert the polynomial string into linked list of polynomial terms

for(int i=0;i<pol.length();i++)

{

co=0;

exp=0;

//Find coefficient

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++;//skip x

if(i==pol.length())

{

if(pol.charAt(i-1)=='x' || pol.charAt(i-1)=='X')

exp=1;

}

i++;//skip ^

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;

}

//Call addNode to add a new term to the polynomial

addNode(co,exp);

i--;

}

}

//Adds a new node to the polynomial

void addNode(int co,int exp)

{

//If there are no terms , yet

if(root==null)

{

root= new Node(co,exp);

root.setNext(null);

}

//if there are some terms, add the new term in sorting order(by exponent)

else

{

Node newNode=new Node(co,exp);

Node temp,prev=root;

temp=root;

int i=0;

while(temp.getNext()!=null && temp.getExponent()>=newNode.getExponent())

{

  

prev=temp;

temp=temp.getNext();

}

if(temp.getNext()==null)

{

if(temp.getExponent()>=newNode.getExponent())

{

temp.setNext(newNode);

newNode.setNext(null);

}

else

{

newNode.setNext(temp);

temp.setNext(null);

if(temp==root)

{

root=newNode;

}

else

prev.setNext(newNode);

}

  

}

else

{

if(temp==root)

{

newNode.setNext(temp);

root=newNode;   

}

else

{

newNode.setNext(temp);

prev.setNext(newNode);

  

  

}

}

}

}

@Override

//Adds two polynomials and returns the resultant polynomial

public PolynomialInterface add(PolynomialInterface other)

{

LinkListPolynomial nllp=new LinkListPolynomial();

LinkListPolynomial otherPoly=(LinkListPolynomial)other;

Node temp1=this.root;

Node temp2=otherPoly.root;

while(temp1!=null )

{

Node t=otherPoly.root;

if(temp1.getExponent()==t.getExponent())

nllp.addNode(temp1.getCoefficient()+t.getCoefficient(),temp1.getExponent());

else

{

while(t!=null)

{

if(temp1.getExponent()==t.getExponent())

{

nllp.addNode(temp1.getCoefficient()+t.getCoefficient(),temp1.getExponent());

break;

}

t=t.getNext();

}

if(t==null)

{

nllp.addNode(temp1.getCoefficient(),temp1.getExponent());

}

}

temp1=temp1.getNext();

}

while( temp2!=null)

{

Node t=this.root;

while(t!=null)

{

if(temp2.getExponent()==t.getExponent())

{

break;

}

t=t.getNext();

}

if(t==null)

{

nllp.addNode(temp2.getCoefficient(),temp2.getExponent());

}

temp2=temp2.getNext();

}

return nllp;

}

@Override

//Substracts one polynomial from another and returns the resultant polynomial

public PolynomialInterface subtract(PolynomialInterface other)

{

LinkListPolynomial nllp=new LinkListPolynomial();

LinkListPolynomial otherPoly=(LinkListPolynomial)other;

Node temp1=this.root;

Node temp2=otherPoly.root;

while(temp1!=null )

{

Node t=otherPoly.root;

if(temp1.getExponent()==t.getExponent())

nllp.addNode(temp1.getCoefficient()-t.getCoefficient(),temp1.getExponent());

else

{

while(t!=null)

{

//subtract the coefficients, if the exponents are equal

if(temp1.getExponent()==t.getExponent())

{

nllp.addNode(temp1.getCoefficient()-t.getCoefficient(),temp1.getExponent());

break;

}

t=t.getNext();

}

//add the first term as it is, if the exponents are not equal

if(t==null)

{

nllp.addNode(temp1.getCoefficient(),temp1.getExponent());

}

}

temp1=temp1.getNext();

}

//add the remaining terms

while( temp2!=null)

{

Node t=this.root;

while(t!=null)

{

if(temp2.getExponent()==t.getExponent())

{

break;

}

t=t.getNext();

}

if(t==null)

{

nllp.addNode(temp2.getCoefficient()*-1,temp2.getExponent());

}

temp2=temp2.getNext();

}

return nllp;

}

public String toString()

{

String poly="";

Node temp=root;

//Convert the linked list into polynomial string

while(temp!=null)

{

if(temp.getCoefficient()!=0)

{

if(temp.getExponent()==1)

{

if(temp.getCoefficient()<0)

poly=poly+"-"+temp.getCoefficient()*-1+"x";

else

poly=poly+temp.getCoefficient()+"x";

}

else if(temp.getExponent()!=0)

{

if(temp.getCoefficient()<0)

poly=poly+"-"+temp.getCoefficient()*-1+"x^"+temp.getExponent();

else

{

if(temp.getNext()!=null)

poly=poly+"+"+temp.getCoefficient()+"x^"+temp.getExponent();

else

poly=poly+temp.getCoefficient()+"x^"+temp.getExponent();

}

}

else

{

if(temp.getCoefficient()<0)

poly=poly+"-"+temp.getCoefficient()*-1;

else

poly=poly+"+"+temp.getCoefficient();

}

}

temp=temp.getNext();

}

return poly;

}

}

LinkedListInPoly:

public class LinkedListInArrayPolynomial implements PolynomialInterface

{

LinkedListInArrayPolynomial(String pol)

{

}

@Override

public PolynomialInterface add(PolynomialInterface other) {

// TODO Auto-generated method stub

return null;

}

@Override

public PolynomialInterface subtract(PolynomialInterface other) {

// TODO Auto-generated method stub

return null;

}

  

}

Polynomial Demo:

public class PlynomialDemo
{
public static void main(String[] args)
{
// example strings constructor must handle
// String s = "44";
// String s = "44x";
// String s = "4x^4+3x^3-3";
// String s = "4x^3-3x^11";
// String s = "44x^6-3x^10+4x^4";
// String s = "25x^5-3x^13+4x^12-78";
// String s ="34x^15-44x^14-3x^12+4x^31-78";
// String s1 = "44";
// String s2 = "44x-78";
// String s1 = "4x^4+3x^3-3";
// String s2 = "4x^6-3x^12";
String s1 = "4x^14-3x^12+4x^4+78";
String s2 = "-4x^4-3x^12+4x^17-78";
// String s1 = "4x^4+3x^11+4x^10";
// String s2 = "5x^14-3x^12+4x^19-78";
// String s1 = "4x^5+4x^4-3x^12-4x^41-78";
// String s2 = "-4x^4+3x^12+4x^41+78";
// Four implementations of the interface
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 string is " + s1);
System.out.println("exAsIndex1 = " + exAsIndex1);
// System.out.println("exAsIndex2 string is " + s2);
System.out.println("exAsIndex2 = " + exAsIndex2);
System.out.println("exAsIndex3 = exAsIndex1.add(exAsIndex2) " + exAsIndex3);
exAsIndex3 = exAsIndex1.subtract(exAsIndex2);
// System.out.println("exAsIndex1 string is " + s1);
// System.out.println("exAsIndex1 = " + exAsIndex1);
// System.out.println("exAsIndex2 string is " + s2);
//System.out.println("exAsIndex2 = " + 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 string is " + s1);
System.out.println("sortA1 = " + sortA1);
// System.out.println("sortA2 string is " + s2);
System.out.println("sortA2 = " + sortA2);
System.out.println("sortA3 = sortA1.add(sortA2) " + sortA3);
sortA3 = sortA1.subtract(sortA2);
// System.out.println("sortA1 string is " + s1);
//System.out.println("sortA1 = " + sortA1);
// System.out.println("sortA2 string is " + s2);
//System.out.println("sortA2 = " + 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 string is " + s1);
System.out.println("link1 = " + link1);
// System.out.println("link2 string is " + s2);
System.out.println("link2 = " + link2);
link3 = link1.add(link2);
System.out.println("sum of link1 and link2 = " + link3);
// System.out.println("link1 string is " + s1);
// System.out.println("link1 = " + link1);
// System.out.println("link2 string is " + s2);
//System.out.println("link2 = " + link2);
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 string is " + s1);
System.out.println("linkInArray1 = " + linkInArray1);
// System.out.println("linkInArray2 string is " + s2);
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);
}
}

Explanation / Answer

#include #include #include using namespace std; // Declaring the class for the linked list with it's two required //integer variables that need to be linked for the polynomials class link{ public: int coeff; int expo; class link *next; }; //sets the linked list variables equal to NULL for the while statement to work/stop class link *poly1=NULL,*poly2=NULL,*poly=NULL; //Declaring the functions and their parameters void input(link*node); void output(link*node); void addition(link*poly1, link*poly2,link*poly); //Main function that will call all of the other functions when necessary through a do/while loop int main() { char ch; do { //declaring the polynomial variables poly1=(class link *)malloc(sizeof(class link)); poly2=(class link *)malloc(sizeof(class link)); poly=(class link *)malloc(sizeof(class link)); //"poly" is the variable that is the sum of poly1 and poly2 //goes to the create function to input the first and second polynomials printf(" First polynomial:"); input(poly1); printf(" Second polynomial:"); input(poly2); //output of the first and second polynomials printf(" First polynomial:"); output(poly1); printf(" Second polynomial:"); output(poly2); //adds together the 2 polynomials and outputs them addition(poly1,poly2,poly); printf(" Sum of polynomial:"); output(poly); //allows the user to repeat the program or end it printf(" Add two more polynomials, or hit 'N' to exit program:"); ch=getch(); } while(ch=='y'||ch=='Y'); printf(" "); return 0; } //Allows the user to enter the coefficient and the exponent for any number of integers in the polynomial void input(class link *node) { char ch; do { printf(" Enter the coefficient:"); scanf("%d",&node->coeff); //input for the coefficient printf(" Enter the exponent:"); scanf("%d",&node->expo); //input for the exponent node->next=(class link*)malloc(sizeof(class link)); node=node->next; node->next=NULL; //sets the next to NULL printf(" Continue?(Y/N):"); //allows the user to enter more integers in the polynomial ch=getch(); } while(ch=='y' || ch=='Y'); } //Adds the two polynomials together void addition(class link *poly1,class link *poly2,class link *poly) { //Organizes the exponents of the varibles in the polynomials while(poly1->next && poly2->next) { if(poly1->expo>poly2->expo) { poly->expo=poly1->expo; poly->coeff=poly1->coeff; poly1=poly1->next; } else if(poly1->expoexpo) { poly->expo=poly2->expo; poly->coeff=poly2->coeff; poly2=poly2->next; } else { poly->expo=poly1->expo; poly->coeff=poly1->coeff+poly2->coeff; poly1=poly1->next; poly2=poly2->next; } poly->next=(class link *)malloc(sizeof(class link)); poly=poly->next; poly->next=NULL; } //Actually adds the coefficients together if the exponents are equal while(poly1->next || poly2->next) { if(poly1->next) { poly->expo=poly1->expo; poly->coeff=poly1->coeff; poly1=poly1->next; } if(poly2->next) { poly->expo=poly2->expo; poly->coeff=poly2->coeff; poly2=poly2->next; } poly->next=(class link *)malloc(sizeof(class link)); poly=poly->next; poly->next=NULL; } } //Outputs the sum of the polynomials void output(class link *node) { //while loop to output the linked list variables while(node->next!=NULL) { printf("%dx^%d",node->coeff,node->expo); node=node->next; if(node->next!=NULL) printf("+"); } }