Question: The second lab continues with the Polynomial class from the first lab by adding new methods for polynomial arithmetic in its source code. (There is
The second lab continues with the Polynomial class from the first lab by adding new methods for polynomial arithmetic in its source code. (There is no inheritance or polymorphism yet in this lab.) Since the class Polynomial is designed to be immutable, none of the following methods should modify the objects this or other in any way, but return the result of that arithmetic operation as a brand new Polynomial object created inside that method.
public Polynomial add(Polynomial other)
Creates and returns a new Polynomial object that represents the result of polynomial addition of the two polynomials this and other. This method should not modify this or other polynomial in any way. Make sure that just like with the constructor, the coefficient of the highest term of the result is nonzero, so that adding the two polynomials 5x10 - x2 + 3x and -5x10 + 7, each of them of degree 10, produces the result polynomial -x2 + 3x + 7 that has a degree of only 2 instead of 10.
public Polynomial multiply(Polynomial other)
Creates and returns a new Polynomial object that represents the result of polynomial multiplication of the two polynomials this and other. Polynomial multiplication works by multiplying all possible pairs of terms between the two polynomials and adding them together, combining the terms of equal rank together into the same term.
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 PolynomialTestTwo { private static final int SEED = 12345; private static final int TRIALS = 100000; private Polynomial createRandom(int deg, Random rng) { int[] c = new int[deg + 1]; for(int j = 0; j < deg + 1; j++) { c[j] = rng.nextInt(20) - 10; } return new Polynomial(c); } private boolean polyEq(Polynomial p1, Polynomial p2, CRC32 check) { if(p1.getDegree() != p2.getDegree()) { return false; } for(int k = 0; k <= p1.getDegree(); k++) { check.update(p1.getCoefficient(k)); if(p1.getCoefficient(k) != p2.getCoefficient(k)) { return false; } } return true; } @Test public void massTest() { Random rng = new Random(SEED); CRC32 check = new CRC32(); for(int i = 0; i < TRIALS; i++) { Polynomial p1 = createRandom(rng.nextInt(10), rng); Polynomial p2 = createRandom(rng.nextInt(10), rng); Polynomial p3 = p1.add(p2); Polynomial p4 = p2.add(p1); assertTrue(polyEq(p3, p4, check)); Polynomial p5 = p1.multiply(p2); Polynomial p6 = p2.multiply(p1); assertTrue(polyEq(p5, p6, check)); } assertEquals(2427324440L, check.getValue()); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
