-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSymbol.java
More file actions
57 lines (48 loc) · 1.38 KB
/
Copy pathSymbol.java
File metadata and controls
57 lines (48 loc) · 1.38 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
public class Symbol{
public double coefficient;
public int power;
public static void main(String[] args){
Symbol term = new Symbol(3,2);
term.print();
}
public Symbol(double coeff, int pow){
this.coefficient = coeff;
this.power = pow;
}
public void print(){
System.out.print(this.coefficient);
System.out.print("X^");
System.out.print(this.power);
}
public void setValues(double coeff, int pow){
this.coefficient = coeff;
this.power = pow;
}
public Symbol multiply(Symbol B){
double newCoeff = this.coefficient * B.coefficient;
int newPow = this.power + B.power;
return new Symbol(newCoeff,newPow);
}
public boolean addTo(Symbol B){
double oldCoeff = B.coefficient;
if (B.power==this.power){
B.coefficient = oldCoeff + this.coefficient;
return true;
}
else{return false;}
}
public boolean add(Symbol B){
double oldCoeff = B.coefficient;
if (B.power==this.power){
this.coefficient = oldCoeff + this.coefficient;
return true;
}
else{return false;}
}
}
class SymbolComparator implements java.util.Comparator<Symbol> {
@Override
public int compare(Symbol a, Symbol b) {
return a.power - b.power;
}
}