Revise Listing 7.9, Calculator.java, to accept an expression as a string in which the operands and operator
Question:
Revise Listing 7.9, Calculator.java, to accept an expression as a string in which the operands and operator are separated by zero or more spaces. For example, 3+4 and 3 + 4 are acceptable expressions. Here is a sample run:java Exercise10_26 "4 + 5" style="" class="fr-fic fr-dib">
Listing
Transcribed Image Text:
х C. Administrator: Command Prompt c:\exercise>java Exercise10 26 "4+5" 4 + 5 = 9 c:\exercise>java Exercise10_26 "4 + 5" 4 + 5 = 9 c:\exercise>java Exercise10_26 "4 + 4 + 5 = 9 5" c:\exercise>java Exercise10_26 "4 * 4 * 5 = 20 c:\exercise> 1 public class Calculator { /** Main method */ public static void main(String [] args) { // Check number of strings passed if (args.length != 3) { System.out.println( "Usage: java Calculator operand1 operator operand2"); System.exit(0); 2 3 4. 8. 9. 10 // The result of the operation int result = 0; 11 12 13 14 15 16 17 18 19 20 21 // Determine the operator switch (args[1].charAt(0)) { case '+': result = Integer.parseInt(args[0]) + Integer.parseInt(args[2]); break; ': result = Integer.parseInt(args[0]) - case Integer.parseInt(args[2]); break; 22 case '.': result = Integer.parseInt(args[0]) * Integer.parseInt(args[2]); 23 24 break; case '/': result = Integer.parseInt(args[0]) / Integer.parseInt(args[2]); 25 26 27 28 // Display result System.out.println(args [0] + + result); 29 30 + args[1] + + args[2] 31 32 33 }
Fantastic news! We've Found the answer you've been seeking!
Step by Step Answer:
Answer rating: 50% (2 reviews)
Program Plan 1 Create a class Calculator 2 Declare and initialize the variable int result 0 calculat...View the full answer
Answered By
Ashish Jaiswal
I have completed B.Sc in mathematics and Master in Computer Science.
4.90+
20+ Reviews
39+ Question Solved
Related Book For
Introduction to Java Programming, Comprehensive Version
ISBN: 978-0133761313
10th Edition
Authors: Y. Daniel Liang
Question Posted:
Students also viewed these Computer science questions
-
Revise Listing 3.8, Lottery.java, to generate a lottery of a threedigit number. The program prompts the user to enter a three-digit number and determines whether the user wins according to the...
-
Revise Listing 3.8, Lottery.java, to generate a lottery of a twodigit number. The two digits in the number are distinct.? Listing 3.8 1 import java.util.Scanner; 2 3 public class Lottery { public...
-
Revise Listing 8.3, FindNearestPoints.java, to display all closest pairs of points with the same minimum distance. Here is a sample run: Listing Enter the number of points: 8-Enter Enter 8 points: 0...
-
Which of these groups has the higher priority? b) -COH or CNH2 a) -CH3 or -CH,CH3 c) -CH;CH,CH3 d) -CH-CH CH3 or -C=N or -CH;CH,CH,CH3 CH3 e) -CHCH3 or
-
Let H be the set of all vectors of the form Where t is any real number. Show that h is a subspace of R3. (Use the method of Exercise 9.) 3: 0-7
-
Name the following compounds. Be sure to designate the principal chain properly before constructing the name. (a) CH3CH-CH-CH-CHCHCH3 T CH3 (b) | CH T CH CH CH3 CH3 1 CH3CHCHCH-C- T T CH3 CH3 CHCH3 T...
-
In 2015, the city of San Francisco enacted an ordinance that required health warnings on advertisements for certain sugar-sweetened beverages (SSBs) that read: WARNING: Drinking beverages with added...
-
Youve entered into a contract to purchase a new house, and the closing is scheduled for next week. Its typical for some last-minute bargaining to occur at the closing table, where sellers often try...
-
Does the monopoly power wielded by professional sports leagues hurt the fans, players, citizens, local economies, or the sport? What solutions could be used ?
-
Calculate the Lifo, Fifo, and Cost of Good sold. Inventory Costing Methods 5 6 7 B 9 0 1 2 3 34 25 26 27 28 Date Item 1-May Beginning Inventory 8-May Purchase 15-May Purchase 22-May Purchase 29-May...
-
Design a class named Time. The class contains:??The data fields?hour,?minute, and?second?that represent a time.? A no-arg constructor that creates a Time object for the current time. (The values of...
-
The split method in the String class returns an array of strings consisting of the substrings split by the delimiters. However, the delimiters are not returned. Implement the following new method...
-
How many of the 16 different relations on {0, 1} contain the pair (0, 1)?
-
Trevor Wilson is 27 years old. He works for a company with a good health insurance plan. Indeed, Trevor has heard it's priced better than most plans and has consequently taken out the optional...
-
Judith will retire this year and has $560,000 in retirement assets available to her. She is planning for a 20 year retirement and has been getting a safe 10% rate of return on her assets What amount...
-
In 2021, Kevin had total sales of $160,000, of which $65,000 were on account. On December 31, 2021, there are receivables of $42,000. He estimates that $4,000 of these receivables are doubtful of...
-
Tony owns a clothing store in Melbourne. The following transactions occurred in the September 2022 quarter: Cash sales $65,000 Credit sales (accounts receivable customers) 120,000 Cash purchases...
-
55 years old. $5,431 fully franked dividends with $2,327 of franking credits. The taxpayer received a cash payment of $5,431. No spouse. Earned $85,000 from working, with $21,000 withheld, therefore...
-
On January 1, 2017, Garzon purchased 6% bonds (held-to-maturity) issued by PBS Utilities at a cost of $40,000, which is their par value. The bonds pay interest semiannually on July 1 and January 1....
-
Annual dividends of ATTA Corp grew from $0.96 in 2005 to $1.76 in 2017. What was the annual growth rate?
-
A buffer ____________. A. Holds bytes that are scheduled for input or output B. Deteriorates program performance C. Cannot be flushed in java D. All of the above are true.
-
inputstream is ____________. A. A child of outputstream B. An abstract class C. Used for screen output as opposed to file output D. All of the above are true.
-
Javas print() and println() methods are defined in the ____________ class. a. BufferedOutputStream b. System c. PrintStream d. Print
-
(r. 8, 4) R21(r)Y1,0(0, 0)| b. (r. 8.) = R10 (r)Y0,0(8.) c. (r. 6.) = R43(r)Y3,0(0,0) d. (r. 6, ) = R22 (r)Y1,0(0, 0) (4) Which of the above states do not exist? And why?
-
What is the wavelength of radiation that has a frequency of 6.912*1014 s? (c = 3.0*108 m/s *
-
Heterocyclic compounds are essential component of many known drugs. In this assignment you need find 5 of these heterocyclic compound-containing drugs. And for each of them you need to find the...
Study smarter with the SolutionInn App