operators
Class PSAdd

java.lang.Object
  extended byoperators.PSAdd
All Implemented Interfaces:
PSArithmeticOperator, PSObject, PSOperator

public class PSAdd
extends java.lang.Object
implements PSArithmeticOperator

Implements PSArithmeticOperator interface.

Methods such as getOperatorToken() are inherited by PSArithmenticOperator interface which in turn, are inherited by PSOperator.This concrete class is responsible for the addittion of two numbers found on the top of the stack.

Before the addition,action() checks : First,there have to be at least two elements inside the stack. Second, both operands must be numbers,ie both types are PSNumber.Last,the addition is performed.


Constructor Summary
PSAdd()
           
 
Method Summary
 void action(PSStack s)
          Performs addition on two operands.
 java.lang.String getOperatorToken()
          Returns the token identifying this operator.
 boolean isValid(PSStack s)
          Checks if it valid to use this operator now.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PSAdd

public PSAdd()
Method Detail

getOperatorToken

public java.lang.String getOperatorToken()
Returns the token identifying this operator.

Returns the name of the operand that this concrete class simulates,here "add".

Precondition : A valid operator class has been created.
Postcondition : A string is returned,labeling the semantics of this class.

Specified by:
getOperatorToken in interface PSOperator
Returns:
The token identifying this operator.

isValid

public boolean isValid(PSStack s)
                throws PSSyntaxException
Checks if it valid to use this operator now.

The addition can occur only if the stack contains two objects,both being of type PSNumber.

Precondition : A valid instance of PSStack is given.
Postcondition : A reply to the question whether the addition can occur.

Specified by:
isValid in interface PSOperator
Parameters:
s - A reference to a PSStack instance.
Returns:
True if the operation can be performed,false otherwise.
Throws:
PSSyntaxException - The syntax of the token is wrong.

action

public void action(PSStack s)
Performs addition on two operands.

Perfoms the addition upon the top two elements of the stack .

Precondition : A valid stack is given to method with at least two PSNumber operands.
Postcondition : Addition will be performed and the result will be stored back to the stack.

Specified by:
action in interface PSArithmeticOperator
Parameters:
s - The Run-time Stack where the operands (here literals) are retrieved and stored.