Write a Java program which will input sequences of characters representing infix expressions involving the binary...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
Write a Java program which will input sequences of characters representing infix expressions involving the binary (dyadic) operators +, -, *, and plus parentheses and single digit operands. The program should output the equivalent postfix expressions AND the values of those expressions. For example: INPUT: 6+9* (5* (3+4)) OUTPUT: 6 9 5 3 4 + += 321 As discussed in class, Dijkstra's Shunting Algorithm can be employed to convert an infix expression into an equivalent postfix expression using a stack for operators. Furthermore, a postfix expression can be evaluated using a stack for operands. The logical design provided on the next page combines these two algorithms into one procedure. The procedure should be performed for each input expression. The Shunting Algorithm requires the use of two value-returning methods: input Priority and stackPriority. input Priority (with token as its actual parameter) returns the priority value of an operator when it is on the input string. stack Priority (with top element of operator stack as actual parameter) returns the priority value of an operator on the stack. Use the following for implementation of these two methods: OPERATOR INPUT PRIORITY + * NA 2 2 3 3 4 STACK PRIORITY 0 2 2 3 3 1 ( Since the algorithm requires two stacks (each of which comprised of different element types), an interface (i.e., general specification) for a stack abstract data type should be defined. The implementation of the operator stack class used in your program should reference data elements of type character; similarly, the implementation of the operand stack class should reference data elements of integer type. Both stack classes should be implemented using the reference-based linked model. Be sure to follow the techniques of good programming style and use extensive comments to provide for internal documentation of your source program, including author, date, purpose (s), pre-condition (s), and post-condition (s) for all methods. For evaluation of this lab assignment, you are required to separately provide your source program (.java) file(s) as well as your input file and output file (or screenshot of output) via Canvas submission. Please submit these deliverables on or before the assignment due date. class containing static methods: main() ... pseudocode provided, inputPriority(), stackPriority() interface for stacks: StackInterface (contains declarations for stack methods but no code therein... also no data members) class for operator stack (implements StackInterface): contains top data member (reference to char-type node) and full definitions of all methods declared in interface class for operand stack (implements StackInterface): contains top data member (reference to int-type node) and full definitions of all methods declared in interface class for char-type nodes: contains char data member (operator) and reference data member next to next char-type node - also standard node operations: constructor(s), gets, and sets class for int-type nodes: contains int data member (operand) and reference data member next to next int-type node - also standard node operations: constructor(s), gets, and sets Initialize the operator stack to contain a ';' (bottom of stack operator) Get the first token while the end of the expression is not reached if the token is an operand then Print the token followed by a space push the operand onto the operand stack else if the token is a ')' then else peek while the top of the operator stack is not equal to '(' pop the operator stack Print the operator followed by a space pop the operand stack twice Apply the designated operation to the two operands push the operation result onto the operand stack end while pop the '(' and discard it peek while inputPriority (token) stackPriority (top of operator stack) pop the operator stack Print the operator followed by a space pop the operand stack twice Apply the designated operation to the two operands push the operation result onto the operand stack end while push the token onto the operator stack Get the next token end while peek while the top of the operator stack is not equal to ';' pop the operator stack Print the operator followed by a space pop the operand stack twice Apply the designated operation to the two operands push the operation result onto the operand stack end while pop the operand stack, print an "=" sign followed by a space, and then print the result 5 = 5 3 5 - 5 3 4 = 2 + 73 * 56 = 35 * * +3 = 51 3 / + +6+ = 29 = 1 2 5 7 8 + 5 = . 5 3 4 5 * 9453 - * 1 2 3 * 4 * = 27 567 * : = 5040 = 12 2 48 34 + = 9 1 | - ||2 + 25 242 + 5 3 = 7 3 5 * * 19 6 - - * 3 = 42* = 3 9 7 1 15 * - 6 9 5 3 4 + * - 12 = 3 - 56 = 43 * 2 / 5 + = 8 + 12+ 3+ 4+ 5+ = 321 = 15 9 3/8 2 / + = 7 Output, +x+ 5 5-3 5* (3+4) 7*3+5*6 2+5* (7+8)/3 (5) 3+4*5+6 9-4 (5-3) 1*2*3*4*5*6*7 (2+4)* (8-6) 3+4 9-1-2-3 2*(((4+2))) (((((5))))) 3*7-4*2+5*6 3*5 (3* (9-7*1))/2+5 6+9 (5* (3+4)) 1+2+3+4+5 9/3+8/2 In Put.txt Write a Java program which will input sequences of characters representing infix expressions involving the binary (dyadic) operators +, -, *, and plus parentheses and single digit operands. The program should output the equivalent postfix expressions AND the values of those expressions. For example: INPUT: 6+9* (5* (3+4)) OUTPUT: 6 9 5 3 4 + += 321 As discussed in class, Dijkstra's Shunting Algorithm can be employed to convert an infix expression into an equivalent postfix expression using a stack for operators. Furthermore, a postfix expression can be evaluated using a stack for operands. The logical design provided on the next page combines these two algorithms into one procedure. The procedure should be performed for each input expression. The Shunting Algorithm requires the use of two value-returning methods: input Priority and stackPriority. input Priority (with token as its actual parameter) returns the priority value of an operator when it is on the input string. stack Priority (with top element of operator stack as actual parameter) returns the priority value of an operator on the stack. Use the following for implementation of these two methods: OPERATOR INPUT PRIORITY + * NA 2 2 3 3 4 STACK PRIORITY 0 2 2 3 3 1 ( Since the algorithm requires two stacks (each of which comprised of different element types), an interface (i.e., general specification) for a stack abstract data type should be defined. The implementation of the operator stack class used in your program should reference data elements of type character; similarly, the implementation of the operand stack class should reference data elements of integer type. Both stack classes should be implemented using the reference-based linked model. Be sure to follow the techniques of good programming style and use extensive comments to provide for internal documentation of your source program, including author, date, purpose (s), pre-condition (s), and post-condition (s) for all methods. For evaluation of this lab assignment, you are required to separately provide your source program (.java) file(s) as well as your input file and output file (or screenshot of output) via Canvas submission. Please submit these deliverables on or before the assignment due date. class containing static methods: main() ... pseudocode provided, inputPriority(), stackPriority() interface for stacks: StackInterface (contains declarations for stack methods but no code therein... also no data members) class for operator stack (implements StackInterface): contains top data member (reference to char-type node) and full definitions of all methods declared in interface class for operand stack (implements StackInterface): contains top data member (reference to int-type node) and full definitions of all methods declared in interface class for char-type nodes: contains char data member (operator) and reference data member next to next char-type node - also standard node operations: constructor(s), gets, and sets class for int-type nodes: contains int data member (operand) and reference data member next to next int-type node - also standard node operations: constructor(s), gets, and sets Initialize the operator stack to contain a ';' (bottom of stack operator) Get the first token while the end of the expression is not reached if the token is an operand then Print the token followed by a space push the operand onto the operand stack else if the token is a ')' then else peek while the top of the operator stack is not equal to '(' pop the operator stack Print the operator followed by a space pop the operand stack twice Apply the designated operation to the two operands push the operation result onto the operand stack end while pop the '(' and discard it peek while inputPriority (token) stackPriority (top of operator stack) pop the operator stack Print the operator followed by a space pop the operand stack twice Apply the designated operation to the two operands push the operation result onto the operand stack end while push the token onto the operator stack Get the next token end while peek while the top of the operator stack is not equal to ';' pop the operator stack Print the operator followed by a space pop the operand stack twice Apply the designated operation to the two operands push the operation result onto the operand stack end while pop the operand stack, print an "=" sign followed by a space, and then print the result 5 = 5 3 5 - 5 3 4 = 2 + 73 * 56 = 35 * * +3 = 51 3 / + +6+ = 29 = 1 2 5 7 8 + 5 = . 5 3 4 5 * 9453 - * 1 2 3 * 4 * = 27 567 * : = 5040 = 12 2 48 34 + = 9 1 | - ||2 + 25 242 + 5 3 = 7 3 5 * * 19 6 - - * 3 = 42* = 3 9 7 1 15 * - 6 9 5 3 4 + * - 12 = 3 - 56 = 43 * 2 / 5 + = 8 + 12+ 3+ 4+ 5+ = 321 = 15 9 3/8 2 / + = 7 Output, +x+ 5 5-3 5* (3+4) 7*3+5*6 2+5* (7+8)/3 (5) 3+4*5+6 9-4 (5-3) 1*2*3*4*5*6*7 (2+4)* (8-6) 3+4 9-1-2-3 2*(((4+2))) (((((5))))) 3*7-4*2+5*6 3*5 (3* (9-7*1))/2+5 6+9 (5* (3+4)) 1+2+3+4+5 9/3+8/2 In Put.txt
Expert Answer:
Related Book For
Java How To Program Late Objects Version
ISBN: 9780136123712
8th Edition
Authors: Paul Deitel, Deitel & Associates
Posted Date:
Students also viewed these programming questions
-
Design a Java class that represents a cache with a fixed size. It should support operations like add, retrieve, and remove, and it should evict the least recently used item when it reaches capacity.
-
Q1. You have identified a market opportunity for home media players that would cater for older members of the population. Many older people have difficulty in understanding the operating principles...
-
March 31, 2014, adjusted trial balance for Brenner Climbing Adventures has been alphabetized as follows: Required Journalize the closing entries. No. Account Debit Credit $ 2,600 168 Accumulated...
-
Suppose an Olive Garden restaurant is considering whether to (1) bake bread for its restaurant in-house or (2) buy the bread from a local bakery. The chef estimates that variable costs of making each...
-
How are individual human rights protected in Canada?
-
An electronic instrument of mass \(10 \mathrm{~kg}\) is mounted on an isolation pad. If the base of the isolation pad is subjected to a shock in the form of a step velocity of \(10 \mathrm{~mm} /...
-
A put and a call have the following terms: The price of the stock is currently $29. You sell the stock short. Illustrate how to use the call or the put to reduce your risk exposure. a) What is the...
-
1. (10 points) An irregularly shaped piece of metal is weighed on a balance, as is found to be 20.97g. The metal piece is then placed into a graduated cylinder filled with distilled water. The...
-
XYZ Manufacturing would like to purchase a machine for its current operations. XYZ would like to know the maximum price it should pay for that machine. That is, how high must the price be for the...
-
Interest and penalties of $59,580 were recorded on delinquent taxes, of which $10,750 was estimated to be uncollectible. Sales taxes receivable of $21,722 were also recorded. Required: Interest and...
-
Use Numpy to solve the equation Ax = b, where: A= 1.3174 2.7250 2.7250 1.7181 0.4002 0.8278 1.2272 2.5322 0.8218 1.5608 0.3629 2.9210 1.9664 2.0011 0.6532 1.9945 b 8.4855 4.9874 5.6665 6.6152
-
Calculate the remaining values Calculation: Description Purchase Benefits Benefits Benefits Benefits Residual Total Net PV Real Dollars Time Costs/Benefits 0 1 2 3 4 4 -400,000 90,000 90,000 90,000...
-
Project YMC has the following anticipated cash flows before allowing for inflation. The cost of capital has been calculated at 19.5% to include an allowance for inflation. The rate of inflation is...
-
V. Current ratio QUESTION 3 The following information relate to tow businesses: Polihali (Pty) Ltd M 20 12 20,000 Unit selling price Unit variable cost Fixed costs Contributin ratio Graphics (Pty)...
-
Introduction to Professional Communications , Part 5.5 (P.S link below) includes the following checklist for determining whether a report fulfills its goals: Report considers the audience's needs...
-
2. According to a report, the average cost to rent a full-size car is $150 per day. Suppose that the distribution of this rental cost is normally distributed with a population standard deviation of...
-
Listed below are common types of current liabilities, contingencies, and commitments: a. Accounts payable b. Bank loans and commercial paper c. Notes payable d. Dividends payable e. Sales and excise...
-
Phishing is a form of identity theft in which, in an e-mail, a sender posing as a trustworthy source attempts to acquire private information, such as your user names, passwords, credit-card numbers...
-
Modify Fig.7.13 to deal a five-card poker hand. Then modify class DeckOfCards of Fig.7.12 to include methods that determine whether a hand contains a) A pair b) Two pairs c) Three of a kind (e.g.,...
-
Modify Exercise 4.15 to combine your code from the four separate triangles of asterisks such that all four patterns print side by side. Make clever use of nested for loops. Exercise 4.15 Write an...
-
What does it mean to say that the demand for resources is a derived demand? Is the demand for all goods and services a derived demand?
-
Using the data in exercise 2, determine how many units of resources the firm will want to acquire. Data from in exercise 2 Using the information in the following table, calculate the marginal revenue...
-
Using the information in the following table, calculate the marginal revenue product (MRP = MPP MR). Unit of Resources Total Resource Output Price Price 1 10 $5 $10 2 25 $5 $10 345 35 $5 $10 40 $5...
Study smarter with the SolutionInn App