-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInterpreter.java
More file actions
55 lines (44 loc) · 1.1 KB
/
Interpreter.java
File metadata and controls
55 lines (44 loc) · 1.1 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
interface Expression {
boolean interpret(String context);
}
class TerminalExpression implements Expression {
String data;
public TerminalExpression(String data) {
this.data = data;
}
public boolean interpret(String context) {
if (context.contains(data)) {
return true;
}
else {
return false;
}
}
}
class OrExpression implements Expression {
Expression expr1, expr2;
public OrExpression(Expression expr1, Expression expr2) {
this.expr1 = expr1;
this.expr2 = expr2;
}
public boolean interpret(String context) {
return expr1.interpret(context) || expr2.interpret(context);
}
}
class AndExpression implements Expression {
Expression expr1, expr2;
public AndExpression(Expression expr1, Expression expr2) {
this.expr1 = expr1;
this.expr2 = expr2;
}
public boolean interpret(String context) {
return expr1.interpret(context) && expr2.interpret(context);
}
}
class Test {
public static void main(String[] args) {
Expression expr1 = new TerminalExpression("John");
Expression expr2 = new TerminalExpression("Tom");
Expression expr3 = new OrExpression(expr1, expr2);
}
}