public String derive() { String u, du, v, dv; String value = token.getValue(); if (token.isVariable()) { return one; } else if (token.isNumber()) { return zero; } else if (value.equals(plus)) { du = A.derive(); dv = B.derive(); return lB + du + value + dv + rB; } else if (value.equals(minus)) { du = A.derive(); dv = B.derive(); return lB + du + value + dv + rB; } else if (value.equals(times)) { u = A.getExpression(); du = A.derive(); v = B.getExpression(); dv = B.derive(); return lB + lB + u + value + dv + rB + "+" + lB + v + value + du + rB + rB; } else if (value.equals(divide)) { u = A.getExpression(); du = A.derive(); v = B.getExpression(); dv = B.derive(); return lB + lB + lB + v + "*" + du + rB + "-" + lB + u + "*" + dv + rB + rB + value + lB + v + "^2" + rB + rB; } else if (value.equals(power)) { // power is not a function of variable u = A.getExpression(); du = A.derive(); v = B.getExpression(); return lB + v + "*" + u + value + lB + v + "-1" + rB + "*" + du + rB; } return ""; }
private String getExpression() { if (A != null && B != null) { return lB + A.getExpression() + token.getValue() + B.getExpression() + rB; } return token.getValue(); }