Question: Data Structures (in Java) - Ch. 2 Programming Assignment 2-3 We can represent a polynomial as a list of terms, where the terms are in

Data Structures (in Java) - Ch. 2 Programming Assignment 2-3

We can represent a polynomial as a list of terms, where the terms are in decreasing order by the exponent.

  • You should define a class Term that contains data fields coef and exponent. For example, 5x4 has a coef value of 5 and an exponent value of 4.
  • To add two polynomials, you traverse both lists and examine the two terms at the current iterator position.
  • If the exponent of one is smaller than the exponent of the other, then insert the larger one into the result and advance that lists iterator.
  • If the exponents are equal, then create a new term with that exponent and the sum of the two coefficients, and advance both iterators. For example: 3x4 + 2x2 + 3x + 7 added to 2x3 + -5x + 5 is 3x4 + 2x3 + 2x2 + -2x +12.

Write a polynomial class with an inner class Term.

  • The polynomial class should have a data field terms that is of type LinkedList .
  • Provide an addpoly method and a readpoly method.
  • Method readypoly reads a string representing a polynomial such as 2x3 + -4x2 and returns a polynomial list with two terms.
  • You also need a toString method for class Term and Polynomial that would display this stored polynomial 2x^3 + 4x^2.

Provide a multiple method for your polynomial class.

  • To multiply, you iterate through polynomial A and then multiply all terms of polynomial B by the current term of polynomial A.
  • You then add each term you get by multiplying two terms to the polynomial result.
  • Hint: to multiply two terms, multiply their coefficients and add their exponents. For example, 2x3 x 4x2 is 8x5.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!