Question: Revise the given program so that the user can use the terminal and run the program in the following formats: java Calculator 4 + 5,

 Revise the given program so that the user can use theterminal and run the program in the following formats: java Calculator 4

Revise the given program so that the user can use the terminal and run the program in the following formats: java Calculator 4 + 5, java Calculator 4+5, java Calculator 4+ 5, java Calculator 4 +5. Notice some of the "4 + 5" have no spaces or double spaces between each character.

*10.26 (Calculator) 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: X G. Administrator: Command Prompt O O C:\exercise >java Exercise1_26 "4+5" 4 + 5 = 9 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 * 5" 4 * 5 = 20 C:\exercise 3 LISTING 7.9 Calculator.java 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(1); 9 5 10 11 // The result of the operation int result = 0; 13 14 15 16 17 // Determine the operator switch (args[1].charAt(0)) { case '+': result = Integer.parseInt(args[0]) + Integer.parseInt(args[2]); break: case '-': result = Integer.parseInt(args[0]) - Integer.parseInt(args[2]); break; case '.': result = Integer.parseInt(args[0]) * Integer.parseInt(args[2]); break; case '/': result = Integer.parseInt(args[0]) / Integer.parseInt(args[2]); 28 29 // Display result System.out.println(args [O] + ' ' + args[1] + ' ' + args[2] + " = " + result); 31 } 32 33 }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!