CS 210 Homework #4 Due wed Oct 10 Assignment: constuct and test simple RPN Calculator, using stack. Reimplement stack using list (instead of array) Pieces you'll need: a. ST2.java, same basically as last week. Slight update: it now accepts a null string (or "keyboard") and will then read from the keyboard. Note that tokens have a "type" field. This may be useful in determining what kind of token you're dealing with. ST2 has a main program, which you'll want to modify or replace. b. StackX.java, as before. This is a revised version of the code from Lafore (see Chap04/Stack) I have modified it to use Object instead of long, and added a display method. Note that the elements of the stack are of type Object. You may need to cast the result returned by pop(). Things to be done: 0. THINK FIRST. This assignment uses many of the same pieces as last week. Do you want to take last week's pieces and change them, or start over from the basic elements? There are reasons on both sides. (Probably, it will be less confusing to start with a new "main" program -- in a new file) 1. Make a simple calculator, using "Reverse Polish Notation" (also called "Polish postfix"). As discussed in class, RPN is a form where the operation symbol follows the operands. It always has an implicit stack! Thus normal arithmetic ("infix"): 2+3 2+3*4 RPN ("postfix") form: 2 3 + 2 3 4 * + This program will read tokens from its input stream. Tokens are either numbers, or command/operators. Perform the appropriate action as each token is read. Please implement the following basic operators: "+" binary add (two operands, from stack) "*" binary multiply (ditto) "S" Show (print) the stack "P" Pop and Print the "answer" (pop off top of stack) Note that the StringTokenizer's rules are a bit like the Java language. Thus you'll need to separate numbers by a space if there's no operator. 234 -> two hundred thirty four S2 -> string "S2" ** Plug loose ends. If the stack is empty, pop should print an error message rather than just crashing. Push should do the same if the stack is full. (Not necessary to extend the size of the stack). Also, don't assume that the input is correct: 3 apples 4 supper should give an error message. Note: it's up to you whether you read input from a file or the keyboard. But read ALL the input from the same place! ** The assignment MUST have some debugging print statements which indicate what's going on at various points. It's up to you whether you want them conditional on a VERBOSE flag so you can create a pretty version to show off! > There is a similar project in Lafore -- look in Chapter 4, > "Evaluating Postfix Expressions". You might want to have a > look at it. If you're a bit wobbly on what's going on, then > certainly pick up the code, run it, add some print > statements... Note that Lafore doesn't use the > StringTokenizer, so his inputs are single-digit numbers (which > he converts from char to int). 2. Take the working calculator from part 1. Construct a new class which provides a new implementation of the methods we're using from the original StackX, but this time uses a linked list instead of an array. Modify the program to use the linked list class. > Lafore, chapter 5, has a basic implementation of LinkList, as > well as a clever animation. Work your way through those before > you start programming this part. Thoughts: How do handle having two versions? You can, of course, make a full copy of the file. Or: consider having a variable which controls whether you create a array or a list stack object. (Perhaps StackX itself should be an interface? That sounds promising) It might look like: interface StackX ...; class ArrayStack implements StackX ...; class ListStack implements StackX ...; StackX stk; if (useList) { stk = new ListStack(..); } else { stk = new ArrayStack(..); } what do you think? NOTE: Don't start part 2 until part 1 is working and you're comfortable with it! 3. (extra challenge --> don't worry about this until you've finished #1 and #2, those are the important part of the homework) Extend the calculator to do some additional interesting operations "-" unary minus (ONE operand -- "negate") "<" less-than: result is 0 if false, 1 if true "Clr" Clear the stack "Dup" Duplicate (push another value the same as the stack top) thus: 3 2 - + will give: 1 4 3- 2*+ will give: -2 7 Dup + will give: 14 4 3+ Dup 2*+ will give: 21 3 4 < will give: 1 3 4 Clr 3 2 + will give: 5