import java.util.HashMap; /** * Class for representing a simple variable table, mapping variables to their values. * * @author Dave Reed * @version 2/9/07 */ public class VariableTable { private HashMap varMap; /** * Constructs an empty variable table, mapping variable names to values. */ public VariableTable() { varMap = new HashMap(); } /** * Assigns a new value to a variable in the table. * @param var the variable name to be assigned to * @param val the value to be assigned to var */ public void assign(String var, int val) { varMap.put(var, val); } /** * Looks up the value of a variable in the table, assigning that variable * to be 0 if it is not already present * @param var the variable to be looked up * @return the value associated with that variable */ public int lookup(String var) { if (!varMap.containsKey(var)) { this.assign(var, 0); } return varMap.get(var); } }