(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:
Answer rating: 100% (QA)
This program checks if a given string is a valid JavaScript expression using the ScriptEngineManager import javaxscriptScriptEngine import javaxscript... View the full 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...
-
describe if and how the organization that you represent use crew resource management. Do you see value in the use of crew resource management
-
(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...
-
The file fast food contains the amount that a sample of fifteen customers spent for lunch ($) at a fast food restaurant: 7.42 6.29 5.83 6.50 8.34 9.51 7.10 6.80 5.90 4.89 6.50 5.52 7.90 8.30 9.60 a....
-
Is Best Buy focusing too much or too little on cost reduction? Explain.
-
Collection of Amounts Previously Written Off} Hannah purchased a laptop computer from Perry Corp. for \(\$ 1,500\). Hannah's receivable has been outstanding for over 180 days, and Perry determines...
-
The Lamplighter Company, the lessor, agrees to lease equipment to Tilson Company, the lessee, beginning January 1, 2007. The lease terms, provisions, and related events are as follows: The lease is...
-
Moe Electronics produces APP switches for NASA. The current assembly capacity is 100 switches per hour. It takes 40 minutes to receive the necessary component from the previous workstation. Completed...
-
What type of communication is happening when technology is used to send messages to a large, mixed audience? Discuss
-
1. The average weight of 6 people in an elevator is 173 pounds. A 7 th person gets on and now the average weight is 165 pounds. How much did the 7 th person weigh? 2. Use the following data obtained...
-
A compressed-air storage system uses excess electricity to power a compressor that fills a tank with pressurized air. The tank has a volume of 100 m and initially holds ambient air (290 K, 1 bar)....
-
Suppose you want to buy a stock with a 5 0 % initial and 3 0 % maintenance margin. The stock is worth $ 1 0 0 . a . What is the equity in your initial position? What is your leverage? b . Suppose the...
-
The total cost, in dollars, of manufacturing x calculators is given by C(x) = 10 x + 1000. a. What is the total cost of manufacturing 100 calculators? b. What is the rate of change in the total cost...
-
If P(t)= (cost, sint) has coordinates (0.994,0.11), find the coordinates of a. P(t + ) X = b. P(-t) x = c. P(t - ) X = c. P(-t-T) x = y = y = y = y = III
-
op1.fun - Downloa... Nelnet Atmospheric House... Question 4 of 10 ' Sold 10 bicycles from the October 1 inventory to Team America for $250 each, terms 2/10, n/30. 6 7 Received credit from the Nixon...
-
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...
-
No. The break in an investor-owned firm's MCC schedule is due to the higher cost involved with issuing new common stock once the firm's retained earnings has been exhausted. Since not-for-profit...
-
Since not-for-profit businesses are expected to provide a social value in addition to an economic benefit, project analysis must consider social value along with expected cash flows. The summation of...
-
a. Without access to tax-exempt debt, all of the benefits to using debt for a not-for-profit firm would disappear. Thus, in accordance with MM capital structure theory, and considering financial...
Study smarter with the SolutionInn App