Question: Could you please help me on this JAVA program please. I really appreciate your help Calculator.java, is a simple command-line calculator. Note that the program

Could you please help me on this JAVA program please. I really appreciate your help

Calculator.java, is a simple command-line calculator. Note that the program terminates if any operand is nonnumeric. Write a program with an exception handles that deals with nonnumeric operands; then write another program without using an exception handler to achieve the same objective. Your program should display a message that informs the user of the wrong operand type before exiting (see below command prompt).

Command prompt:

C:\exercise>java Exercise12_01 4+5

4+5 = 9

C:\exercise>java Exercise12_01 4-5

4-5 = -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);

}

// The result of the operation

int result = 0;

// 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]);

}

// Display result

System.out.println(args[0] + ' ' + args[1] + ' ' + args[2]

+ " = " + result);

}

}

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!