/** * Represents the non-constant polynomial type. Holds a postive degree (or order) * and a polynomial of lower order. * @author Dung X. Nguyen */ class NonConstPoly extends APolynomial { /** * Data Invariant: 0 < _degree. */ private int _degree; /** * Data Invariant: _lowerPoly._degree < _degree. * @SBGen Variable (,lower order polynomial,,64) */ private APolynomial _lowerPoly; /** * Checks for legal input parameters and initialize this NonConstPoly to the given * coefficient, degree, and lower order polynomial. * @param coef the coefficient, != 0. * @param degree the degree, > 0. * @param lowPoly the lower ordered polynomial, lowPoly.getDegree () < degree. * @exception throws IllegalArgumentException if conditions on coef, degree and lowerPoly are violated. */ public NonConstPoly (double coef, int degree, APolynomial lowPoly) { if (null == lowPoly) { throw new IllegalArgumentException ("lowPoly must be non-null!"); } if (0.0 == coef) { throw new IllegalArgumentException ("coef must be non-zero!"); } if (degree <= 0) { throw new IllegalArgumentException ("Degree must be positive!"); } if (degree <= lowPoly.getDegree()) { throw new IllegalArgumentException ("lowPoly is not a lower order term!"); } _coef = coef; _degree = degree; _lowerPoly = lowPoly; } public int getDegree () { return _degree; } public APolynomial getLowerPoly () { return _lowerPoly; } /** * Compares this NonConstPoly's own degree with the degree of the APolynomial parameter * p and compute the sum according to the following 3 cases. * p.getDegree () < this.getDegree(): the sum consists of the highest order term of * this NonConstPoly plus the sum of the lower order polynomial with p. * p.getDegree () == this.getDegree(): the coefficient of the highest order term of the sum * is the sum of the two coefficients. The lower order terms of the sum is the sum * of the lower order polynomial of this NonConstPoly and the lower order polynomial of p. * p.getDegree () > this.getDegree(): the sum consists of the highest order term of * p plus the sum of this NonConstPoly with the lower order polynomial of p. */ public APolynomial add (APolynomial p) { double pCoef = p.getLeadCoef(); // avoid making repeated calls to getLeadCoef() int pDegree = p.getDegree(); // and getDegree(). if (pDegree < _degree) { return new NonConstPoly (_coef, _degree, _lowerPoly.add (p)); } if (pDegree == _degree) { double sumCoef = _coef + pCoef; return (0.0 == sumCoef)? _lowerPoly.add (p.getLowerPoly()): new NonConstPoly (sumCoef, _degree, _lowerPoly.add (p.getLowerPoly())); } return new NonConstPoly (pCoef, pDegree, add (p.getLowerPoly())); } /** * Evaluates the leading term then adds the result to the value of the lower order * polynomial at x. */ public double eval (double x) { return _coef * Math.pow (x, _degree) + _lowerPoly.eval (x); } /** * Returns the String ax^n + String representation of the lower order polynomial, where * a is the coefficient and n is the order of this NonConstPoly. */ public String toString () { return Double.toString (_coef) + "x^" + Integer.toString (_degree) + _lowerPoly.toString4LowerPoly (); } /** * Called by an APolynomial that contains this NonConstPoly as its lower order polynomial. * Returns " + " followed by the String representation of this NonConstPoly. */ String toString4LowerPoly () { return " + " + toString (); } }