package polynomial; import java.util.Scanner; /** * For representing terms in a polynomial. * * @author Sjaak Smetsers * @version 1.0 * @date 15-02-2012 * * @author Camil Staps, s4498062 * @date 17-03-2015 */ public class Term { /** * Each term consists of a coefficient and an exponent */ private double coefficient; private int exponent; /** * a get-method for the exponent * * @return exponent */ public int getExp() { return exponent; } /** * a get-method for the coefficient * * @return coefficient */ public double getCoef() { return coefficient; } /** * A two-argument constructor * * @param c the value for the coefficient * @param e the value for the exponent */ public Term(double c, int e) { coefficient = c; exponent = e; } /** * A copy-constructor * * @param t the term to be copied */ public Term(Term t) { this(t.coefficient, t.exponent); } /** * For adding two terms with equal exponents * * @param t the term added to this * @require exponent == t.exponent */ public void plus(Term t) { coefficient += t.coefficient; } /** * For multiplying two terms * * @param t the multiplier */ public void times(Term t) { exponent += t.exponent; coefficient *= t.coefficient; } /** * Converts a term into a readable representation the standard format is * %fx^%d * I changed the implementation to remove trailing zeroes from the coefficient. * * @return the string representing the term */ @Override public String toString() { String nice_coefficient = Double.toString(coefficient); if (Math.round(coefficient) == coefficient) nice_coefficient = Integer.toString((int) coefficient); if (exponent == 0) { return String.format("%s", nice_coefficient, exponent); } else if (exponent == 1) { return String.format("%sx", nice_coefficient, exponent); } else { return String.format("%sx^%d", nice_coefficient, exponent); } } /** * Standard implementation of equality * @param may_be_term the object to check equality with * @return true iff may_be_term represents the same term as this term */ @Override public boolean equals(Object may_be_term) { if (may_be_term == null || getClass() != may_be_term.getClass()) { return false; } else { Term term = (Term) may_be_term; return exponent == term.exponent && coefficient == term.coefficient; } } /** * Apply a term to a concrete variable * @param x the variable to apply the term to * @return the result of the application of the term to x */ public double apply(double x) { return coefficient * Math.pow(x, exponent); } /** * A static method for converting scanner input into a term. The expected * format is two numbers (coef, exp) separated by whitespaces. The coef * should be either an integer or a decimal number. The exp is an integer. * * @param s the scanner providing the input * @return null if no term could be found, the found term otherwise */ public static Term scanTerm(Scanner s) { String coef_patt = "\\-?\\d+(\\.\\d+)?"; if (s.hasNext(coef_patt)) { String coef = s.next(coef_patt); int exp = s.nextInt(); return new Term(Double.parseDouble(coef), exp); } else { return null; } } }