Finger exercises
-
Lukasièwicz was a Polish logician, so his notation for parentheses-free
expressions is often called Reverse Polish Notation. To get your brain
in gear, convert the following expressions to RPN! What are the values
of the expressions?
- 1 * 2 + 3
- 1 + 2 * 3
- 1 + 2 - 3 ^ 4
-
1 ^ 2 - 3 * 4
-
1 + 2 * 3 - 4 ^ 5 + 6
-
( 1 + 2 ) * 3 + ( 4 ^ ( 5 - 6 ) )
-
1 + 2 + 3 / 4 + 5 + 6 * ( 7 + 8 )
-
9 - 1 - 2 - 3 * 2 - 1
-
For the infix expression a + b ^ c * d ^ e ^ f - g - h / ( i + j ),
do the following:
-
Show how the operator precedence parsing algorithm generates the corresponding
postfix expression.
-
Show how a postfix machine evaluates the resulting postfix expression.
-
Explain, in general terms, how unary operators can be incorporated into
the expression evaluators. Assume that the unary operators precede their
operands and have high precedence.
Lab exercises
Read through all of the exercises before starting! Oh dear,
this is a lot of work. I guess we can't play one-person-types-while-the-other-looks-on
this week.... I would strongly suggest that one person get
exercise 1 to work while the other one starts exercise 2. Then you exchange code,
and voilà, it works! Now you can get back together to do the third
exercise. The bored are, of course, done by the break, so they go on to do other interesting things.
-
Implement a class Stack.java as discussed in the lecture, using
a linked list of objects! Don't use the Stack or LinkedList that is available by default in Java. I understand that there are a million of these available on the net. Start with the code that implements a stack using an array of objects that is available under reading materials. Extend the class to include both an exception on stack underflow as well as stack overflow. Will you really need both exceptions? Why or why not? Override the toString() method to provide a useful way of printing a stack.
-
Implement a class Postfix.java that has a method
public int evaluate (String pfx){...}
that takes a String representing a
postfix expression and determines the value represented by that expression.
You will need to convert the individual characters to an object for storing in the stack. Remember that Characters is a wrapper class for chars. You will be needing a stack for the evaluation, luckily your partner is currently in the process of making one. Build a test class and check the postfix expressions you did in the finger
exercises. If there is a difference between the value computed and the
value expected, either you were wrong, or the implementation is wrong or
both. Do not go on before you are sure that this is working right!
-
Now add another method to the Postfix.java class
public String infixToPostfix (String ifx){...}
that converts an infix expression which is presented as a String to
a String representing a postfix expression! Throw an exception if your input is not well-formed.
-
Now add another method that reads a string from the console, evaluates the result and prints the result to the console.
- (For the bored) Once this works for digits, go on and parse multidigit Integers out of the String. Can you do it for double values as
well? If you are still bored, parse mixed expressions (doubles and ints in the same expression).
- (For the really bored) Java 1.5 now has generics, which would be a useful way of implementing stacks. Have a look at the tutorial, sniff around, see if you can make a generic stack.