-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVariable.java
More file actions
47 lines (39 loc) · 942 Bytes
/
Variable.java
File metadata and controls
47 lines (39 loc) · 942 Bytes
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
package expression;
public class Variable implements BothExpressions {
public final String name;
private final int hash;
public Variable(String name) {
this.name = name;
hash = name.hashCode();
}
public int evaluate(int x) {
return x;
}
public int evaluate(int x, int y, int z) {
return switch(name) {
case("x") -> x;
case("y") -> y;
case("z") -> z;
default -> 0;
};
}
@Override
public String toString() {
return name;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (obj.getClass() == this.getClass()) {
Variable other = (Variable) obj;
return other.name.equals(this.name);
}
return false;
}
@Override
public int hashCode() {
return hash;
}
}