forked from darius/expr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVariable.java
More file actions
68 lines (59 loc) · 1.52 KB
/
Copy pathVariable.java
File metadata and controls
68 lines (59 loc) · 1.52 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
// Variables associate values with names.
// Copyright 1996 by Darius Bacon; see the file COPYING.
package com.expr;
import java.util.Hashtable;
/**
* A variable is a simple expression with a name (like "x") and a
* settable value.
*/
public class Variable extends Expr {
private static Hashtable<String, Variable> variables = new Hashtable<>();
/**
* Return a unique variable named `name'. There can be only one
* variable with the same name returned by this method; that is,
* make(s1) == make(s2) if and only if s1.equals(s2).
*
* @param name the variable's name
* @return the variable; create it initialized to 0 if it doesn't
* yet exist
*/
static public synchronized Variable make(String name) {
Variable result = variables.get(name);
if (result == null)
variables.put(name, result = new Variable(name));
return result;
}
private String name;
private double val;
/**
* Create a new variable, with initial value 0.
*
* @param name the variable's name
*/
public Variable(String name) {
this.name = name;
val = 0;
}
/**
* Return the name.
*/
public String toString() {
return name;
}
/**
* Get the value.
*
* @return the current value
*/
public double value() {
return val;
}
/**
* Set the value.
*
* @param value the new value
*/
public void setValue(double value) {
val = value;
}
}