-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPolynomial.java
More file actions
82 lines (67 loc) · 2.19 KB
/
Copy pathPolynomial.java
File metadata and controls
82 lines (67 loc) · 2.19 KB
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
import java.util.ArrayList;
import java.util.Collections;
public class Polynomial{
public ArrayList<Symbol> terms;
public static void main(String[] args) {
Symbol term1 = new Symbol(3,2);
Symbol term2 = new Symbol(1,3);
Symbol term3 = new Symbol(2,9);
Symbol term4 = new Symbol(6,1);
Symbol term5 = new Symbol(4,0);
Symbol term6 = new Symbol(2.6,3);
Polynomial expression = new Polynomial();
expression.addTerm(term1);
expression.addTerm(term2);
expression.addTerm(term3);
expression.addTerm(term4);
expression.addTerm(term5);
expression.addTerm(term6);
expression.print();
expression.sort();
expression.print();
Polynomial groupedExpression = expression.groupTerms();
groupedExpression.print();
}
public Polynomial(){
this.terms = new ArrayList<Symbol>();
}
public Polynomial(ArrayList<Symbol> x){
this.terms = x;
}
public void print(){
for (int i = 0; i < this.terms.size(); i++){
this.terms.get(i).print();
System.out.print(" + ");
}
System.out.println();
}
public void addTerm(Symbol x){
terms.add(x);
}
public Polynomial groupTerms(){
int prevPower = -1;// -1 so it does not equal the power of first term because first term is minimum 1
ArrayList<Symbol> newTerms = new ArrayList<Symbol>();
for(int i = 0; i < this.terms.size(); i++){
if (prevPower == this.terms.get(i).power){
newTerms.getLast().add(this.terms.get(i));
}
else{
newTerms.add(this.terms.get(i));
}
prevPower = this.terms.get(i).power;
}
return new Polynomial(newTerms);
}
public boolean equals(Polynomial P){
if (this.terms.size() != P.terms.size()){return false;}
for (int i = 0; i<this.terms.size();i++){
if (this.terms.get(i) != P.terms.get(i)){
return false;
}
}
return true;
}
public void sort(){
Collections.sort(this.terms, new SymbolComparator());
}
}