

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Homework #5: Stacks, Queues, & Deques (50 pts) Due date: Friday, May 2, 2003 (5:00 PM) Treat this paper as a specification for your assignment. Your code will be graded on correctness, readability, and style. Study problems and exercises must be legible and will be graded on correctness.
Operator Description Example Output
Of course, you are free to add more operators if you would like (such as asin, acos, atan, ceil, floor, rand, etc.). Your RPNCalc should test for illegal input—incorrect count of numbers (too many, “ 4 5 3 +”, or too few, “4 +”), unknown operators (which should be treated as a incorrectly formatted number), and/ or numbers that do not have a valid format (e.g., “45+”, “5.32.1”, etc.)—and report an error along with the offending argument string when illegal input is detected. Hint: one way to implement this is to read in each string and compare it with the list of accepted operators, if it is not a valid operator then assume that it is a number, convert it to a Double (the wrapper class for dou- ble) using one of he following methods in the Double class: public static double parseDouble(String s) public static Double valueOf(String s) and push it onto the stack. (Note that both conversion methods will throw a NumberFormatException if the string is not in the correct format.) If the argument string is a valid operator, pop the appropriate number of Doubles off the stack (depending on the given operator), convert them to double (or an int for the value of n used in the last three operators), and then perform the appropriate computation and push the result back onto the stack. When all of the command line arguments are processed, the result is output by popping the stack (which should result in an empty stack) and printing the value.
As a side note, you can make this command line RPN calculator easier to use if you add the following alias to your “.cshrc” file: alias calc java RPNCalc You can then simply type commands such as the following: calc pi 5 ^2 * (computes the area of a circle with a radius of 5)
Written homework:
E 10.5) ( 2 pts ) What is the Polish notation representation of the following expression?
( a * ( b + c )) + ( b / d ) * a
E 11.1) ( 5 pts ) How might you use a single deque to determine whether a string is a palindrome? (See pro- gramming project 2, Chapter 10).
E 11.5) ( 8 pts ) Consider the following diagram. Starting from node A, list the vertices as they might be visited in a breadth-first search and as they might be visited in a depth-first search. Note that there are many different sequences for both forms of search.
In class: a printout of written homework in the file Answers.txt and a printout of your source files RPN- Calc.java and Maze.java.
Via the web: the files Answers.txt, RPNCalc.java, and Maze.java and a screen grab of the solu- tion to the maze problem called Maze.jpg (or Maze.tif).
E
B
B
A D
C
F
E J
G
H
I