Class Variable

java.lang.Object
  |
  +--Variable

public class Variable
extends java.lang.Object

The Variable class allows the user to represent a mathematical variable. A variable has a name and a value. For example, X + 3 * X = Y is a mathematical equation. The characters X and Y are mathematical variables which represent numeric values. Similarly, density = mass / volume is a mathematical equation with the variables density, mass and volume. Because in the equation Z = (X + 3) * X the two X's are not in fact different variables, but represent different references to the same value, each Variable object does not maintain its own value. Rather there is a static member VariableDereferencer de which maintains a list of all variable names and maintains a value for each.


Field Summary
protected static VariableDereferencer d
          An object which stores the values of the variables.This in an implementer of VariableDereferencer which stores the values for all of the variables.
 java.lang.String name
          The name of the variable in the dereferencer.
 
Constructor Summary
Variable()
          A default constructor.
Variable(java.lang.String s)
          Instantiates a variable with just a name.
Variable(java.lang.String s, int value)
          This contructor allows a variable to be given an intial value.
 
Method Summary
static VariableDereferencer dereferencer()
           
 int hashCode()
          Returns the hash code from String for the name of the current Variable.
 int intValue()
          Returns the same as valueOf().
 boolean isFalse()
          A Variable operates along the same lines as an integer variable in C.
 boolean isTrue()
          A Variable operates along the same lines as an integer variable in C.
static void setDereferencer()
          This sets the object which is responsible for storing the values of the variables and returning them.
static void setDereferencer(VariableDereferencer de)
          This sets the object which is responsible for storing the values of the variables and returning them.
 void setValue(int i)
          Assigns a value to the reference in the VariableDereferencer with the same name as the current name.
 java.lang.String toString()
          Returns a string containing the name of the variable followed by its value.
 int valueOf()
          Returns the current value of the variable.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

d

protected static VariableDereferencer d
An object which stores the values of the variables.This in an implementer of VariableDereferencer which stores the values for all of the variables.

name

public java.lang.String name
The name of the variable in the dereferencer.
Constructor Detail

Variable

public Variable()
A default constructor. By default a variable exists only as a reference. It has no name and no value.

Variable

public Variable(java.lang.String s)
Instantiates a variable with just a name. If a variable is instantiated without a value specified its value is to set to 0.
Parameters:
s - a string representing the name of the variable

Variable

public Variable(java.lang.String s,
                int value)
This contructor allows a variable to be given an intial value. If a variable has already been created with the same name then that value will be overwritten in the dereferncer. There may only be one variable with a given name.
Parameters:
s - a string representing the name of the variable
value - an initial value for the variable
Method Detail

valueOf

public int valueOf()
Returns the current value of the variable. If the variable has no name or if there is no dereferencer set then it returns 0. This will eventually be replaced with a VariableNotFoundException.
Returns:
value of the current variable

intValue

public int intValue()
Returns the same as valueOf(). Added to make the Variable useable in a similar contect to Integer.
Returns:
valueOf()

hashCode

public int hashCode()
Returns the hash code from String for the name of the current Variable.
Returns:
interger useful for implementing a hash table
Overrides:
hashCode in class java.lang.Object
See Also:
String.hashCode()

setValue

public void setValue(int i)
Assigns a value to the reference in the VariableDereferencer with the same name as the current name. If the name is not found in the dereferencer then a new VariableReference is added and new value assigned.
Parameters:
i - value to be assigned

setDereferencer

public static void setDereferencer()
This sets the object which is responsible for storing the values of the variables and returning them. VariableDereferencer is an interface and any object may implement it. By default the variable dereferencer is set to be a VariableTable.

setDereferencer

public static void setDereferencer(VariableDereferencer de)
This sets the object which is responsible for storing the values of the variables and returning them. VariableDereferencer is an interface and any object may implement it.
Parameters:
de - class implementing VariableDereferencer to store variable values

isTrue

public boolean isTrue()
A Variable operates along the same lines as an integer variable in C. If the value is 0 then it is considered false, otherwise it is considered true.
Returns:
the boolean representation of the current value

isFalse

public boolean isFalse()
A Variable operates along the same lines as an integer variable in C. If the value is 0 then it is considered false, otherwise it is considered true.
Returns:
the inverse of the boolean representation of the current value

toString

public java.lang.String toString()
Returns a string containing the name of the variable followed by its value.
Returns:
String of the form: name: [value]
Overrides:
toString in class java.lang.Object

dereferencer

public static VariableDereferencer dereferencer()
Returns:
the current VariableDereferencer