/* An instance of this class represents a polynomial with integer coefficients. ** ** CMPS 144 ** Authors: R. McCloskey and < STUDENT's NAME > ** Collaborated with: ... ** Known Defects: ... */ public class Polynomial implements Cloneable { // instance variable // ----------------- // coeff[i] is the coefficient of x^i in this polynomial protected int[] coeff; // constructors // ------------ /* Establishes this polynomial as the zero polynomial (having degree ** zero and coefficient zero). */ public Polynomial() { // STUB! } /* Establishes this polynomial to have as coefficients the values in ** the specified array c[], with c[i] (for each value of i) being the ** coefficient of the x^i term. If c has length zero, this polynomial ** is made to be the zero polynomial. */ public Polynomial(int[] c) { if (c.length == 0) { // for the unusual case in which c[] has no elements coeff = new int[1]; coeff[0] = 0; } else { // Find the highest-numbered location in c[] containing a non-zero // value in order to determine the degree of this polynomial. int degree = c.length - 1; //Loop invariant: Every element in c[degree+1 .. c.length) is zero. while (degree != 0 && c[degree] == 0) { degree = degree - 1; } // Create a coefficient array having just as many elements as needed. coeff = new int[degree+1]; // Copy the values from c[0..degree] into coeff[]. for (int i=0; i <= degree; i++) { coeff[i] = c[i]; } } } // observers // --------- /* Returns the degree of this polynomial. */ public int degreeOf() { return coeff.length - 1; } /* Returns the coefficient of x^k in this polynomial. ** (If k > degreeOf(), zero is returned.) */ public int coefficient(int k) { int result; if (k <= degreeOf()) { result = coeff[k]; } else { result = 0; } return result; } /* Returns the result of evaluating this polynomial at the specified ** value (x). */ public double valueAt(double x) { return 0.0; // STUB! } /* Returns a value within the interval [low,high] that is within EPSILON ** units of a root (aka zero) of this polynomial. ** An exception is thrown if either low > high or the product of ** valueAt(low) and valueAt(high) is positive (in which case there is ** no guarantee of there being a root in the interval [low,high]). */ public double rootOf(double low, double high, double EPSILON) { // Verify that low <= high. If not, throw an exception. if (low > high) { throw new IllegalArgumentException("Empty interval"); } // Verify that the product valueAt(low) * valueAt(high) is not // positive. If not, throw an exception. if (valueAt(low) * valueAt(high) > 0) { throw new IllegalArgumentException("Values at endpoints have same sign!"); } // A root in [low,high] must exist. Find one. return 0.0; // STUB! } /* Reports whether or not this polynomial equals the given one (p), ** which is to say that they are of the same degree and their ** corresponding coefficients are the same. */ public boolean equals(Polynomial p) { boolean result; if (degreeOf() != p.degreeOf()) { result = false; } else { // degrees are equal, so verify that corresponding coeffs are too int i=0; // loop invariant: coefficents [0..i) match while (i <= degreeOf() && coefficient(i) == p.coefficient(i)) { i = i+1; } // coefficients [0..i) match (according to the loop invariant), so // if i == degreeOf()+1, all coefficients match; conversely, if // i != degreeOf()+1, it can only be because the loop terminated // "early" upon observing a pair of non-matching coefficients. result = (i == degreeOf()+1); } return result; } /* Returns a string describing this polynomial. ** Example: "3 + -4x^1 + 17x^2 + 0x^3 + 2x^4" ** Note that the resulting string is built from back-to-front. */ public String toString() { String result = ""; for (int j = degreeOf(); j != 0; j--) { result = "+ " + coefficient(j) + "x^" + j + " " + result; } result = coefficient(0) + " " + result; return result; } // mutators // -------- // NONE !!! // generators // ---------- /* Returns a new Polynomial that is the sum of this polynomial ** and the one given (p). */ public Polynomial sum(Polynomial p) { return null; // STUB! } /* Returns a new Polynomial that is the difference of this polynomial ** and the one given (p). */ public Polynomial difference(Polynomial p) { return null; // STUB! } /* Returns a new Polynomail that is the product of this polynomial ** and the given one (p). */ public Polynomial product(Polynomial p) { return null; // STUB! } // clone // ----- /* Returns a clone of this polynomial. */ public Polynomial clone() { return new Polynomial(coeff); } }