-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOperatorNode.java
More file actions
71 lines (59 loc) · 1.64 KB
/
OperatorNode.java
File metadata and controls
71 lines (59 loc) · 1.64 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
/**
* Operator nodes are the internal nodes of a binary expression tree.
*
* @author Maricel Vicente
* @version April 27, 2020
*/
public class OperatorNode extends ExpressionNode {
private Operator op;
public OperatorNode(Operator op) {
super();
this.op = op;
}
public OperatorNode(Operator op, ExpressionNode left, ExpressionNode right) {
super(left, right);
this.op = op;
}
/**
* Evaluate the expression rooted at this node and return the result.
*/
@Override
public double evaluate() {
// UNFINISHED.
double num = 0;
if(op.opString() == "+"){
num = right().evaluate() + left().evaluate();
}
else if(op.opString() == "-"){
num = left().evaluate() - right().evaluate();
}
else if(op.opString() == "*"){
num = right().evaluate() * left().evaluate();
}
else if(op.opString() == "/"){
num = left().evaluate() / right().evaluate();
}
return num;
}
@Override
public String postfixString() {
// UNFINISHED (See superclass javadocs for requirements.)
String leftSubtree = left().postfixString();
String rightSubtree = right().postfixString();
return leftSubtree + rightSubtree + op.opString();
}
@Override
public String prefixString() {
// UNFINISHED
String leftSubtree = left().prefixString();
String rightSubtree = right().prefixString();
return leftSubtree + rightSubtree + op.opString();
}
@Override
public String infixString() {
// UNFINISHED
String leftSubtree = left().infixString();
String rightSubtree = right().infixString();
return leftSubtree + rightSubtree + op.opString();
}
}