(60pts) This class has static methods that will help us evaluate Lisp arithmetic expressions. Before a...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
(60pts) This class has static methods that will help us evaluate Lisp arithmetic expressions. Before a List expression is evaluated, we have to make sure that it is a valid expression. The following expressions show valid and invalid Lisp arithmetic expressions. Valid Expression (+ (-5) ) (* 4 4 5) (322) (* 4 4) Invalid Expressions (+ (-) ) (* 4 4 5) (/3 8 11) (* 4 4) (+ (-9) (* 4 4 5) (* (/ 3 6 7) (* 1 1) (+ (-2) (* 4 4 5) (/ 3 2 1)) (* c 6) (+ (-2) (* 3 3 4) ((* (/ 3 1 1) (* a b) ) (40pts) public static Double evaluate(String expr) - a method that accepts a Lisp expression and returns the result. The method returns a RunTimeException if the accepted expression is not a valid Lisp expression. The following can be reasons for the method to throw the exception: o An empty expression (). o Unbalanced expression. o Invalid expression. See the following examples of invalid expressions. o If the expression does not adhere to the rules outlined in the "Overview" section, it is invalid. For this project, if an expression contains operators other than the basic four arithmetic operators it will be considered as an operand. An expression can contain a single letter variables as operands. If an expression contains variable operands, it should prompt the programmer for values of the operands and use the values for the evaluation. For the sake of simplicity, only integer values are going to be accepted from the user. Note that if an expression contains operands such as var, it will consider it as three operand tokens v, a, r and prompt the programmer for the three values. Take a look at the example given below. The following table gives examples of invalid/valid Lisp expressions. Expression (* 3255) (-) () (/* w 1) (a + b + c + d) (* (/ 3 w 1) (* (r r)) (* (/ 3 w 1* rr)) (*(/355)(*2k5)) (* (/ 3 w 1) (/) (* r r)) valid invalid invalid invalid invalid invalid invalid valid invalid Result Remark - operator needs at least one operand Expression cannot be empty (just for this project) two operators one after the other not prefixed not balanced two operators without proper parethesis Your program should be smart enough to recognize tokens even if there is no space division operator needs at least one operand Example: > import java.util.*; > String test1 = "(+ (- 6 7) (* 234 455 256) (/ (/3) (*) (-2 3 1)))";; > result = ExpressionEvaluator.evaluate (test1); > System.out.println(result) > 2.7256318833333332E7 > String test2 = "(+ (- 632) (* a 3 b c) (/ (+ 32) (*) (- c 3 d)))" > result = ExpressionEvaluator.evaluate (test2); < > What is the value of 'a'? > 5 // accepts 5 from the user > What is the value of 'b'? > 6 // accepts 6 from the user > What is the value of 'c'? // accepts 22 from the user > What is the value of 'c'? // accepts 10 from the user > What is the value of 'd'?// accepts 9 from the user > > 1332.0 System.out.println(result) > String test3 = (+ (-6) (* abb3c4bc))"; > result = ExpressionEvaluator.evaluate (test3); < > What is the value of 'a'? // accepts 1 from the user > What is the value of 'b'? // accepts 6 from > What is the value of 'b'? // accepts 0 from the user > What is the value of 'c'? // accepts 10 from the user > What is the value of 'b'?// accepts 19 from the user > What is the value of 'c'?// accepts 95 from the user > System.out.println(result) > -6.0 (60pts) This class has static methods that will help us evaluate Lisp arithmetic expressions. Before a List expression is evaluated, we have to make sure that it is a valid expression. The following expressions show valid and invalid Lisp arithmetic expressions. Valid Expression (+ (-5) ) (* 4 4 5) (322) (* 4 4) Invalid Expressions (+ (-) ) (* 4 4 5) (/3 8 11) (* 4 4) (+ (-9) (* 4 4 5) (* (/ 3 6 7) (* 1 1) (+ (-2) (* 4 4 5) (/ 3 2 1)) (* c 6) (+ (-2) (* 3 3 4) ((* (/ 3 1 1) (* a b) ) (40pts) public static Double evaluate(String expr) - a method that accepts a Lisp expression and returns the result. The method returns a RunTimeException if the accepted expression is not a valid Lisp expression. The following can be reasons for the method to throw the exception: o An empty expression (). o Unbalanced expression. o Invalid expression. See the following examples of invalid expressions. o If the expression does not adhere to the rules outlined in the "Overview" section, it is invalid. For this project, if an expression contains operators other than the basic four arithmetic operators it will be considered as an operand. An expression can contain a single letter variables as operands. If an expression contains variable operands, it should prompt the programmer for values of the operands and use the values for the evaluation. For the sake of simplicity, only integer values are going to be accepted from the user. Note that if an expression contains operands such as var, it will consider it as three operand tokens v, a, r and prompt the programmer for the three values. Take a look at the example given below. The following table gives examples of invalid/valid Lisp expressions. Expression (* 3255) (-) () (/* w 1) (a + b + c + d) (* (/ 3 w 1) (* (r r)) (* (/ 3 w 1* rr)) (*(/355)(*2k5)) (* (/ 3 w 1) (/) (* r r)) valid invalid invalid invalid invalid invalid invalid valid invalid Result Remark - operator needs at least one operand Expression cannot be empty (just for this project) two operators one after the other not prefixed not balanced two operators without proper parethesis Your program should be smart enough to recognize tokens even if there is no space division operator needs at least one operand Example: > import java.util.*; > String test1 = "(+ (- 6 7) (* 234 455 256) (/ (/3) (*) (-2 3 1)))";; > result = ExpressionEvaluator.evaluate (test1); > System.out.println(result) > 2.7256318833333332E7 > String test2 = "(+ (- 632) (* a 3 b c) (/ (+ 32) (*) (- c 3 d)))" > result = ExpressionEvaluator.evaluate (test2); < > What is the value of 'a'? > 5 // accepts 5 from the user > What is the value of 'b'? > 6 // accepts 6 from the user > What is the value of 'c'? // accepts 22 from the user > What is the value of 'c'? // accepts 10 from the user > What is the value of 'd'?// accepts 9 from the user > > 1332.0 System.out.println(result) > String test3 = (+ (-6) (* abb3c4bc))"; > result = ExpressionEvaluator.evaluate (test3); < > What is the value of 'a'? // accepts 1 from the user > What is the value of 'b'? // accepts 6 from > What is the value of 'b'? // accepts 0 from the user > What is the value of 'c'? // accepts 10 from the user > What is the value of 'b'?// accepts 19 from the user > What is the value of 'c'?// accepts 95 from the user > System.out.println(result) > -6.0
Expert Answer:
Related Book For
Computer Architecture A Quantitative Approach
ISBN: 978-0123704900
4th edition
Authors: John L. Hennessy, David A. Patterson
Posted Date:
Students also viewed these programming questions
-
A point charge Q is located on the axis of a disk of radius R at a distance b from the plane of the disk (Fig. P24.68). Show that if one fourth of the electric flux from the charge passes through the...
-
Investors know that bonds issued by different economic entities have different probabilities of defaulting. Which one of the following is not one of them? 1) The U.S. government has defaulted on its...
-
(a) Let k be any real number and a < 1. Show that the function (1+x)k has the following power series representation (1 + x) = (h)a = 1+ kx + ? -x + k(k-1) 2! n=0 The above series is called the...
-
Emerald Statuary manufactures bust statues of famous historical figures. All statues are the same size. Each unit requires the same amount of resources. The following information is from the static...
-
In statistics, the phrase correlation does not imply causation is used to emphasize that correlation between two variables does not automatically imply that one causes the other. Can you think of two...
-
Kronenberger Burgoyne, LLP, was a law firm with two equity partners who agreed to equal ownership as of 2009. Before 2009, Kronenberger had owned a majority interest in the firm, and when, in 2011,...
-
Haver Company currently produces component RX5 for its sole product. The equipment that is used to produce RX5 must be replaced, and management must decide whether to replace the equipment or buy RX5...
-
Discuss the market concentration, competitiveness, and performance of the Australian grocery industry compared to other western countries. Identify the key stakeholders in the food supply chain, and...
-
LEGO is one of the worlds most recognizable toys. The small, colorful building blocks have spawned countless sets, figurines, video games, and even movies and theme parks. LEGO is built on a very...
-
I'm working on a HTML webpage lab that I'm not sure how to continue in the script.js page in the Visual Studio Code, can someone help? createCourseArray() It should look for elements that have course...
-
TexMex Food Company is considering a new salsa whose data are shown below. Under the new tax law, the equipment to be used in the project is eligible for 100% bonus depreciation, so it will be fully...
-
Can you raise one eyebrow at a time? Use the students in your statistics class (or a group of friends) to estimate the percentage of people who can raise one eyebrow at a time. How can your result be...
-
Q9: The circuit in the diagram contains capacitors connected in series and in parallel. The total capacitance of the circuit is 36 F. What is the capacitance C? HH C 95 F 25 F
-
blake and miranda are in the middle of bitter divorce proceedings , Jeremey CPA agrees to prepare net worth statements for both individuals and has promised an arbitrator that he will present numbers...
-
12. White is a customer of Romero Supply. White has requested an extension of time to setting his $60,000 account with Romero. Rather than simply extending the time, on 12/1, Romero accepts a...
-
III. Given the following graph: M a) Give the depth-first traversal of the graph, starting from vertex K. (in case you have the choice between multiple vertices to visit, always select the vertex...
-
X-1 Find the domain of the function f(x) : x 1 2 - O (-00, -1) U (-1, ) O (-00, 1) U (1, ) O -00, -1) U (-1, 1) U (1, 0) O (- 1, 1)
-
Some applications read a large data set first, then modify most or all of it. The base MSI coherence protocol will first fetch all of the cache blocks in the Shared state, and then be forced to...
-
In the case of a cache miss, both the switched snooping protocol described earlier and the directory protocol in this case study perform the read or write operation as soon as possible. In...
-
In a server farm such as that used by Amazon or the Gap, a single failure does not cause the whole system to crash. Instead, it will reduce the number of requests that can be satisfied at any one...
-
Which of the following is not an inherent part of Statement on Auditing Standards, No. 99/113? 1. Greater scrutiny of the chief executive and chief financial officers personal financial condition 2....
-
Which of the following statements best describes corporate governance with respect to fraud? 1. Auditors are primarily responsible for the detection of fraud, the Board of Directors for the...
-
Which of the following is not a reason that the prevention and detection of fraud resulting from management override and collusion presents a significant challenge for the antifraud community? 1....
Study smarter with the SolutionInn App