Copy the following starter codes and put them in an Eclipse project:
“true”, “false”, “^” “&”, “!”, “(”, “)”Invalid tokens should be discarded. If the input line is e.g.:
true & ! (false ^ true)the strings produced by subsequent calls to the next() method of a TokenIter should be:
"true", "&", "!", "(", "false", "^", "true", ")".
true true false true^false true ^ false true ^ false ! ( ! (true & false))
When properly implemented
javac TokenIter.java java TokenItershould produce the following output:
line: [ ! BAD (true ^ false) % truelybad] next token: [!] next token: [(] next token: [true] next token: [^] next token: [false] next token: [)]
Notice that invalid tokens are skipped.
javac Stack.java java Stackshould produce
push: alpha push: beta [alpha, beta] peek: beta [alpha, beta] pop: beta beta [alpha] pop: alpha alpha
javac Queue.java java Queueshould produce
[gamma, delta] gamma delta
Method convertIn2Post() converts a valid infix expression to the corresponding postfix expression. It uses a TokenIter to turn the input line into a sequence of tokens. On an empty line, a line without tokens, but maybe white space, it produces an empty result. The working of convertIn2Post is described in the Lecture notes (L3: Queues), and in Prichard 7.4.
Method evalPost() evaluates the postfix expression produced by convertIn2Post() and produces the result. The working of evalPost() is described in the Lecture notes (L2: Stacks), and in Prichard 7.4.
When properly implemented,
javac In2Post.java java In2Postshould produce
Testing In2Post, debug: true input line: [] postfix: [] result: input line: [ true ^ false ] enqueue: true push: ^ enqueue: false pop: ^ enqueue: ^ postfix: [true, false, ^] dequeue: true push: true dequeue: false push: false dequeue: ^ pop: false pop: true push: true pop: true result: true input line: [! true] push: ! enqueue: true pop: ! enqueue: ! postfix: [true, !] dequeue: true push: true dequeue: ! pop: true push: false pop: false result: false debug: false input line: [true ^ true & false] postfix: [true, true, false, &, ^] result: true input line: [true & (true ^ false)] postfix: [true, true, false, ^, &] result: true input line: [! true] postfix: [true, !] result: false input line: [! true ^ true] postfix: [true, !, true, ^] result: true input line: [! (! true ^ false)] postfix: [true, !, false, ^, !] result: true
Make sure you put java files in your jar (not class files). The TAs can help you creating a proper jar file.
The P2.jar file should have the *.java files in the top level directory and not in the src directory. jar files generated by Eclipse typically have source files in the src directory.
The directory structure of P2.jar should be as follows:
P2.jar |__ Stack.java |__ Queue.java |__ In2Post.java |__ TokenIter.java