Class Printer


public class Printer extends Visitor
Printer is a Visitor that converts arithmetic expressions into Strings, using a specified notation (prefix, infix or postfix). It is used in the Calculator class to print arithmetic expressions in a human-readable format.
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructor of the Printer class, which initializes the notation to infix by default.
    Printer(Notation notation)
    Constructor of the Printer class, which takes a Notation as a parameter to specify the notation to be used for printing expressions.
  • Method Summary

    Modifier and Type
    Method
    Description
    Getter method to obtain the string representation of the expression that has been built by the visit methods.
    void
    The Visitor can traverse a Complex number (a subtype of Expression)
    void
    The Visitor can traverse an Integer (a subtype of Expression)
    void
    The Visitor can traverse a Rationnal number (a subtype of Expression)
    void
    The visit methods for Real.
    void
    The Visitor can traverse a binary function (a subtype of Expression)
    void
    The Visitor can traverse an unary function (a subtype of Expression)
    void
    The visit method for Operation.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Printer

      public Printer(Notation notation)
      Constructor of the Printer class, which takes a Notation as a parameter to specify the notation to be used for printing expressions.
      Parameters:
      notation - The notation to be used for printing expressions (prefix, infix or postfix)
    • Printer

      public Printer()
      Constructor of the Printer class, which initializes the notation to infix by default.
  • Method Details

    • visit

      public void visit(Real r)
      The visit methods for Real. These methods build the string representation of the expression being visited, using the specified notation.
      Specified by:
      visit in class Visitor
      Parameters:
      r - The Real to be visited
    • visit

      public void visit(IntegerAtom i)
      Description copied from class: Visitor
      The Visitor can traverse an Integer (a subtype of Expression)
      Specified by:
      visit in class Visitor
      Parameters:
      i - The operation being visited
    • visit

      public void visit(Complex c)
      Description copied from class: Visitor
      The Visitor can traverse a Complex number (a subtype of Expression)
      Specified by:
      visit in class Visitor
      Parameters:
      c - The Real number being visited
    • visit

      public void visit(Rationnal q)
      Description copied from class: Visitor
      The Visitor can traverse a Rationnal number (a subtype of Expression)
      Specified by:
      visit in class Visitor
      Parameters:
      q - The Real number being visited
    • visit

      public void visit(Operation o)
      The visit method for Operation. Needs to handle the different notations (prefix, infix and postfix) and the different number of arguments that an operation can have. It uses helper methods visitArgs and visitArgsInfix to handle the different notations.
      Specified by:
      visit in class Visitor
      Parameters:
      o - The Operation to be visited
    • visit

      public void visit(UnaryFunction o)
      Description copied from class: Visitor
      The Visitor can traverse an unary function (a subtype of Expression)
      Specified by:
      visit in class Visitor
      Parameters:
      o - The unary function being visited
    • getResult

      public String getResult()
      Getter method to obtain the string representation of the expression that has been built by the visit methods.
      Returns:
      The string representation of the expression that has been built by the visit methods
    • visit

      public void visit(BinaryFunction f)
      Description copied from class: Visitor
      The Visitor can traverse a binary function (a subtype of Expression)
      Specified by:
      visit in class Visitor
      Parameters:
      f - The binary function being visited