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...
-
Components of an investment policy statement include all of the following except: A. Asset allocation strategy. B. Client constraints. C. Duties and responsibilities of the client and the personal...
-
Presented below are the balance sheet accounts of the Mayfair Company as of December 1, 2017. The accounts are listed in random order. During the month of December 2017, the following events...
-
This problem continues the Draper Consulting, Inc., situation from P21-35 of Chapter 21. Assume Draper Consulting began January with $29,000 cash. Management forecasts that collections from credit...
-
11) Determine the future value of the following scenarios: a) Annual deposits of $7,596 for 30 years in an account paying 12% compounded annually? b) Quarterly deposits of $1,899 for 30 years in an...
-
Which one of the following amounts is capitalized and depreciated? $40 paid for an oil change in one of the companys delivery vehicles $7,000 paid for closing costs on the acquisition of land on...
-
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.
-
Using simple exponential smoothing and the following time series data, respond to each of the items. (Note the data are the same as exercise 8.) a. G raph the time series data. What do you observe?...
-
Rewrite the save and restore routines in the language of your choice so that they employ the push and pop procedures.
-
Rewrite the save and restore routines in 2-address code, assuming block move (BMOVE) and restore (BRESTORE) instructions are available. Make the necessary assumptions about the format of the...
-
In recent years, some teams names and mascots have come to be considered offensive. Do you think this is a binary variable? Explain your answer, with examples.
-
Rewrite the save and restore routines so that they save and restore to the head and tail of a ring buffer, respectively.
-
Rewrite the save and restore routines assuming that eight general register (R0R7) and the program counter are to be saved on a stack. Do this for (a) 0-address machine (b) 1-address machine (c)...
-
Paul Howard, the new plant manager of Garden Scapes Manufacturing Plant Number 7, has just reviewed a draft of his year-end financial statements. Howard receives a year-end bonus of 11.5% of the...
-
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 bearish on D&K App. and decide to sell short 100 shares at the current market price of $22.80 per share, how high can the stock price of D&K App go before you get a margin call if the...
-
The requires investment companies to register with the Securities and Exchange Commission (SEC) and to fulfill certain disclosure requirements. a. Investment Company Act of 1940 b. Sarbanes-Oxley Act...
-
Statement: Maintenance records indicate that each of a company's five CNC machines need maintenance after 20 hrs of use. The breakdowns have been determined to be Poisson distributed. The one...
Study smarter with the SolutionInn App