Package cs2110

Class RpnCalc

java.lang.Object
cs2110.RpnCalc

public class RpnCalc extends Object
An interactive calculator for evaluating expressions represented in Reverse Polish Notation.
  • Constructor Summary Link icon

    Constructors
    Constructor
    Description
    Create a new calculator object with no variables set and an initial expression of "0" that understands the core math functions defined in `UnaryFunction`.
  • Method Summary Link icon

    Modifier and Type
    Method
    Description
    void
    Consume the first token in `scanner`, interpret it as a command name, and execute the appropriate command handler.
    void
    doClear(Scanner scanner)
    Parse remaining arguments in `scanner` and perform the "clear" command to clear all variable assignments in this calculator instance.
    void
    doDef(Scanner scanner)
    Parse remaining arguments in `scanner` and perform the "def" command to define a new function equivalent to the current expression.
    void
    doDefs(Scanner scanner)
    Parse remaining arguments in `scanner` and perform the "defs" command to print all function definitions in this calculator instance.
    void
    doDeps(Scanner scanner)
    Perform the "deps" command to print the names of variables that the current expression depends on.
    void
    doEval(Scanner scanner)
    Perform the "eval" command to evaluate the current expression and print the result.
    void
    doHelp(Scanner scanner)
    Print the commands understood by this calculator, their arguments (in angle brackets, with additional square brackets around optional arguments), and a brief description of the commands' effects.
    void
    doInfix(Scanner scanner)
    Perform the "infix" command to print the current expression in infix notation.
    void
    doOpcount(Scanner scanner)
    Perform the "opcount" command to print the number of operations required to evaluate the current expression.
    void
    Perform the "optimize" command to replace the current expression with its optimized form, propagating constant subexpressions based on the current variable assignments.
    void
    doPostfix(Scanner scanner)
    Perform the "postifx" command to print the current expression in postfix (RPN) notation.
    void
    doSet(Scanner scanner)
    Parse remaining arguments in `scanner` and perform the "set" command to assign a value to a variable in this calculator instance.
    void
    Parse remaining arguments in `scanner` and perform the "tabulate" command to evaluate the current expression at a range of abscissa and print each abscissa and ordinate value.
    void
    doUnset(Scanner scanner)
    Parse remaining arguments in `scanner` and perform the "unset" command to unassign a value from a variable in this calculator instance.
    void
    doVars(Scanner scanner)
    Parse remaining arguments in `scanner` and perform the "vars" command to print all variable assignments in this calculator instance.
    boolean
    Return whether the user has issued an 'exit' command.
    static void
    main(String[] args)
    Run an interactive calculator application.

    Methods inherited from class java.lang.Object Link icon

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

    • RpnCalc Link icon

      public RpnCalc()
      Create a new calculator object with no variables set and an initial expression of "0" that understands the core math functions defined in `UnaryFunction`.
  • Method Details Link icon

    • dispatchCommand Link icon

      public void dispatchCommand(Scanner scanner)
      Consume the first token in `scanner`, interpret it as a command name, and execute the appropriate command handler. Do nothing if `scanner` has no tokens. If the command name is not recognized, print an error message.
    • exitRequested Link icon

      public boolean exitRequested()
      Return whether the user has issued an 'exit' command.
    • doHelp Link icon

      public void doHelp(Scanner scanner)
      Print the commands understood by this calculator, their arguments (in angle brackets, with additional square brackets around optional arguments), and a brief description of the commands' effects. Arguments passed in `scanner` are currently ignored.
    • doSet Link icon

      public void doSet(Scanner scanner)
      Parse remaining arguments in `scanner` and perform the "set" command to assign a value to a variable in this calculator instance. Prints usage to `System.err` and returns if improper arguments are passed.
    • doUnset Link icon

      public void doUnset(Scanner scanner)
      Parse remaining arguments in `scanner` and perform the "unset" command to unassign a value from a variable in this calculator instance. Prints usage to `System.err` and returns if improper arguments are passed.
    • doClear Link icon

      public void doClear(Scanner scanner)
      Parse remaining arguments in `scanner` and perform the "clear" command to clear all variable assignments in this calculator instance. Prints usage to `System.err` and returns if improper arguments are passed.
    • doVars Link icon

      public void doVars(Scanner scanner)
      Parse remaining arguments in `scanner` and perform the "vars" command to print all variable assignments in this calculator instance. Prints usage to `System.err` and returns if improper arguments are passed.
    • doDefs Link icon

      public void doDefs(Scanner scanner)
      Parse remaining arguments in `scanner` and perform the "defs" command to print all function definitions in this calculator instance. Prints usage to `System.err` and returns if improper arguments are passed.
    • doEval Link icon

      public void doEval(Scanner scanner)
      Perform the "eval" command to evaluate the current expression and print the result. If arguments are provided in `scanner`, parse them as an RPN expression and update the current expression. Prints to `System.err` and returns if expression cannot be parsed or depends on variables that have not been assigned a value.
    • doInfix Link icon

      public void doInfix(Scanner scanner)
      Perform the "infix" command to print the current expression in infix notation. If arguments are provided in `scanner`, parse them as an RPN expression and update the current expression. Prints to `System.err` and returns if expression cannot be parsed.
    • doPostfix Link icon

      public void doPostfix(Scanner scanner)
      Perform the "postifx" command to print the current expression in postfix (RPN) notation. If arguments are provided in `scanner`, parse them as an RPN expression and update the current expression. Prints to `System.err` and returns if expression cannot be parsed.
    • doDeps Link icon

      public void doDeps(Scanner scanner)
      Perform the "deps" command to print the names of variables that the current expression depends on. If arguments are provided in `scanner`, parse them as an RPN expression and update the current expression. Prints to `System.err` and returns if expression cannot be parsed.
    • doOpcount Link icon

      public void doOpcount(Scanner scanner)
      Perform the "opcount" command to print the number of operations required to evaluate the current expression. If arguments are provided in `scanner`, parse them as an RPN expression and update the current expression. Prints to `System.err` and returns if expression cannot be parsed.
    • doOptimize Link icon

      public void doOptimize(Scanner scanner)
      Perform the "optimize" command to replace the current expression with its optimized form, propagating constant subexpressions based on the current variable assignments. If arguments are provided in `scanner`, parse them as an RPN expression and update the current expression. Prints to `System.err` and returns if expression cannot be parsed.
    • doTabulate Link icon

      public void doTabulate(Scanner scanner)
      Parse remaining arguments in `scanner` and perform the "tabulate" command to evaluate the current expression at a range of abscissa and print each abscissa and ordinate value. The required arguments are:
      1. var: The name of the abscissa variable
      2. lo: The minimum value of the abscissa (floating-point number)
      3. hi: The maximum value of the abscissa (floating-point number)
      4. n: The number of abscissa to sample at (integer)
      If additional arguments are provided in `scanner`, parse them as an RPN expression and update the current expression.

      The `n` sampled abscissa are evenly spaced between `lo` and `hi`. After executing this command,`var` should be assigned the value `hi`. Prints to `System.err` and returns if improper arguments are passed, if expression cannot be parsed, or if expression depends on variables that have not been assigned a value.

    • doDef Link icon

      public void doDef(Scanner scanner)
      Parse remaining arguments in `scanner` and perform the "def" command to define a new function equivalent to the current expression. The required arguments are:
      1. name: The name of the function to define
      2. var: The name of the variable to serve as the function's argument
      If additional arguments are provided in `scanner`, parse them as an RPN expression and update the current expression.

      Prints to `System.err` and returns if improper arguments are passed, if expression cannot be parsed, if expression depends on variables other than var, or if a function named `name` has already been defined (in the latter two cases, the current expression is still updated).

    • main Link icon

      public static void main(String[] args)
      Run an interactive calculator application. If a program argument is provided, commands are read from a file rather than `System.in`.