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

Create a class to represent a term in an algebraic expression. As defined here,

ID: 3812169 • Letter: C

Question

Create a class to represent a term in an algebraic expression. As defined here, a term consists of an integer coefficient and a nonnegative integer exponent. E.g.

in the term 4x2, the coefficient is 4 and the exponent 2

in -6x8, the coefficient is -6 and the exponent 8

Your class will have a constructor that creates a Term object with a coefficient and exponent passed as parameters, and accessor methods that return the coefficient and the exponent. Your class will also override toString to return a Term in this general form:

ax^b     where a is the coefficient and b the exponent

with these special cases:

Case

Returns

a = 1

x^b

b = 1

ax

b = 0

a

a = 1, b = 1

x

You may assume that the coefficient will not be zero.

     

The Generic Node Class

This has already been done, declared as an inner class in Polynomial. Do not modify it in any way. Note that it will not compile until the Term class is defined.

The Polynomial Class

A “skeleton” of the Polynomial class you are to use is on the class web site. All you have to do is write the bodies of the methods.

Do not declare any new instance variables or methods and do not modify any of the method declarations.

As defined here, a polynomial is a sequence of terms. E.g.

1.

2.

3.

The terms of polynomial 1 are (3,2), (4,4) and (1,6). The terms of polynomial 2 are (2,0), (5,2), (6,3) and (2,7). Polynomial 3 has only one term (4,10)

To receive credit for this assignment, you must use no data structures other than your own programmer-defined (you being the programmer) singly-linked list of generic Nodes

Note that the Polynomial class has:

A constructor that creates a null Polynomial (a Polynomial with 0 terms)

A copy constructor that creates a new Polynomial as an exact duplicate of an existing one (aka: a “deep” copy)

A method with signature

     public void addTerm(int coefficient, int exponent)

which creates a Term and places it in its proper position in the linked list

The terms on the list are stored in ascending order by exponent (see III., above) so there is never a need to “sort” the list. Terms with the same exponent may be stored in any order but will appear after all terms with lesser exponents and before all terms with greater exponents

A method with signature

     public Polynomial polyAdd(Polynomial p)

which adds this Polynomial to p and returns the sum

A method with signature

     public Polynomial polyMultiply(Polynomial p)

which multiplies this Polynomial by p and returns the product

An overridden toString method that returns a String representation of a polynomial as a sum of terms. For example polynomial 1 above would have this String representation:

          3x^2 + 48x^4 + x^6

    

A private method with signature

     private void collectTerms()

which “collects” like terms of this Polynomial. E.g.

     Before:   x^2 + 3x^2 + 2x^2 + 3x^3 + 5x^4 + 2x^4

     After:    6x^2 + 3x^3 + 7x^4

Polynomials should always be printed with like terms collected.

Helpful Hints! Divide and Conquer!

Begin by coding the Term class and Polynomial methods toString and addTerm. For now, have addTermsimply add each new Term at the head of the list or, if you prefer, at the end. This will enable you to run the program and verify that your Term class is correct and that new Terms are indeed being created and added to the list.

Code the polyAdd and polyMultiply methods. These are fairly straightforward and once completed, along with the temporary version of addTerm, you will have a majority of the credit in the bank. J

Modify the addTerm method so that each new Term is inserted in it’s proper place in the list. An algorithm will be discussed in class. Note that none of the other methods will need to be modified in any way. More credit in the bank.

Code the collectTerms method. An algorithm will be discussed in class.

Code the copy constructor.

Don’t forget: “With linked lists, crayons are more important than keyboards.

-------------------------------------------------------------------------------------------

Skeleton for Polynomial.java:

-------------------------------------------------------

PolynomialTester.java

=========================================

Please code only the collectTerms method.

Please comment the code throughly

Please solve this using only Node manipulation and LinkedList methods.

Also keep the solution as simple as possible, thanks

Case

Returns

a = 1

x^b

b = 1

ax

b = 0

a

a = 1, b = 1

x

Explanation / Answer

void collectTerms(const Polynomial & p1, const Polynomial & p2 )

{
     //getExpo() function returns the exponent part of the polynomial

   int p1Expo = p1.getExpo( );
   int p2Expo = p2.getExpo( ),       expo;
   double sum;
    

   if ( p1Expo > p2Expo )
      expo = p1Expo;
   else
      expo = p2Expo;

   for ( int i=expo; i >= 0; i-- )
   {
      sum = p1.getCoeff( i ) + p2.getCoeff( i );              //getCoeff() should return the Coefficient of the polynomial
      if ( sum != 0.0 )               // Don't need to set Coefficients that are 0.0
         {
       assert( k <= MAX_EXPO );
      Coeff[k] = value;
        // fix the expo if necessary
       if ( value != 0.0 && k > expo )   // expo increased (but not past maximum)
          expo = k;
           // if terms with largest exponents are 0, decrease expo
   int i = expo;
       while ( i >= 0 && Coeff[i] == 0.0 )
           {
              expo--;
              i--;
           }
   }
   } toString(); ///this should convert parts into polynomial

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote