/** * Term models terms in a polynomial. * * @author Lyndon While * @version 1.0 */ public class Term { // the term = coefficient * x ^ exponent private double coefficient; private int exponent; public Term(double c, int e) { coefficient = c; exponent = e; } // returns the coefficient public double getCoefficient() { return coefficient; } // returns the exponent public int getExponent() { return exponent; } // returns the term as a simple String for display: // sign + coefficient + x^ + exponent public String displaySimple() { // TODO String sign; if (coefficient < 0) sign = "- "; else sign = "+ "; return sign + Math.abs(coefficient) + "x^" + exponent; } // returns the term as a String for display: // see the sample file and the test program for the layout required public String displayImproved() { // TODO if (coefficient == 0) return "0"; String soln; if (coefficient < 0) soln = "- "; else soln = "+ "; double c = Math.abs(coefficient); String cs; if (c == (int)c) cs = "" + (int) c; else cs = "" + c; if (exponent == 0) return soln + cs; if (c != 1) soln += cs; soln += "x"; if (exponent == 1) return soln; else return soln + "^" + exponent; } }