Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In spirit of the Fraction example class seen in the first lecture, your first ta

ID: 3912256 • Letter: I

Question

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*

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*

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

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<Polynomial>. 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<Polynomial>, 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<Polynomial>, allowing the instances of Polynomial to be sorted or stored inside some instance of TreeSet<Polynomial>. 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*

Explanation / Answer

/* A Polynomial has all its coefficients in an array coeffs */

public class Polynomial {

// coeffs[i] is the coefficient of the term x^i

protected int[] coeffs;

/* This Polynomial has coefficients as specified in the array */

public Polynomial(int[] coefficients) {

coeffs = new int[coefficients.length];

for (int i = 0; i < coeffs.length; i++)

coeffs[i] = coefficients[i];

}

/* =degree of this Polynomial */

public int getDegree() {

int d = coeffs.length - 1;

while ((coeffs[d] == 0) && (d > 0))

d--;

return d;

}

public double getCoefficient(int i) {

return this.coeffs[i];

}

/* ={This Polynomial is the same as Polynomial p}, true or false */

public boolean equals(Polynomial p) {

if (getDegree() != p.getDegree())

return false;

for (int i = 0; i <= getDegree(); i++)

if (coeffs[i] != p.coeffs[i])

return false;

return true;

}

/* = The value of this polynomial evaluated at x */

public int evaluate(int x) {

int sum = 0;

int input = 1;

for (int k = 0; k < coeffs.length; k++) {

sum += coeffs[k] * input;

input *= x;

}

return sum;

}

/* Add this Polynomial to Polynomial p */

public Polynomial add(Polynomial p) {

int degree = getDegree();

if (p.getDegree() > degree) {

degree = p.getDegree();

}

int[] coefficients = new int[degree];

for (int i = 0; i < getDegree(); i++)

coefficients[i] += coeffs[i];

for (int i = 0; i < p.getDegree(); i++)

coefficients[i] += p.coeffs[i];

return new Polynomial(coefficients);

}

/* =Polynomial that is the derivative of this Polynomial */

public Polynomial derivative() {

int[] coefficients = new int[getDegree()];

for (int i = 0; i < getDegree(); i++) {

coefficients[i] = (i + 1) * coefficients[i + 1];

}

return new Polynomial(coefficients);

}

@Override

public String toString() {

int degree = getDegree();

if (degree == -1)

return "0";

else if (degree == 0)

return "" + coeffs[0];

else if (degree == 1)

return coeffs[1] + "x + " + coeffs[0];

String s = coeffs[degree] + "x^" + degree;

for (int i = degree - 1; i >= 0; i--) {

if (coeffs[i] == 0)

continue;

else if (coeffs[i] > 0)

s = s + " + " + (coeffs[i]);

else if (coeffs[i] < 0)

s = s + " - " + (-coeffs[i]);

if (i == 1)

s = s + "x";

else if (i > 1)

s = s + "x^" + i;

}

return s;

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote