Question: 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
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 5x3 - 7x + 42 that will be used as example in all of the following methods would be given 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 terms. For example, if given the coefficient array {-1, 2, 0, 0, 0}, the resulting polynomial would have degree of only one, as if that argument array had really been {-1, 2} without these leading 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 (at least not without resorting to underhanded tricks such as reflection), the constructor should not assign only the reference to the coefficients array to the private field of coefficients, but it must create a separate but identical defensive copy of the argument array, and store that defensive copy instead. This ensures that the stored coefficients of the polynomial do not change if somebody later changes the contents of the shared coefficients array that was given as the constructor argument.
Test Code:
import static org.junit.Assert.*; import org.junit.After; import org.junit.Before; import org.junit.Test; import java.util.Random; import java.io.*; import java.util.*; import java.util.zip.CRC32; public class PolynomialTestOne { @Test public void testPolynomial() { int[] c1 = {5, 0, -2, 3}; Polynomial p1 = new Polynomial(c1); assertEquals(3, p1.getDegree()); assertEquals(-2, p1.getCoefficient(2)); assertEquals(5, p1.evaluate(0)); assertEquals(21, p1.evaluate(2)); c1[3] = 5555; assertEquals(3, p1.getCoefficient(3)); int[] c2 = {42, 99, -3, -10, -4}; Polynomial p2 = new Polynomial(c2); assertEquals(4, p2.getDegree()); assertEquals(99, p2.getCoefficient(1)); int[] c3 = {99, 0, 0, 0, 0, 0, 0, 0}; Polynomial p3 = new Polynomial(c3); assertEquals(0, p3.getDegree()); assertEquals(99, p3.getCoefficient(0)); } private static final int SEED = 12345; private static final int TRIALS = 1000000; @Test public void massTest() { Random rng = new Random(SEED); CRC32 check = new CRC32(); for(int i = 0; i < TRIALS; i++) { int deg = rng.nextInt(10); int[] c = new int[deg + 1]; for(int j = 0; j < deg + 1; j++) { c[j] = rng.nextInt(20) - 10; } Polynomial p = new Polynomial(c); check.update(p.getDegree()); for(int j = -3; j <= 3; j++) { check.update(p.evaluate(j)); } } assertEquals(3166965570L, check.getValue()); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
