After making the changes (definng stackqueue, etc) I am still recieving 2 errors. I'm hoping someone could help me out here. The errors are as follows:
cannot find symbol class TextReader (line21)
non-static variable this cannot be referenced from a static context(line25)
any thoughts on the matter are certainly welcomed
===============================
Code Below
===============================
import java.io.* ;
import java.util.* ;
import java.lang.* ;
// class Calculator reads expressions from standard in that may be in postfix,
// prefix, or fully parenthesized infix notation.
// It then creates an expression tree and can create expression strings
// in other notations and/or evaluate the original expression.
public class Calculator
{
private
String postfix, infix, prefix ;
private double value ;
private Node myRoot ;
public static void main(String[] args)
{
System.out.println("This program evaluates fully parenthesized and spaced infix expressions, postfix expressions, and prefix expressions") ;
System.out.println("Enter 'quit' to exit.") ;
// open a console reader
TextReader console = new TextReader(System.in) ;
console.eolIsSignificant(true) ;
// process the console input
VectorQueue tokens = new VectorQueue() ;
while(true)
{
char c = console.peek() ;
if(c=='\n')
{
// end of line, process the expression
console.readChar() ;
if(!tokens.isEmpty())
{
// obtain the first element of the queue
Token firstToken = (Token) tokens.elementAt(0) ;
// check for a quit command
if(firstToken.getType() == Token.OPERATOR &&
firstToken.getOp().equals("quit"))
return ;
// create a new calculator object
Calculator calc = new Calculator(tokens) ;
// print the expression tree to console
System.out.println("Tree:") ;
calc.printTree() ;
// print the expression
System.out.println("Postfix:"+calc.getPostfix()) ;
System.out.println("Prefix:"+calc.getPrefix()) ;
System.out.println("Infix:"+calc.getInfix()) ;
System.out.println("Value:"+calc.getValue()) ;
System.out.println() ;
}
}
else if(c=='(' || c==')')
{
// enqueue a PAREN token
tokens.enqueue(new Token()) ;
console.readChar() ;
}
else if( Character.isDigit(c) )
{
// enqueue a number
tokens.enqueue(new Token(console.readDouble())) ;
}
else if(c==' ')
{
console.readChar() ;
}
else
{
// enqueue a symbol
tokens.enqueue(new Token(console.readWord())) ;
}
}
}
public Calculator(VectorQueue tokens)
{
Token firstToken = (Token) tokens.elementAt(0) ;
// convert this queue into a binary tree by checking the first token
if(firstToken.getType() == Token.PAREN)
buildInfix(tokens) ;
else if(firstToken.getType() == Token.OPERATOR)
buildPrefix(tokens) ;
else
buildPostfix(tokens) ;
// initialize object state
postfix = "" ;
prefix = "" ;
infix = "" ;
value = evaluate(myRoot) ;
}
// getter methods - return object state
public String getPrefix()
{
return prefix ;
}
public String getPostfix()
{
return postfix ;
}
public String getInfix()
{
return infix ;
}
public double getValue()
{
return value ;
}
// printTree prints the expression tree to standard out
public void printTree()
{
printTreeHelp(myRoot, "") ;
}
private void printTreeHelp(Node root, String offset)
{
if(root == null)
return ;
printTreeHelp(root.left, offset + " ") ;
System.out.println(offset + root.data) ;
printTreeHelp(root.right, offset + " ") ;
}
// evaluate recursively traverses the expression tree and in
// doing so builds infix, postfix, and prefix versions of the
// expression and returns the value of the expression
private double evaluate(Node root)
{
if(root.left == null && root.right == null)
{
prefix += " " + root.data ;
postfix += " " + root.data ;
infix += " " + root.data ;
return ((Double)root.data).doubleValue() ;
}
else
{
prefix += " " + root.data ;
infix += " (" ;
double arg1 = evaluate(root.left) ;
infix += " " + root.data ;
double arg2 = evaluate(root.right) ;
postfix += " " + root.data ;
infix += " )" ;
return evaluate((String)root.data, arg1, arg2) ;
}
}
// buildPostfix converts a postfix expression to an expression tree
private void buildPostfix(Queue tokens)
{
Stack stk = new VectorStack() ;
while(!tokens.isEmpty())
{
Token tkn = (Token) tokens.dequeue() ;
if(tkn.getType() == Token.OPERAND)
{
// this token is a number, push it
stk.push(new Node(new Double(tkn.getNum()))) ;
}
else
{
// this token is an operator
// pop the last two elements on the stack
Node arg2 = (Node) stk.pop() ;
Node arg1 = (Node) stk.pop() ;
// create a new node, push it
stk.push(new Node(tkn.getOp(), arg1, arg2)) ;
}
}
myRoot = (Node) stk.pop() ;
}
// buildPrefix converts a prefix expression into an expression tree
private void buildPrefix(Queue tokens)
{
myRoot = buildPrefixHelp(tokens) ;
}
private Node buildPrefixHelp(Queue tokens)
{
Token tkn = (Token) tokens.dequeue() ;
if(tkn.getType() == Token.OPERAND)
{
return new Node(new Double(tkn.getNum())) ;
}
else
{
return new Node(tkn.getOp(), buildPrefixHelp(tokens), buildPrefixHelp(tokens)) ;
}
}
// buildInfix converts an infix expression into an expression tree
private void buildInfix(Queue tokens)
{
myRoot = buildInfixHelp(tokens) ;
}
private Node buildInfixHelp(Queue tokens)
{
Token tkn = (Token) tokens.dequeue() ;
if(tkn.getType() == Token.OPERAND)
{
// this token must correspond to a number
return new Node(new Double(tkn.getNum())) ;
}
else
{
// this is a non-leaf node
Node tmp = new Node() ;
// process the left sub expression
tmp.left = buildInfixHelp(tokens) ;
// next token we encounter is an operator
tmp.data = ((Token)tokens.dequeue()).getOp() ;
// process the right sub expression
tmp.right = buildInfixHelp(tokens) ;
tokens.dequeue() ;
return tmp ;
}
}
private static double evaluate(String operator, double operand1, double operand2)
{
if (operator.equals("+"))
return operand1 + operand2 ;
else if (operator.equals("-"))
return operand1 - operand2 ;
else if (operator.equals("*"))
return operand1 * operand2 ;
else if (operator.equals("/"))
return operand1/operand2 ;
else if (operator.equals("^"))
return Math.pow(operand1, operand2) ;
else
throw new RuntimeException("Illegal operator: " + operator) ;
}
// Interface Stack defines a set of operations for manipulating a LIFO
// (Last In First Out) structure that can be used to store ints. It has
// the following public methods:
// public void push(Object value);
// pushes the given value on the top of the stack
// public Object pop();
// removes and returns the value at the top of the stack
// public boolean isEmpty();
// returns true if the stack is empty; otherwise returns false
// public int length();
// returns the number of elements currently stored in the stack
public interface Stack
{
public void push(Object value);
public Object pop();
public boolean isEmpty();
public int length();
}
// Interface Queue defines a set of operations for manipulating a FIFO
// (First In First Out) structure that can be used to store ints. It has
// the following public methods:
// public void enqueue(Object value);
// stores the given value at the end of the queue
// public Object dequeue();
// removes and returns the value at the front of the queue
// public boolean isEmpty();
// returns true if the queue is empty; otherwise returns false
// public int length();
// returns the number of elements currently stored in the queue
public interface Queue
{
public void enqueue(Object value);
public Object dequeue();
public boolean isEmpty();
public int length();
}
// VectorQueue implements the Queue interface, providing a set of
// operations for manipulating a FIFO (First In First Out) structure
// that can be used to store objects. It has the following public methods:
// public VectorQueue()
// constructs an empty VectorQueue
// public void enqueue(Object value);
// enqueues the given value at the end of the queue
// public Object dequeue();
// removes and returns the value at the front of the queue
// public boolean isEmpty();
// returns true if the queue is empty; otherwise returns false
// public int length();
// returns the number of elements currently stored in the queue
public class VectorQueue extends Vector implements Queue
{
// no constructor needed (default constructor calls the Vector
// default constructor)
public void enqueue(Object value)
// post: enqueues the given value at the back of the queue
{
addElement(value);
}
public Object dequeue()
// pre : !isEmpty()
// post: removes and returns the value at the front of the queue;
// raises an exception if queue is empty
{
if (isEmpty())
throw new RuntimeException("Attempt to dequeue an empty queue");
Object result = firstElement();
removeElementAt(0);
return result;
}
public int length()
// post: returns the number of values currently stored in the queue
{
return size();
}
}
// VectorStack implements the Stack interface, providing a set of
// operations for manipulating a LIFO (Last In First Out) structure
// that can be used to store objects. It has the following public methods:
// public void push(Object o) {
// adds an element to the stack
// public Object pop() {
// removes and returns the last value in the stack
// public int length() {
// returns the number of values currently stored in the stack
public class VectorStack extends Vector implements Stack {
public void push(Object o) {
// adds an element to the stack
add(o) ;
}
public Object pop() {
// removes and returns the last value in the stack
if(isEmpty())
throw new RuntimeException("Attempt to pop an empty stack.") ;
Object o = lastElement() ;
removeElementAt(size()-1) ;
return o ;
}
public int length() {
// returns the number of values currently stored in the stack
return size() ;
}
}
}