-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTerm.java
48 lines (39 loc) · 1.08 KB
/
Term.java
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
package linkedListPolynomials;
class Term {
Double coefficient;
int degree;
public Term() {
this(null, 0);
}
public boolean isPositive() {
return coefficient > 0;
}
public Term(Double coefficient, int degree) {
this.coefficient = coefficient;
this.degree = degree;
}
public Double getCoefficient() {
return coefficient;
}
public void setCoefficient(Double coefficient) {
this.coefficient = coefficient;
}
public int getDegree() {
return degree;
}
public void setDegree(int degree) {
this.degree = degree;
}
public String toString() {
String ans = "";
if (coefficient.doubleValue() == 0) return "";
if (degree == 0) return coefficient.toString();
if (coefficient != 1) {
if (coefficient == -1) ans += "-";
else ans += coefficient + " ";
}
ans = ans + "X";
if (degree == 1) return ans;
return ans + "^" + degree;
}
}