Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In spirit of the Fraction example class seen in the first lecture, your first task in this course is to implement the class Polynomial whose

In spirit of the Fraction example class seen in the first lecture, your first task in this course is to implement the class Polynomial whose objects represent polynomials of variable x with integer coefficients, and their basic mathematical operations. (If your math skills on polynomials have become a bit rusty since you last used them back in high school, check out the page "Polynomials" in the "College Algebra" section of "Paul's Online Math Notes", the best and still very concise online resource for college level algebra and calculus that I know of.) As is good programming style unless there exist good reasons to do otherwise, this class will be intentionally designed to be immutable so that Polynomial objects cannot change their internal state after they have been constructed. The public interface of Polynomial should consist of the following instance methods.

@Override public String toString()

Implement this method first to return some kind meaningful, human readable String representation of this instance of Polynomial. This method is not subject to testing by the JUnit testers, so you can freely choose for yourself the textual representation that you want this method to produce. Having this method implemented properly will become immensely useful for debugging the remaining methods that you will write inside Polynomial class.

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.

public int getDegree()

Returns the degree of this polynomial. For example, the previous polynomial has degree 3.

public int getCoefficient(int k)

Returns the coefficient for the term of order k. For example, the term of order 3 of the previous polynomial equals 5, and the term of order 0 equals 42.

public int evaluate(int x)

Evaluates the polynomial using the value x for the unknown symbolic variable of the polynomial. For example, when called with x = 2 for the previous example polynomial, this method would return 68. Your method does not have to worry about potential integer overflows, but can assume that the final and intermediate results of this computation will always stay within the range of int.

*TEST FILE 1*

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()); } }

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 FILE 2*

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()); } }

*The question I need help with is this one above is for refrence to get to this point* need these 3 methods below

In this lab, we continue modifying the source code for the Polynomial class from the first two labs to allow equality and ordering comparisons to take place between objects of that type. Modify the class definition so that this class now implements Comparable. Then write the following methods to implement equality and ordering comparisons.

@Override public boolean equals(Object other)

Returns true if the other object is also a Polynomial of the exact same degree as this, so that the coefficients of this and other polynomial are pairwise equal. If the other object is anything else, this method returns false. (You can actually implement this method last after implementing the method compareTo, since at that point the logic of equality checking will be a one-liner after the instanceof check.)

@Override public int hashCode()

Whenever you override the equals method in a subclass, you should also override the hashCode method to ensure that two objects that their equals method considers to be equal will also have equal hash codes. This method computes and returns the hash code of this polynomial, used to store and find this object inside some instance of HashSet, or some other hash table based data structure. This hash code is computed by adding up the coefficients of this polynomial, but so that the coefficient for term k is multiplied by the element in position k % 5 in the array [547, 619, 877, 1013, 1163]. Define that array inside your class as a private named constant MULTIPLIERS.

public int compareTo(Polynomial other)

Implements the ordering comparison between this and other polynomial, as required by the interface Comparable, allowing the instances of Polynomial to be sorted or stored inside some instance of TreeSet. This method returns +1 if this is greater than other, -1 if other is greater than this, and 0 if both polynomials are equal in the sense of the equals method.

A total ordering relation between polynomials is defined by the rule that any polynomial of higher degree is automatically greater than any polynomial of lower degree, regardless of their coefficients. For two polynomials whose degrees are equal, the result of the order comparison is determined by the highest-order term for which the coefficients of the polynomials differ, so that the polynomial with a larger such coefficient is considered to be greater in this ordering.

Be careful to ensure that this method ignores the leading zeros of high order terms if you have them inside your polynomial coefficient array, and that the ordering comparison criterion is precisely the one defined in the previous paragraph.

*TEST FILE 3*

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 PolynomialTestThree { @Test public void testEquals() { int[] c1 = {-10, 99, 11, 12}; int[] c2 = {-10, -99, 11, 12}; Polynomial p1 = new Polynomial(c1); Polynomial p2 = new Polynomial(c2); Polynomial p3 = new Polynomial(c1); assertTrue(p1.equals(p1)); assertTrue(p1.equals(p3)); assertFalse(p1.equals(p2)); assertFalse(p2.equals(p1)); assertFalse(p1.equals("hello world")); } @Test public void testCompareTo() { int[] c1 = {-10, 99, 11, 12}; int[] c2 = {-10, -99, 11, 12}; int[] c3 = {42, 10000000}; Polynomial p1 = new Polynomial(c1); Polynomial p2 = new Polynomial(c2); Polynomial p3 = new Polynomial(c3); assertEquals(+1, p1.compareTo(p2)); assertEquals(-1, p2.compareTo(p1)); assertEquals(+1, p1.compareTo(p3)); assertEquals(-1, p3.compareTo(p2)); assertEquals(0, p1.compareTo(p1)); } 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); } @Test public void massTest() { Random rng = new Random(SEED); TreeSet tree = new TreeSet(); HashSet hash = new HashSet(); 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); assertEquals(tree.contains(p1), hash.contains(p1)); tree.add(p1); hash.add(p1); assertEquals(0, p1.compareTo(p1)); assertEquals(0, p2.compareTo(p2)); assertEquals(p1.compareTo(p2), -p2.compareTo(p1)); check.update(p1.compareTo(p2)); } assertEquals(tree.size(), hash.size()); for(Polynomial p: tree) { assertTrue(hash.contains(p)); } for(Polynomial p: hash) { assertTrue(tree.contains(p)); } assertEquals(28339163L, check.getValue()); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2015 Porto Portugal September 7 11 2015 Proceedings Part 2 Lnai 9285

Authors: Annalisa Appice ,Pedro Pereira Rodrigues ,Vitor Santos Costa ,Joao Gama ,Alipio Jorge ,Carlos Soares

1st Edition

3319235249, 978-3319235240

More Books

Students also viewed these Databases questions