Using Java Write a program called Arithmetic that takes three command-line argum
ID: 3700936 • Letter: U
Question
Using JavaWrite a program called Arithmetic that takes three command-line arguments: two integers followed by an arithmetic operator (+, -, * or /). The program shall perform the corresponding operation on the two integers and print the result.
Using Java
Write a program called Arithmetic that takes three command-line arguments: two integers followed by an arithmetic operator (+, -, * or /). The program shall perform the corresponding operation on the two integers and print the result.
Write a program called Arithmetic that takes three command-line arguments: two integers followed by an arithmetic operator (+, -, * or /). The program shall perform the corresponding operation on the two integers and print the result.
Explanation / Answer
public class Arithmatic {
public static void main(String[] args) {
int num1 = Integer.parseInt(args[0]);
int num2 = Integer.parseInt(args[1]);
String operator = args[2];
if(operator.equalsIgnoreCase("+")) {
int addition = num1+num2;
System.out.println("Addition is : "+ addition);
}
else if(operator.equalsIgnoreCase("-")) {
int subtraction = num1-num2;
System.out.println("Subtraction is : "+ subtraction);
}
else if(operator.equalsIgnoreCase("'*'")) {
int multiplication = num1*num2;
System.out.println("Multiplication is : "+ multiplication);
}
else if(operator.equalsIgnoreCase("/")) {
int division = num1/num2;
System.out.println("Division is : "+ division);
}
}
}
We used '*' for multiplication. jvm interprets * as "all files and folders in the current directory"
thats why we user '*'
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.