Write a menu driven java program using command line arguments for the following:
1. Addition
2. Subtraction
3. Multiplication
4. Division.
import java.io.*;
public class
CommandLineCalculator {
public static void main(String[] args) {
if (args.length != 3) {
System.out.println("CommandLine Argument");
return;
}
String operation = args[0];
double num1 =
Double.parseDouble(args[1]);
double num2 =
Double.parseDouble(args[2]);
double result = 0;
switch (operation) {
case "1":
result = num1 + num2;
System.out.println("Addition Result: " + result);
break;
case "2":
result = num1 - num2;
System.out.println("Subtraction Result: " + result);
break;
case "3":
result = num1 * num2;
System.out.println("Multiplication Result: " + result);
break;
case "4":
if (num2 == 0) {
System.out.println("Error: Division by zero is not allowed.");
} else {
result = num1 / num2;
System.out.println("Division Result: " + result);
}
break;
default:
System.out.println("Invalid operation. Please choose 1 for
Addition, 2 for Subtraction, 3 for Multiplication, or 4 for Division.");
}
}
}
Output-
C:\Program
Files\Java\jdk1.7.0_80\bin>javac CommandLineCalculator.java
C:\Program
Files\Java\jdk1.7.0_80\bin>java CommandLineCalculator 1 4 5
Addition Result: 9.0
C:\Program
Files\Java\jdk1.7.0_80\bin>java CommandLineCalculator 2 4 2
Subtraction Result: 2.0
C:\Program Files\Java\jdk1.7.0_80\bin>