
1.Write a class Calculator, that will take two command line double arguments and ââ?¬Ë?+ââ?¬â?¢,ââ?¬â?¢-ââ?¬Ë?,ââ?¬â?¢/ââ?¬â?¢ or ââ?¬Ë?*ââ?¬â?¢. Signal an error if the user does not give the arguments. In the class declare two Double type instance variables. Based on arg3, perform the corresponding mathematical operation and print the result. Hint : use the switch statement and valueOf method .

import java.util.*;
class Calculator
{
public static void main(String[] args)
{
boolean exit=false;
Scanner input=new Scanner(System.in);
System.out.print("Enter number1: ");
double num1=input.nextDouble();
System.out.print("Enter number2: ");
double num2=input.nextDouble();
System.out.println("1 Addition");
System.out.println("2 Subtraction");
System.out.println("3 Multiplication");
System.out.println("4 Division");
System.out.println("5 Exit");
do{
System.out.print("Enter your choice: ");
int choice=input.nextInt();
switch(choice){
case 1:
double add=num1+num2;
System.out.println("Addition="+add);
break;
case 2:
double sub=num1-num2;
System.out.println("Subtraction="+sub);
break;
case 3:
double mul=num1*num2;
System.out.println("Multiplication="+mul);
break;
case 4:
double div=num1/num2;
System.out.println("Division="+div);
break;
case 5:
exit=true;
break;
}
}
while(!exit);
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.