dbrown
Class Variable

java.lang.Object
  extended by dbrown.Variable

public class Variable
extends java.lang.Object

Recognizes and stores simple variables for mathematical expressions.

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

Field Summary
private  java.lang.String variable
           
private static java.util.regex.Pattern variablePattern
           A pattern for matching single-letter variables using the regular expression "^[a-zA-Z][0-9]".
 
Constructor Summary
Variable(java.lang.String variable)
          Create a variable object.
 
Method Summary
static java.util.regex.MatchResult getMatchResult(java.lang.String s)
          Returns the result of attempting to match the variable pattern against the input string.
static boolean isVariable(java.lang.String s)
          Determines whether or not the input string is an variable.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

variablePattern

private static final java.util.regex.Pattern variablePattern

A pattern for matching single-letter variables using the regular expression "^[a-zA-Z][0-9]". The expression has two sections:

  1. ^ : attempts to match the expression against only the beginning of a string.
  2. [a-zA-Z][0-9] attempts to match the any one of the upper or lower case letters followed by a single digit.

A few changes in this pattern could allow it to match longer multi-character variables as well.


variable

private java.lang.String variable
Constructor Detail

Variable

public Variable(java.lang.String variable)
Create a variable object.

Parameters:
variable - The string to treat as an variable.
Method Detail

getMatchResult

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

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

isVariable

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

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

toString

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