Class Operation

java.lang.Object
calculator.operations.Operation
All Implemented Interfaces:
Expression
Direct Known Subclasses:
Divides, Minus, Plus, Power, Times

public abstract class Operation extends Object implements Expression
Operation is an abstract class that represents arithmetic operations, which are a special kind of Expressions, just like numbers are.
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    The list of expressions passed as an argument to the arithmetic operation
    protected int
    The neutral element of the operation (e.g. 1 for *, 0 for +)
    protected String
    The character used to represent the arithmetic operation (e.g. "+", "*")
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    To construct an operation with a list of expressions as arguments, as well as the Notation used to represent the operation.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Accept method to implement the visitor design pattern to traverse arithmetic expressions.
    void
    Add more parameters to the existing list of parameters
    boolean
    Two operation objects are equal if their list of arguments is equal and they correspond to the same operation.
    getter method to return the number of arguments of an arithmetic operation.
    getter method to return the symbol of the arithmetic operation.
    int
    The method hashCode needs to be overridden it the equals method is overridden; otherwise there may be problems when you use your object in hashed collections such as HashMap, HashSet, LinkedHashSet.
    abstract Complex
    op(Complex c1, Complex c2)
    Abstract method representing the actual binary arithmetic operation to compute on Complexes
    abstract Atom
    Abstract method representing the actual binary arithmetic operation to compute on Integer
    abstract Atom
    Abstract method representing the actual binary arithmetic operation to compute on Rationnals
    abstract Real
    op(Real r1, Real r2)
    Abstract method representing the actual binary arithmetic operation to compute on Reals
    final String
    Convert the arithmetic operation into a String to allow it to be printed, using the default printer
    final String
    Appeal to the visitor to convert the arithmetic operation into a String to allow it to be printed, using the notation n (prefix, infix or postfix) that is specified as a parameter.

    Methods inherited from class Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • args

      public List<Expression> args
      The list of expressions passed as an argument to the arithmetic operation
    • symbol

      protected String symbol
      The character used to represent the arithmetic operation (e.g. "+", "*")
    • neutral

      protected int neutral
      The neutral element of the operation (e.g. 1 for *, 0 for +)
  • Constructor Details

    • Operation

      protected Operation(List<Expression> elist) throws IllegalConstruction
      To construct an operation with a list of expressions as arguments, as well as the Notation used to represent the operation.
      Parameters:
      elist - The list of expressions passed as argument to the arithmetic operation
      Throws:
      IllegalConstruction - Exception thrown if a null list of expressions is passed as argument
  • Method Details

    • getArgs

      public List<Expression> getArgs()
      getter method to return the number of arguments of an arithmetic operation.
      Returns:
      The number of arguments of the arithmetic operation.
    • getSymbol

      public String getSymbol()
      getter method to return the symbol of the arithmetic operation.
      Returns:
      The symbol of the arithmetic operation (e.g. "+", "-", "*", "/").
    • op

      public abstract Real op(Real r1, Real r2)
      Abstract method representing the actual binary arithmetic operation to compute on Reals
      Parameters:
      r1 - first Real of the binary operation
      r2 - second Real of the binary operation
      Returns:
      result of computing the binary operation
    • op

      public abstract Complex op(Complex c1, Complex c2)
      Abstract method representing the actual binary arithmetic operation to compute on Complexes
      Parameters:
      c1 - first Complex of the binary operation
      c2 - second Complex of the binary operation
      Returns:
      result of computing the binary operation
    • op

      public abstract Atom op(IntegerAtom i1, IntegerAtom i2)
      Abstract method representing the actual binary arithmetic operation to compute on Integer
      Parameters:
      i1 - first Integer of the binary operation
      i2 - second Integer of the binary operation
      Returns:
      result of computing the binary operation
    • op

      public abstract Atom op(Rationnal q1, Rationnal q2)
      Abstract method representing the actual binary arithmetic operation to compute on Rationnals
      Parameters:
      q1 - first Rationnal of the binary operation
      q2 - second Rationnal of the binary operation
      Returns:
      result of computing the binary operation
    • addMoreParams

      public void addMoreParams(List<Expression> params)
      Add more parameters to the existing list of parameters
      Parameters:
      params - The list of parameters to be added
    • accept

      public void accept(Visitor v)
      Accept method to implement the visitor design pattern to traverse arithmetic expressions. Each operation will delegate the visitor to each of its arguments expressions, and will then pass itself to the visitor object to get processed by the visitor object.
      Specified by:
      accept in interface Expression
      Parameters:
      v - The visitor object
    • toString

      public final String toString()
      Convert the arithmetic operation into a String to allow it to be printed, using the default printer
      Overrides:
      toString in class Object
      Returns:
      The String that is the result of the conversion.
    • toString

      public final String toString(Notation n)
      Appeal to the visitor to convert the arithmetic operation into a String to allow it to be printed, using the notation n (prefix, infix or postfix) that is specified as a parameter.
      Parameters:
      n - The notation to be used for representing the operation (prefix, infix or postfix)
      Returns:
      The String that is the result of the conversion.
    • equals

      public boolean equals(Object o)
      Two operation objects are equal if their list of arguments is equal and they correspond to the same operation.
      Overrides:
      equals in class Object
      Parameters:
      o - The object to compare with
      Returns:
      The result of the equality comparison
    • hashCode

      public int hashCode()
      The method hashCode needs to be overridden it the equals method is overridden; otherwise there may be problems when you use your object in hashed collections such as HashMap, HashSet, LinkedHashSet.
      Overrides:
      hashCode in class Object
      Returns:
      The result of computing the hash.