dbrown
Class Operator

java.lang.Object
  extended by dbrown.Operator

public class Operator
extends java.lang.Object

Recognizes, stores, and executes simple mathematical operations.

Version:
2007-10-12 (For CP213 Fall 2007 - Assignment 1)
Author:
David Brown

Field Summary
private static java.util.regex.Pattern operatorPattern
           A pattern for matching operators using the regular expression "^[+-/*%()]".
private  char symbol
          The character representation of the operator.
 
Constructor Summary
Operator(char c)
          Constructs a new operator based upon the character passed in.
Operator(java.lang.String s)
          Constructs a new operator based upon the character passed in.
 
Method Summary
static java.util.regex.MatchResult getMatchResult(java.lang.String s)
          Returns the result of attempting to match the operator pattern against the input string.
static boolean isOperator(java.lang.String s)
          Determines whether or not the input string is an operator.
 char toChar()
          Returns the character symbol of the operator.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

operatorPattern

private static final java.util.regex.Pattern operatorPattern

A pattern for matching operators using the regular expression "^[+-/*%()]". The expression has two sections:

  1. ^ : attempts to match the expression against only the beginning of a string.
  2. [+-/*%()] attempts to match the any one of the operator symbols.

A few changes in this pattern could allow it to match longer operators as well.


symbol

private char symbol
The character representation of the operator.

Constructor Detail

Operator

public Operator(char c)
Constructs a new operator based upon the character passed in.

Parameters:
c - an operator character

Operator

public Operator(java.lang.String s)
Constructs a new operator based upon the character passed in.

Parameters:
s - a string whose first character is an operator character
Method Detail

getMatchResult

public static java.util.regex.MatchResult getMatchResult(java.lang.String s)
Returns the result of attempting to match the operator pattern against the input string.

Parameters:
s - The string to compare against the operator pattern.
Returns:
a MatchResult
Throws:
java.util.InputMismatchException - - if the next token in the input string is not an operator

isOperator

public static boolean isOperator(java.lang.String s)
Determines whether or not the input string is an operator.

Parameters:
s - The string to compare against the operator pattern.
Returns:
true if the input string is an operator, false otherwise.

toChar

public char toChar()
Returns the character symbol of the operator.

Returns:
the character symbol of the operator.

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object