Building Parsers with Java

sjm.examples.sling
Class Arithmetic

java.lang.Object
  |
  +--sjm.examples.sling.SlingFunction
        |
        +--sjm.examples.sling.Arithmetic

public class Arithmetic
extends SlingFunction

This class wraps an arithmetic function around two source functions.

An Arithmetic object has an operator and two sources. The operator must be '+', '-', '/', '*' or '%' (or else f(t) of this object will always be (0, 0)).

The value of this function at time t is the value of the operator applied to the source functions at time t. For example, evaluating the arithmetic sum f1 + f2 at time t creates the point (f1(t).x + f2(t).x, f1(t).y + f2(t).y).


Field Summary
protected  char operator
           
 
Fields inherited from class sjm.examples.sling.SlingFunction
source
 
Constructor Summary
Arithmetic(char operator)
          Constructs operator(t, t), where operator is one of the symbols +, -, /, *, %.
Arithmetic(char operator, SlingFunction a, SlingFunction b)
          Constructs a function object that is the sum, difference, quotient, product or remainder (depending on the operator) of the supplied functions.
 
Method Summary
protected  double arithmetic(double a, double b)
           
 Point f(double t)
          Returns the sum, difference, quotient, product or remainder (depending on the operator) of this object's source functions.
 java.lang.String toString()
          Returns a string representation of this function.
 
Methods inherited from class sjm.examples.sling.SlingFunction
eval, extrema, fresh
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

operator

protected char operator
Constructor Detail

Arithmetic

public Arithmetic(char operator)
Constructs operator(t, t), where operator is one of the symbols +, -, /, *, %.

Arithmetic

public Arithmetic(char operator,
                  SlingFunction a,
                  SlingFunction b)
Constructs a function object that is the sum, difference, quotient, product or remainder (depending on the operator) of the supplied functions.
Parameters:
operator - the operator to apply, one of the characters in +-/*%
SlingFunction - an operand
SlingFunction - another operand
Method Detail

arithmetic

protected double arithmetic(double a,
                            double b)

f

public Point f(double t)
Returns the sum, difference, quotient, product or remainder (depending on the operator) of this object's source functions.
Overrides:
f in class SlingFunction
Parameters:
t - a number that represents how far along a plot is
Returns:
a new point that applies this object's operator to the values of the source functions at time t

toString

public java.lang.String toString()
Returns a string representation of this function.
Overrides:
toString in class java.lang.Object

by Steve Metsker