net.sourceforge.webcompmath.data
Class Constant

java.lang.Object
  extended by net.sourceforge.webcompmath.data.Constant
All Implemented Interfaces:
java.io.Serializable, Expression, ExpressionCommand, MathObject, Value
Direct Known Subclasses:
Variable, WCMConstantBean

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()
          default constructor, intended only for use in GUI building
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)
          Return 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()
default constructor, intended only for use in GUI building


Constant

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

Parameters:
value - value to use

Constant

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

Parameters:
name - name of constant
value - it's value
Method Detail

getName

public java.lang.String getName()
Return the name of this Constant. It can be null.

Specified by:
getName in interface MathObject
Returns:
name of the constant

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
Parameters:
name - name of the constant

getVal

public double getVal()
Return the value of this Constant.

Specified by:
getVal in interface Value
Returns:
value of the constant

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
Parameters:
cases - the cases to use
Returns:
value of the constant

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
Parameters:
wrt - variable wirh respect to
Returns:
the derivative

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
Returns:
string representation

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
Parameters:
stack - the stack to use
cases - the cases to use

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
Parameters:
prog - program containing the constant
myIndex - location of the constant
deriv - the derivative
wrt - variable with respect to

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
Parameters:
prog - program containing the constant
myIndex - location of the constant
Returns:
number of locations

dependsOn

public boolean dependsOn(Variable x)
Return false, since the value of this Constant is independent of the value of x.

Specified by:
dependsOn in interface Expression
Specified by:
dependsOn in interface ExpressionCommand
Parameters:
x - variable to check if depends on
Returns:
always false for constants

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
Parameters:
prog - program containing the constant
myIndex - location of the constant
buffer - string to append