aboutsummaryrefslogtreecommitdiff
path: root/Week7 Polynomials/src/polynomial/Term.java
blob: 60c63817faf6694f31450da6037c410306ffa005 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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;
        }
    }
}