Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please write a constructor and method inside class Polynomial. public Polynomial(int[] coefficients) The constructor that receives as argument the array of coefficients that define the
Please write a constructor and method inside class Polynomial.
public Polynomial(int[] coefficients) The constructor that receives as argument the array of coefficients that define the polynomial. For a polynomial of degree n, the array coefficients contains exactly n + 1 elements so that the coefficient of the term of order k is in the element coefficients[k]. For example, the polynomial 5x - 7x + 42 that will be used as an example in all of the following methods would be represented as the coefficient array {42, -7, 0, 5). Terms missing from inside the polynomial are represented by having a zero coefficient in that position. However, the coefficient of the highest term of every polynomial should always be nonzero, unless the polynomial itself is identically zero. If this constructor is given as argument a coefficient array whose highest terms are zeroes, it should simply ignore those zero coefficients. For example, if given the coefficient array {-1, 2, 0, 0, 0}, the resulting polynomial would have the degree of only one, as if that coefficient array had been {-1, 2} without those pesky higher order zeros. To guarantee that the Polynomial class is immutable so that no outside code can ever change the internal state of an object after its construction (well, at least not without resorting to underhanded Java tricks such as reflection), the constructor should not assign only the reference to the coefficients array to the private field of coefficients, but it absolutely positively must create a separate but identical defensive copy of the argument array, and store that defensive copy instead. This technique ensures that the stored coefficients of the polynomial do not change if some outsider later changes the contents of the shared coefficients array that was passed as the constructor argument. public int getDegree() Returns the degree of this polynomial, that is, the exponent of its highest order term that has a nonzero coefficient. For example, the previous polynomial has degree 3. Constant polynomials have a degree of zeroStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started