Advertisement
akosiraff

Download CMSC 204 Assignment #2 Notation JAVA Answer

Feb 20th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.51 KB | None | 0 0
  1.  
  2. Download: https://solutionzip.com/downloads/cmsc-204-assignment-2-notation/
  3. CMSC 204
  4. Assignment #2
  5. Spring 2018
  6. Infix notation is the notation commonly used in arithmetical and logical formulae and statements. It is characterized by the placement of operators between operands – “infixed operators” – such as the plus sign in “2 + 2”.
  7. Postfix notation is a notation for writing arithmetic expressions in which the operands appear before their operators. There are no precedence rules to learn, and parentheses are never needed.
  8. You will be creating a utility class that converts an infix expression to a postfix expression, a postfix expression to an infix expression and will evaluates a postfix expression.
  9. Concepts tested:
  10. Generic Queue
  11. Generic Stack
  12. Exception handling
  13. Data Element
  14. String
  15. Data Structures
  16. 1. Create a generic queue class called MyQueue. MyQueue will implement the QueueInterface given you. You will be creating MyQueue from scratch (do not use an internal object of the Queue class from java.util)
  17. 2. Create a generic stack class called MyStack. MyStack will implement the Stack Interface given you. You will be creating MyStack from scratch (do not use an internal object of the Stack class from java.util)
  18. Utility Class
  19. The Notation class will have a method infixToPostfix to convert infix notation to postfix notation that will take in a string and return a string, a method postfixToInfix to convert postfix notation to infix notation that will take in a string and return a string, and a method to evaluatePostfix to evaluate the postfix expression. It will take in a string and return a double.
  20. In the infixToPostfix method, you MUST use a queue for the internal structure that holds the postfix solution. Then use the toString method of the Queue to return the solution as a string.
  21. For simplicity sake:
  22. a. operands will be single digit numbers
  23. b. the following arithmetic operations will be allowed in an expression:
  24. + addition
  25. – subtraction
  26. * multiplication
  27. / division
  28. Exception Classes
  29. Provide the following exception clases:
  30. 1. InvalidNotationFormatException – occurs when a Notation format is incorrect
  31. 2. StackOverflowException – occurs when a top or pop method is called on an empty stack.
  32. 3. StackUnderflowException – occurs when a push method is called on a full stack.
  33. 4. QueueOverflowException – occurs when a dequeue method is called on an empty queue.
  34. 5. QueueUnderflowException – occurs when a enqueue method is called on a full queue.
  35. GUI Driver
  36. 1. Initially neither radio button for notation is selected. When a radio button is selected, the Convert button is enabled and the appropriate label and field are visible for the user input.
  37. 2. When the user selects the Convert button, the appropriate label and field with the conversion will be displayed.
  38. 3. When the user selects the Evaluate button, the “answer” to the expression will be displayed.
  39. 4. When the Exit button is selected, the application will close.
  40. ALGORITHMS
  41. Infix expression to postfix expression:
  42. Read the infix expression from left to right and do the following:
  43. If the current character in the infix is a space, ignore it.
  44. If the current character in the infix is a digit, copy it to the postfix solution queue
  45. If the current character in the infix is a left parenthesis, push it onto the stack
  46. If the current character in the infix is an operator,
  47. 1. Pop operators (if there are any) at the top of the stack while they have
  48. equal or higher precedence than the current operator, and insert the popped operators in postfix solution queue
  49. 2. Push the current character in the infix onto the stack
  50. If the current character in the infix is a right parenthesis
  51. 1. Pop operators from the top of the stack and insert them in postfix solution queue until a left parenthesis is at the top of the stack, if no left parenthesis-throw an error
  52. 2. Pop (and discard) the left parenthesis from the stack
  53. When the infix expression has been read, Pop any remaining operators and insert them in postfix solution queue.
  54. Postfix expression to infix expression:
  55. Read the postfix expression from left to right and to the following:
  56. If the current character in the postfix is a space, ignore it.
  57. If the current character is an operand, push it on the stack
  58. If the current character is an operator,
  59. 1. Pop the top 2 values from the stack. If there are fewer than 2 values throw an error
  60. 2. Create a string with 1st value and then the operator and then the 2nd value.
  61. 3. Encapsulate the resulting string within parenthesis
  62. 4. Push the resulting string back to the stack
  63. When the postfix expression has been read:
  64. If there is only one value in the stack – it is the infix string, if more than one
  65. value, throw an error
  66. Evaluating a postfix expression
  67. Read the postfix expression from left to right and to the following:
  68. If the current character in the postfix expression is a space, ignore it.
  69. If the current character is an operand or left parenthesis, push on the stack
  70. If the current character is an operator,
  71. 1. Pop the top 2 values from the stack. If there are fewer than 2 values throw an error
  72. 2. Perform the arithmetic calculation of the operator with the first popped value as the right operand and the second popped value as the left operand
  73. 3. Push the resulting value onto the stack
  74. When the postfix expression has been read:
  75. If there is only one value in the stack – it is the result of the postfix expression, if
  76. more than one value, throw an error
  77.  
  78. Download: https://solutionzip.com/downloads/cmsc-204-assignment-2-notation/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement