Modify Listing 20.12, EvaluateExpression.java, to add operators ^ for exponent and % for remainder. For example, 3
Question:
Modify Listing 20.12, EvaluateExpression.java, to add operators ^ for exponent and % for remainder. For example, 3 ^ 2 is 9 and 3 % 2 is 1. The ^ operator has the highest precedence and the % operator has the same precedence as the * and / operators. Your program should prompt the .user to enter an expression. Here is a sample run of the program:
Data from Listing 20.12,
Transcribed Image Text:
1 1mport java.util.Stack; 3 publ1c class EvaluateExpression { publ1c stat1c void main(String[] args) { 11 Check number of arguments passed 1f (args.length 1= 1) { System.out.printin( "Usage: java EvaluateExpression \"expression\""): System.exit (1); 4 6 7 8 10 11 try { System.out.println(svaluateExpression (args[0]): 12 13 14 15 catch (Exception ex) { 16 System.out.printin("Wrong expression: " + args[0]): 17 18 19 1** Evaluate an expression / publ1c static 1nt evaluateExpression (String expression) { II Create operandStack to store operands Stack
1 1mport java.util.Stack; 3 publ1c class EvaluateExpression { publ1c stat1c void main(String[] args) { 11 Check number of arguments passed 1f (args.length 1= 1) { System.out.printin( "Usage: java EvaluateExpression \"expression\""): System.exit (1); 4 6 7 8 10 11 try { System.out.println(svaluateExpression (args[0]): 12 13 14 15 catch (Exception ex) { 16 System.out.printin("Wrong expression: " + args[0]): 17 18 19 1** Evaluate an expression / publ1c static 1nt evaluateExpression (String expression) { II Create operandStack to store operands Stack operandStack = new Stack<> (): 20 21 22 23 24 II Create operatorStack to store operators Stack operatorStack = new Stack<> (); 25 26 27 II Insert blanks around (.). +. -. 1. and expression = insertBlanks (expression); 28 29 30 II Extract operands and operators String[] tokens = expression.split(" "): 31 32 33 II Phase 1: Scan tokens for (String token: tokens) { 1f (token. length () == 0)II Blank space continue; // Back to the while loop to extract the next token else if (token.charAt(0) == + || token.charAt (0) == -') { II Process all +. -. *./ in the top of the operator stack wh1le (!operatorStack.isEmpty () && (operatorStack.peek () == '+' II 34 35 36 37 38 39 40 41
Fantastic news! We've Found the answer you've been seeking!
Step by Step Answer:
Answer rating: 66% (12 reviews)
import javautil public class Exercise2023 public static void mainString args Scanner input new ScannerSystemin SystemoutprintEnter an expression String exp inputnextLine try Systemoutprintlnexp evalua...View the full answer
Answered By
Nyron Beeput
I am an active educator and professional tutor with substantial experience in Biology and General Science. The past two years I have been tutoring online intensively with high school and college students. I have been teaching for four years and this experience has helped me to hone skills such as patience, dedication and flexibility. I work at the pace of my students and ensure that they understand.
My method of using real life examples that my students can relate to has helped them grasp concepts more readily. I also help students learn how to apply their knowledge and they appreciate that very much.
4.00+
1+ Reviews
10+ Question Solved
Related Book For
Introduction To Java Programming And Data Structures Comprehensive Version
ISBN: 9780136520238
12th Edition
Authors: Y. Daniel Liang
Question Posted:
Students also viewed these Computer science questions
-
Excercise1 : Use Ipconfig and Nslookup to work with DNS. Open a command prompt window. To see a list of recent DNS lookups, type ipconfig /displaydns and press Enter. To perform a DNS lookup, type...
-
Enter the data from Example 1 in your calculator. a. Create a box-and-whisker plot using the data from Example 1. b. How would you determine if it would be appropriate to create a modified boxplot...
-
Modify your algorithm from Exercise 24.3-6 to run in O ((V + E) lg W ) time. (Hint: How many distinct shortest-path estimates can there be in V - S at any point in time?)
-
7. Arrange the following nitrogen containing compounds in decreasing order of basicity NH NH -H (P) (a) S>R>Q> P NO (R) (2) (b) P>Q>S>R -H (S) (c) P>Q>R>S (d) R>Q> P > S
-
Use the standard Normal table to find the following cut-off values for Z. Also sketch a standard Normal curve, and shade the region corresponding to the given probability. a. P (Z < z) = 0.5 b. P (Z...
-
The existence of an internal audit department is recognized as a strong element of a company's control environment. Internal auditors can perform financial audits (similar to that of the external...
-
Highjinks Corporation's sales department has estimated revenue of 250,000 for your division. 60% of this will be achieved in the first half-year and 40% in the remaining half-year. Variable operating...
-
The condensed product-line income statement for Dish N' Dat Company for the month of March is as follows: Fixed costs are 15% of the cost of goods sold and 40% of the selling and administrative...
-
Determine whether to reject or fail to reject Ho at the level of significance of a) a = 0.08 and b) = 0.05. Ho=138, Ha #138, and P = 0.0646. a) Do you reject or fail to reject Ho at the 0.08 level of...
-
The founder of Frenza asks us to assist her in accounting and analysis of the corporations bonds, which have an annual contract rate of 8%. She wants to know the business and accounting implications...
-
Write the following generic method using selection sort and a comparator: public static void selectionSort(E[] list, Comparator comparator) Write a test program that prompts the user to enter six...
-
Write a program that prompts the user to enter a text file name and displays the number of vowels and consonants in the file. Use a set to store the vowels A, E, I, O, and U.
-
Describe the role of the paralegal in the various stages of the divorce process.
-
According to Kang Lee, what are the three common beliefs about children and lying? Are these beliefs correct? Describe the experiment that Lee and his team used to examine lying among children. What...
-
What is the maximum amount an individual with a wage income of $35,000 can contribute to an IRA? What is the deadline to make a contribution to the retirement plan? Under what circumstances can an...
-
Special economic zones, duty drawbacks and subsidies have often been used as part of an export orientated development strategy. This combined with the recent use of domestic subsidies by the USA to...
-
Event General Environment Dimension Using deep-sea submersibles to take pictures of the oil leak a mile below the surface of the water BP stock prices falling 25 percent during the first month after...
-
For an integnal (f(x)dx, show that the two-point Gauss quadrature rule approximates to 1 f f(x)dx cf(x) + c f (x2) -1 where G = 1 = 1 2 = N 1 x 2 3 Im L
-
Explain the term power of the test.
-
Prove that the mean heat capacities C P H and C P S are inherently positive, whether T > T 0 or T < T 0 . Explain why they are well defined for T = T 0 .
-
Revise Listing 18.9 to develop a program that lets the user use the + and ? buttons to increase or decrease the current order by 1, as shown in Figure 18.12a. The initial order is 0. If the current...
-
Modify Listing 18.8, TowerOfHanoi.java, so that the program finds the number of moves needed to move n disks from tower A to tower B. Listing 1 import java.util.Scanner; 2 3 public class Tower0fHanoi...
-
Write a recursive method to return the number of uppercase letters in an array of characters. You need to define the following two methods. The second one is a recursive helper method. Write a test...
-
You are the accountant for Mon Inc., a manufacturer of automobiles. Mon Inc. has the following information available for you (assume there are no indirect materials). Only use numeric characters - do...
-
The ledger of Novak Corp. at December 31, 2022, after the books have been closed, contains the following stockholders' equity accounts. Preferred Stock (11,000 shares issued) Common Stock (300,000...
-
Your answer is partially correct. During 2023, Darwin Corporation started a construction job with a contract price of $4.20 million. Darwin ran into severe technical difficulties during construction...
Study smarter with the SolutionInn App