• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

A continuation of my previous post, if I may...

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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() ;
}
}




}
 
Bartender
Posts: 612
7
Mac OS X Python
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greetings,

It appears that TextReader class has not be found - did you find and download that also.

-steve

Oh, By the way - great care should be taken when 'grabbing' code from the web - make sure it doesn't do anything you don't want it to.
 
John Keith
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I have fixed the TextReader problem. The only error I am now encountering is that of the:

mom-static variable this cannot be reference from a static context -Line 24

Any suggestions on this?

Thanks
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First - try using code tags to format your code. It makes it a lot easier to read. See the Instant UBB Code buttons on the Post A Reply page.

Second - which is line 24 ?
 
John Keith
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, sorry about that. Line 24 states the following:

VectorQueue tokens = new VectorQueue() ; The code, in UBB format is as follows:

 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You've put all the code for the interfaces and other classes inside of class Calculator's enclosing braces, when they should be outside of them. Put each class or interface X into their own file X.java, and put all the files into the same directory. Compiling Calculator.java will be enough to compile everything else.

I won't explain the error message itself, and I don't encourage anyone else to do so, either, at this point -- it's way more than you need to know right now.

By the way, if you're trying to use this code to learn Java -- you could do way better.
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are the definitions of types Stack, Queue, VectorQueue and StackQueue
all nested inside of Calculator? They don't seem to be concepts subordinate
to the concept of a Calculator -- so move them out! By the way, do you
know the difference between a static and a non-static member class?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic