Mathematical expressions usually use infix notation in which the operator is in between the operands: (1+...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
Mathematical expressions usually use infix notation in which the operator is in between the operands: (1+ (2 3)). With prefix notation, the operator comes first: (+1(*23)). You are to write a program that evaluates expressions written in prefix notation. The values will be all integers and the only operators you need to handle are +,,, and /, all of which retain their traditional meanings. Note that with prefix notation, we are not limited to just two operands. That is, we can add any number of values: (+1234) evaluates to 10. This also works for and /. (-10 123) is interpreted as 10-1-2-3 and evaluates to 4. Here is the recursive definition of your expressions: expression: integer value or: (operator value_list) value_list: expression or: expression value_list Operator: or-or/or* 3.2. Notes Your program can assume that the input is well-formed. For example, there will be no bad operators, floating point values, unmatched parentheses, or missing arguments. All the tokens in the input will be separated by whitespace (space, tab, or newline) which means that you can use Scanner.getNext() to get each token. Your program is to continue to accept expressions until the user enters "done". Ignore case on this comparison. Your output must include your name. Your solution must be recursive. Turn in only your source file: PrefixEvaluation.java. Make sure your class is not in a package (that is, it is in the default package). Hint: Consider writing a single recursive method int eval(String e) which evaluates e. Ife is an integer (the base case), just return its value. If e isn't an integer, it must be start with a "("(recursive case). Read the operator next, then read, evaluate and store (ArrayList?) expressions until you find the matching ")". Once you have your values (the operands to the operation), perform the required operation and return its result. 3.3. Required Main Class PrefixEvaluation 3.4. Required Input A series of prefix expressions followed by "done". Mathematical expressions usually use infix notation in which the operator is in between the operands: (1+ (2 3)). With prefix notation, the operator comes first: (+1(*23)). You are to write a program that evaluates expressions written in prefix notation. The values will be all integers and the only operators you need to handle are +,,, and /, all of which retain their traditional meanings. Note that with prefix notation, we are not limited to just two operands. That is, we can add any number of values: (+1234) evaluates to 10. This also works for and /. (-10 123) is interpreted as 10-1-2-3 and evaluates to 4. Here is the recursive definition of your expressions: expression: integer value or: (operator value_list) value_list: expression or: expression value_list Operator: or-or/or* 3.2. Notes Your program can assume that the input is well-formed. For example, there will be no bad operators, floating point values, unmatched parentheses, or missing arguments. All the tokens in the input will be separated by whitespace (space, tab, or newline) which means that you can use Scanner.getNext() to get each token. Your program is to continue to accept expressions until the user enters "done". Ignore case on this comparison. Your output must include your name. Your solution must be recursive. Turn in only your source file: PrefixEvaluation.java. Make sure your class is not in a package (that is, it is in the default package). Hint: Consider writing a single recursive method int eval(String e) which evaluates e. Ife is an integer (the base case), just return its value. If e isn't an integer, it must be start with a "("(recursive case). Read the operator next, then read, evaluate and store (ArrayList?) expressions until you find the matching ")". Once you have your values (the operands to the operation), perform the required operation and return its result. 3.3. Required Main Class PrefixEvaluation 3.4. Required Input A series of prefix expressions followed by "done". Page < 6 of 6 - ZOOM + --------- 3.5. Required Output For each expression, evaluate the expression and print the result. You can use the examples below as a set of test cases. Your output should look like the following (input is in GREEN). Prefix Evaluation - Your Name Enter an expression: 3 Evaluates to 3 Enter an expression: ( +12) Evaluates to 3 Enter an expression: ( / 16 3 ) Evaluates to 5 Enter an expression: ( -10 1 2 3 ) Evaluates to 4 Enter an expression: ( / 120 2 3 4 ) Evaluates to 5 Enter an expression: ( + 1 ( -12 10 ) 3 ( / 20 5 ) 5 ( * 23 ) ) Evaluates to 21 Enter an expression: ( + ( ( * ( / 12 4 ) 2 ) ( + 37 ) ) 10 ) Evaluates to 6 Enter an expression: Done Mathematical expressions usually use infix notation in which the operator is in between the operands: (1+ (2 3)). With prefix notation, the operator comes first: (+1(*23)). You are to write a program that evaluates expressions written in prefix notation. The values will be all integers and the only operators you need to handle are +,,, and /, all of which retain their traditional meanings. Note that with prefix notation, we are not limited to just two operands. That is, we can add any number of values: (+1234) evaluates to 10. This also works for and /. (-10 123) is interpreted as 10-1-2-3 and evaluates to 4. Here is the recursive definition of your expressions: expression: integer value or: (operator value_list) value_list: expression or: expression value_list Operator: or-or/or* 3.2. Notes Your program can assume that the input is well-formed. For example, there will be no bad operators, floating point values, unmatched parentheses, or missing arguments. All the tokens in the input will be separated by whitespace (space, tab, or newline) which means that you can use Scanner.getNext() to get each token. Your program is to continue to accept expressions until the user enters "done". Ignore case on this comparison. Your output must include your name. Your solution must be recursive. Turn in only your source file: PrefixEvaluation.java. Make sure your class is not in a package (that is, it is in the default package). Hint: Consider writing a single recursive method int eval(String e) which evaluates e. Ife is an integer (the base case), just return its value. If e isn't an integer, it must be start with a "("(recursive case). Read the operator next, then read, evaluate and store (ArrayList?) expressions until you find the matching ")". Once you have your values (the operands to the operation), perform the required operation and return its result. 3.3. Required Main Class PrefixEvaluation 3.4. Required Input A series of prefix expressions followed by "done". Mathematical expressions usually use infix notation in which the operator is in between the operands: (1+ (2 3)). With prefix notation, the operator comes first: (+1(*23)). You are to write a program that evaluates expressions written in prefix notation. The values will be all integers and the only operators you need to handle are +,,, and /, all of which retain their traditional meanings. Note that with prefix notation, we are not limited to just two operands. That is, we can add any number of values: (+1234) evaluates to 10. This also works for and /. (-10 123) is interpreted as 10-1-2-3 and evaluates to 4. Here is the recursive definition of your expressions: expression: integer value or: (operator value_list) value_list: expression or: expression value_list Operator: or-or/or* 3.2. Notes Your program can assume that the input is well-formed. For example, there will be no bad operators, floating point values, unmatched parentheses, or missing arguments. All the tokens in the input will be separated by whitespace (space, tab, or newline) which means that you can use Scanner.getNext() to get each token. Your program is to continue to accept expressions until the user enters "done". Ignore case on this comparison. Your output must include your name. Your solution must be recursive. Turn in only your source file: PrefixEvaluation.java. Make sure your class is not in a package (that is, it is in the default package). Hint: Consider writing a single recursive method int eval(String e) which evaluates e. Ife is an integer (the base case), just return its value. If e isn't an integer, it must be start with a "("(recursive case). Read the operator next, then read, evaluate and store (ArrayList?) expressions until you find the matching ")". Once you have your values (the operands to the operation), perform the required operation and return its result. 3.3. Required Main Class PrefixEvaluation 3.4. Required Input A series of prefix expressions followed by "done". Page < 6 of 6 - ZOOM + --------- 3.5. Required Output For each expression, evaluate the expression and print the result. You can use the examples below as a set of test cases. Your output should look like the following (input is in GREEN). Prefix Evaluation - Your Name Enter an expression: 3 Evaluates to 3 Enter an expression: ( +12) Evaluates to 3 Enter an expression: ( / 16 3 ) Evaluates to 5 Enter an expression: ( -10 1 2 3 ) Evaluates to 4 Enter an expression: ( / 120 2 3 4 ) Evaluates to 5 Enter an expression: ( + 1 ( -12 10 ) 3 ( / 20 5 ) 5 ( * 23 ) ) Evaluates to 21 Enter an expression: ( + ( ( * ( / 12 4 ) 2 ) ( + 37 ) ) 10 ) Evaluates to 6 Enter an expression: Done
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
-
Microkernel operating systems aim to address perceived modularity and reliability issues in traditional "monolithic" operating systems. (i) Describe the typical architecture of a microkernel...
-
The accounting records for The Skate Shed, Inc., reflected the following amounts at the end of January 2018: Prepare The Skate Shed?s multistep income statement for the fiscal year ended January 31,...
-
Manfred is considering job offers of the same type of sales position from two retailers with similar product lines: Supreme Audio & Video is offering a base salary of $2000 per month plus a...
-
Answer each of the following questions. a. Employees earned wages of $20,000 during the current month, but were not paid until the following month. Should the employer record any expenses at the end...
-
What steps should workers take to determine if their jobs or intended career paths have a higher risk of being automated?
-
The handmade snuffbox industry is composed of 100 identical firms, each having short-run total costs given by STC = 0.5q2 + 10q + 5 and short-run marginal costs given by SMC = q + 10 Where q is the...
-
Assume that General Electric (GE)'s current assets are $401 billion, fixed assets are $797 billion, current liabilities are $323 billion, and long-term liabilities are zero. Calculate GE's...
-
Evaluate the minimum number of intervals N which ensures an accuracy of 0.001 in the composite Simpson's approximation for the integral A) 9; B) 10; C) 11; D) 12; E) 13;
-
Share a situation in your personal or professional life that made you more aware that your perception had an impact on a communication exchange. Discuss two strategies for avoiding perceptual errors...
-
In response to a client request, draft an email that explains why you are unable to fulfill her request. Keeping her as a client is your primary goal, so remember that. The "Company Policy Excuse" is...
-
Your company decided to organize a special dinner to appreciate all the employees and it has been proposed that the dinner to be held in the middle of December 2021. You have been appointed as the...
-
1) Explain how a Preschool program can help to bring a sense of belonging for the child? 2) It is important for a program to provide physical environments that are age-appropriate and that support...
-
The purpose of writing the analysis on Roger Ferguson's Zoom is to demonstrate why he used specific ideas, word choices, or writing structures in his book to convey his message. A few things that...
-
Why is Congress both admired and criticized?
-
In Exercises 1558, find each product. (9 - 5x) 2
-
\(X\) is the number of bits in error in the next four bits transmitted. What is the expected value of the square of the number of bits in error? Now, \(h(X)=X^{2}\). Therefore, \[ \begin{aligned}...
-
Proportion of US adults who own a cell phone. In a survey of 1006 US adults in 2014, 90% said they had a cell phone. Give the correct notation for the quantity described and give its value.
-
In March 2015, a Nielsen global online survey "found that consumers are increasingly willing to pay more for socially responsible products." Over 30,000 people in 60 countries were polled about their...
Study smarter with the SolutionInn App