edu.hws.jcm.data
Class Constant

java.lang.Object
  |
  +--edu.hws.jcm.data.Constant
All Implemented Interfaces:
Expression, ExpressionCommand, MathObject, java.io.Serializable, Value
Direct Known Subclasses:
Variable

public class Constant
extends java.lang.Object
implements Expression, ExpressionCommand, MathObject

A Constant is a Value that represents a constant real number. (The value doesn't have to be constant in sub-classes, since the member that stores the value is protected, not private.) A Constant doesn't necessarily need a name. If the name is null, then the print string for the Constant is the value of the constant. If it has a non-null name, then the print string is the name. (Note that, as for any MathObject, if the name is null, than the Constant can't be added to a Parser.) Constant objects are used to represent the mathematical constants pi and e. A Constant is both an Expression and an ExpressionCommand. Since it is an ExpressionCommand, it can occur as a command in an ExpressionProgram. In that case, it simply represens a named constant occurs in an expression.

See Also:
Serialized Form

Field Summary
protected  double value
          The value of this Constant.
 
Constructor Summary
Constant(double value)
          Create an unnamed Constant with the given value and null name.
Constant(java.lang.String name, double value)
          Create a Constant with the given name and value.
 
Method Summary
 void appendOutputString(ExpressionProgram prog, int myIndex, java.lang.StringBuffer buffer)
          Append the print string for this Constant to the buffer.
 void apply(StackOfDouble stack, Cases cases)
          Apply the Constant to the stack.
 void compileDerivative(ExpressionProgram prog, int myIndex, ExpressionProgram deriv, Variable wrt)
          Add a commands to deriv to evaluate the derivative of this Constant with respect to the variable.
 boolean dependsOn(Variable x)
          Retrun false, since the value of this Constant is independent of the value of x.
 Expression derivative(Variable wrt)
          Return the derivative of this Constant with respect to the variable wrt.
 int extent(ExpressionProgram prog, int myIndex)
          Return the number of locations that this Constant uses in the program.
 java.lang.String getName()
          Return the name of this Constant.
 double getVal()
          Return the value of this Constant.
 double getValueWithCases(Cases cases)
          Return the value of the Constant.
 void setName(java.lang.String name)
          Set the name of this Constant.
 java.lang.String toString()
          Return the print string representing this Constant.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

value

protected double value
The value of this Constant.
Constructor Detail

Constant

public Constant(double value)
Create an unnamed Constant with the given value and null name.

Constant

public Constant(java.lang.String name,
                double value)
Create a Constant with the given name and value. The name can be null.
Method Detail

getName

public java.lang.String getName()
Return the name of this Constant. It can be null.
Specified by:
getName in interface MathObject

setName

public void setName(java.lang.String name)
Set the name of this Constant. (Note that this should not be done if the Constant has been registered with a Parser.)
Specified by:
setName in interface MathObject

getVal

public double getVal()
Return the value of this Constant.
Specified by:
getVal in interface Value

getValueWithCases

public double getValueWithCases(Cases cases)
Return the value of the Constant. Since a constant is continuous function, there is only one "case", so no case information needs to be recorded in cases.
Specified by:
getValueWithCases in interface Expression

derivative

public Expression derivative(Variable wrt)
Return the derivative of this Constant with respect to the variable wrt. The derivative is another Constant with value zero.
Specified by:
derivative in interface Expression

toString

public java.lang.String toString()
Return the print string representing this Constant. The string is the name of the constant, if that is non-null. Otherwise, it is the value of the constant.
Specified by:
toString in interface Expression
Overrides:
toString in class java.lang.Object

apply

public void apply(StackOfDouble stack,
                  Cases cases)
Apply the Constant to the stack. This is done by pushing the value of the constant onto the stack. The evaluation of a constant doesn't have any "cases", so there is no need to record any information in cases.
Specified by:
apply in interface ExpressionCommand
Following copied from interface: edu.hws.jcm.data.ExpressionCommand
Parameters:
stack - contains results of previous commands in the program.
cases - if non-null, any case information generated during evaluation should be recorded here.

compileDerivative

public void compileDerivative(ExpressionProgram prog,
                              int myIndex,
                              ExpressionProgram deriv,
                              Variable wrt)
Add a commands to deriv to evaluate the derivative of this Constant with respect to the variable. The derivative is 0, so the only command is the constant 0 (which really represents the stack operation "push 0"). The program and the position of the Constant in that program are irrelevant.
Specified by:
compileDerivative in interface ExpressionCommand
Following copied from interface: edu.hws.jcm.data.ExpressionCommand
Parameters:
prog - program in which ExpressionCommand occurs.
myIndex - point at which ExpressionCommand occurs in the ExpressionProgram.
deriv - the derivative of the ExpressionPorgram prog, which is in the process of being computed. Commands should added to deriv that will compute the derivative of this ExpressionCommand.
wrt - commands are added to deriv with respect to this Variable.

extent

public int extent(ExpressionProgram prog,
                  int myIndex)
Return the number of locations that this Constant uses in the program. The value is always 1, since the constant is a complete sub-expression in itself.
Specified by:
extent in interface ExpressionCommand
Following copied from interface: edu.hws.jcm.data.ExpressionCommand
Parameters:
prog - ExpressionProgram in which this ExpressionCommand occurs.
myIndex - index at which ExpressionCommand occurs in prog.
Returns:
total number of indices in prog occupied by this command and commands that generate data used by this command.

dependsOn

public boolean dependsOn(Variable x)
Retrun false, since the value of this Constant is independent of the value of x.
Specified by:
dependsOn in interface Expression

appendOutputString

public void appendOutputString(ExpressionProgram prog,
                               int myIndex,
                               java.lang.StringBuffer buffer)
Append the print string for this Constant to the buffer. (The values of prog and myIndex are irrelevant.)
Specified by:
appendOutputString in interface ExpressionCommand