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% (4 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
-
If the FCC concludes that an indecency violation has occurred, what will the fine be?
-
EXPECTATIONS THEORY AND INFLATION Suppose 2-year Treasury bonds yield 4 5%, while 1-year bonds yield 3%. r* is 1%, and the maturity risk premium is zero. a. Using the expectations theory, what is the...
-
On the basis of the following data, for Teller Co. for 2006 and the preceding year ended December 31, 2005, prepare a statement of cash flows in excel. Use the indirect method of reporting cash flows...
-
Can someone help me solve this Problem 3 Calculate the EUAW for the cash flow shown below. Draw a cash flow diagram. The MARR is 9% (10 points). 7good Year Cash Flow 0 1 point 2 3 -$300,000 $75,000...
-
An experiment was conducted to test the efficacy of chloromycetin in checking typhoid. In a certain hospital chloromycetin was given to 285 out of the 392 patients suffering from typhoid. The number...
-
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...
-
Ed owns 500 shares of Northern Company for which he paid $15,000 several years ago. On November 24, he purchases an additional 350 shares for $6,300. Ed sells the original 500 shares for $10,000 on...
-
Use the following information for questions 1 and 2. Caterpillar Financial Services Corp. (a subsidiary of Caterpillar) and Sterling Construction sign a lease agreement dated January 1, 2020, that...
-
Porch Pirates An InsuranceQuotes.com survey showed that 8% of Americans had a holiday package stolen from outside their front door. Consider the random selection of four Americans. Use the...
-
GATE 2024-EE Question
-
GATE 2024-EE Question
-
GATE 2024-EE Question
-
If necessary, create a new project named Advanced28 Project and save it in the Cpp8\Chap11 folder. Also create a new source file named Advanced28.cpp. The program should declare a 20-element,...
-
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
-
You need to calculate the total number of items ordered by customers from Texas. Two-letter state abbreviations are in Column B and quantity ordered is in Column G. There are 200 rows in the...
-
Cully Company needs to raise $22 million to start a new project and will raise the money by selling new bonds. The company will generate no internal equity for the foreseeable future. The company has...
-
A.1)If the inflation rates in the British pound and the US $ are 4% and 1% respectively, while the exchange rate is 1 pound exchanges for $2, what will the future spot rate be? 2) What is the meaning...
Study smarter with the SolutionInn App