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: 69% (13 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...
-
Bramble company purchased equipment for $220,800 on October 1, 2020. It is estimated that the equipment will have a useful life of 8 years and a salvage value of $12,000. Estimated production is...
-
5. Pop Corporation sells an inventory item to its subsidiary, Son Company, to be used as a plant asset by Son. The workpaper entry to eliminate intercompany profits in the year of sale will not...
-
You are opening a summer business, a chain of three drive-through snow-cone stands. You have hired other college students to work and have purchased a cash register with locked-in tapes. You retain...
-
14. Which of the following is not an indicator that a promise is separately identifiable within a contract? does not modify another good or service lack of commercial substance like other goods or...
-
Two blocks, of masses M = 2.0 kg and 2M, are connected to a spring of spring constant k = 200 N/m that has one end fixed, as shown in Figure. The horizontal surface and the pulley are frictionless,...
-
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.
-
Develop sketches of reactor relief systems for the following four cases: Reactor relief is vapor only Reactor relief is two-phase flow Reactor contents are corrosive Reactor contents are plugging...
-
Why is it critical to immediately contact your Engagement Partner when you suspect or identify non-compliance? He or she will ensure that the non-compliance doesn't affect the Client's reputation He...
-
Question 9: Determine the current and its direction, in each resistor, for the circuit shown below. Show your calculations. R=152 9.0 V + 12V ww R=75 2 R3= 50
-
how can The High - Tech Way To Recycle Clothes sustainable. and what they offer and what are their ecofriendly
-
James Bondbuyer purchases a Treasury bond on Monday, May 2, regular way settlement. The bond pays interest on January 15 and July 15. How many days of accrued interest will be owed to the seller? A...
-
Aviation and air traffic control have come a long way in the last 100-years. Some believethat we have reached a plateau and that growth in aviation will stop. Aviation may go the way of the railroads...
-
Under what circumstances will absorption costing report higher profits than variable costing?
-
In Problem use geometric formulas to find the unsigned area between the graph of y = f(x) and the x axis over the indicated interval. f(x) = x + 5; [0, 4]
-
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...
-
Suppose you bought a bon with an annual coupon rate of 6.5 percent one year ago for $1,032. The bond sells for $1,020 today. a. Assuming a $1,000 face value, what was your total dollar return on this...
-
During the year 2021, William has a job as an accountant, he earns a salary of $100,000. He has done some cleaning services work on his own (self-employed), where he earned a net income of $50,000....
-
Fixed cost per unit is $7 when 25,000 units are produced and $5 when 35,000 units are produced. What is the total fixed cost when 30,000 units are produced? Group of answer choices $150,000....
Study smarter with the SolutionInn App