/** * Write a description of class AddSubPoly here. * * @author (your name) * @version (a version number or a date) */ public class AddSubPoly extends DiffPoly { public AddSubPoly(String variable) { super(variable); } public AddSubPoly(String variable, double[] newcoeffs) { super(variable, newcoeffs); } public AddSubPoly add(DiffPoly poly) { if (this.variable.equals(poly.variable)) { if (this.coeffs.length == 1) { double[] tempcoeffs = new double[poly.coeffs.length]; for (int i = poly.coeffs.length - 1; i >= 0; i--) tempcoeffs[i] = poly.coeffs[i]; tempcoeffs[3] += this.coeffs[0]; return new AddSubPoly(variable, tempcoeffs); } else if (poly.coeffs.length == 1) { double[] tempcoeffs = new double[this.getDegree()]; for (int i = this.getDegree() - 1; i >= 0; i--) tempcoeffs[i] = this.coeffs[i]; tempcoeffs[3] += poly.coeffs[0]; return new AddSubPoly(variable, tempcoeffs); } else if (this.getDegree() > poly.getDegree()) { double[] tempcoeffs = new double[this.getDegree()]; for (int i = this.getDegree(); i >= 0; i--) tempcoeffs = this.coeffs; return new AddSubPoly(variable, tempcoeffs); } else if (this.getDegree() < poly.getDegree()) { double[] tempcoeffs = new double[poly.getDegree()]; for (int i = this.getDegree(); i >= 0; i--) tempcoeffs[i] = poly.coeffs[i] + this.coeffs[i]; return new AddSubPoly(variable, tempcoeffs); } else if (this.getDegree() == poly.getDegree()) { double[] tempcoeffs = new double[this.coeffs.length]; for (int i = this.coeffs.length - 1; i >= 0; i--) tempcoeffs[i] = poly.coeffs[i] + this.coeffs[i]; if (tempcoeffs[3] == 0.0) { double[] shorter = new double[tempcoeffs.length - 1]; for (int i = shorter.length - 1; i >= 0; i--) shorter[i] = tempcoeffs[i]; if ((shorter[0] == 0.0) && (shorter[1] == 0.0) && (shorter[2] == 0.0)) return new AddSubPoly(variable); return new AddSubPoly(variable, shorter); } return new AddSubPoly(variable, tempcoeffs); } } else throw new IllegalArgumentException(); return new AddSubPoly(variable); } public AddSubPoly sub(DiffPoly poly) { double[] temp2 = new double[poly.coeffs.length]; for (int i = poly.coeffs.length - 1; i >= 0; i--) temp2[i] = -1 * poly.coeffs[i]; //AddSubPoly poly1 = new AddSubPoly(this.variable, this.coeffs); AddSubPoly poly2 = new AddSubPoly(poly.variable, temp2); AddSubPoly result = this.add(poly2); return result; } }