/** This class represents and ordered pair (e,de/dx). e is the value of an expression at a particular point, and de/dx is the value of the derivative of that expression evaluated at the same point as the expression. Author: Charlie McDowell */ class ExprDeriv { private double exprValue; private double derivValue; /** Construct an (e, de/dx) object. */ ExprDeriv(double expr, double deriv) { exprValue = expr; derivValue = deriv; } /** The standard string representation. @return This ExprDeriv as a string. */ public String toString() { return "(" + exprValue + ", " + derivValue + ")"; } /** Perform an addition. @param other The right operand of the +. @return A new ExprDeriv representing this + other. */ ExprDeriv add(ExprDeriv other) { return new ExprDeriv(exprValue + other.exprValue, derivValue + other.derivValue); } /** Perform a subtraction. @param other The right operand of the -. @return A new ExprDeriv representing this - other. */ ExprDeriv sub(ExprDeriv other) { return new ExprDeriv(exprValue - other.exprValue, derivValue - other.derivValue); } /** Perform a multiplication. @param other The right operand of the *. @return A new ExprDeriv representing this * other. */ ExprDeriv times(ExprDeriv other) { return new ExprDeriv(exprValue * other.exprValue, derivValue*other.exprValue + other.derivValue*exprValue); } /** Perform a division. @param other The right operand of the /. @return A new ExprDeriv representing this / other. */ ExprDeriv divide(ExprDeriv other) { if (exprValue == 0 && other.exprValue == 0) return new ExprDeriv(derivValue / other.derivValue, 0); else { ExprDeriv temp = new ExprDeriv(exprValue / other.exprValue, (derivValue * other.exprValue - exprValue * other.derivValue) / (other.exprValue*other.exprValue)); return temp; } } }