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...
-
A study showed that 60 percent of The Wall Street Journal subscribers watch CNBC every day. Of these, 70 percent watch it outside the home. Only 20 percent of those who don't watch CNBC every day...
-
What is the difference between a predator and a situational (accidental) fraudster?
-
On July 1, 2012, Gissel Corporation purchased Mills Company by paying $250,000 cash and issuing a $150,000 note payable. At July 1, 2012, the balance sheet of Mills Company was as follows. The...
-
6. (a) Given == 2-23i.Find |=| and arg (=*). Hence, solve z=2-23i leave your answers in polar form. (b) Given that z = 1 + i is a root of the polynomial equation z4-4z3 +11z2 - 14z + 10 = 0. Find the...
-
A cylindrical specimen of a metal alloy 10 mm (0.4 in.) in diameter is stressed elastically in tension. A force of 15,000 N (3,370 lbf) produces a reduction in specimen diameter of 7 10-3 mm (2.8 ...
-
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.
-
Does the fact that world trade has increased at a much faster rate than world GDP (at least up until the financial crisis) highlight the limitations of trade as a driver of growth?
-
Trippett Industries manufactures cleaning products. During the year, the company spent $607,000 on chemicals and $748,000 on conversion costs. Overhead is allocated at a rate of 175% of direct labor...
-
Flounder Corp. erected and placed into service an offshore oil platform on January 1, 2020, at a cost of $ 12 million. Flounder is legally required to dismantle and remove the platform at the end of...
-
Greener Pastures Corporation borrowed $1,800,000 on November 1, 2018. The note carried a 8 percent interest rate with the principal and interest payable on June 1, 2019. ( a ) The note issued on...
-
Most Company has an opportunity to invest in one of two new projects. Project Y requires a $315,000 investment for new machinery with a six-year life and no salvage value. Project Z requires a...
-
Elaborate whyis a link-state routing protocol like a road map? Discuss it with an example.
-
Two steel wires (of the same length and different radii) are connected together, end to end, and tied to a wall. An applied force stretches the combination by 1.0 mm. How far does the midpoint move?...
-
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...
-
Rewrite the following function so that it can operate on arrays of any data type, not just integers. You may assume that all operations and comparisons are defined for all data types that we would...
-
Write the complexity of the following code in Big-O notation: int index = -1; for (int i = 0; i < arr.length && index < 0; i++) { if (arr[i] == target) { index = i; } }
-
Compare differing conceptualizations of the mind and how the mind is studied. Address the influence of internal and environmental conditions on what is recalled from certain kinds of...
Study smarter with the SolutionInn App