Requirements:
Create Calculator Application to have following functionality,
- Add 2 numbers
- Subtract 2 numbers
- Divide 2 numbers
- Multiply 2 numbers
- Take command line input for 2 numbers
- User can enter option based on the menu as shown below,
Menu
(1=Add, 2=Sub, 3=Div, 4=Mul )
Design:
Implementation:
[CalculatorApp.java]
import java.util.Scanner;
public class CalculatorApp {
public static void main(String[] args) {
//read command line arguments from the command line while running this application
double n1 = Double.parseDouble(args[0]); //first number
double n2 = Double.parseDouble(args[1]); //second number
//use to get user input for operation
Scanner scan = new Scanner(System.in);
System.out.println("Menu ");
System.out.println("(1=Add, 2=Sub, 3=Div, 4=Mul )");
int option = scan.nextInt();
CalculatorApp appC = new CalculatorApp();
switch (option) {
case 1 :
appC.add(n1, n2);
break;
case 2 :
appC.sub(n1, n2);
break;
case 3 :
appC.div(n1, n2);
break;
case 4 :
appC.mul(n1, n2);
break;
default :
System.out.println("Invalid option.");
}
} //end of main() method
//Method to add two numbers and display results
public void add(double n1, double n2) {
System.out.println("Adding...");
double result = n1 + n2;
System.out.println(result);
}
//Method to subtract two numbers and display results
public void sub(double n1, double n2) {
System.out.println("Subtracting...");
double result = n1 - n2;
System.out.println(result);
}
//Method to divide two numbers and display results
public void div(double n1, double n2) {
System.out.println("Dividing...");
double result = n1 / n2;
System.out.println(result);
}
//Method to multiply two numbers and display results
public void mul(double n1, double n2) {
System.out.println("Multiplying...");
double result = n1 * n2;
System.out.println(result);
}
}
Testing:
=============How to Compile using java command line without Eclipse
javac CalculatorApp.java
The above command will compile CalculatorApp.java to CalculatorApp.class
=============How to Run using Command prompt (such as dos/unix/linux)
java CalculatorApp 1 2
The above command will run CalculatorApp application with 2 arguments (Number1 and Number2)
=============Output: For Add option [other 3 options (Sub, Div, Mul) ]
Menu
(1=Add, 2=Sub, 3=Div, 4=Mul )
1
Adding...
3.0
Create Calculator Application to have following functionality,
- Add 2 numbers
- Subtract 2 numbers
- Divide 2 numbers
- Multiply 2 numbers
- Take command line input for 2 numbers
- User can enter option based on the menu as shown below,
Menu
(1=Add, 2=Sub, 3=Div, 4=Mul )
Design:
[CalculatorApp.java]
import java.util.Scanner;
public class CalculatorApp {
public static void main(String[] args) {
//read command line arguments from the command line while running this application
double n1 = Double.parseDouble(args[0]); //first number
double n2 = Double.parseDouble(args[1]); //second number
//use to get user input for operation
Scanner scan = new Scanner(System.in);
System.out.println("Menu ");
System.out.println("(1=Add, 2=Sub, 3=Div, 4=Mul )");
int option = scan.nextInt();
CalculatorApp appC = new CalculatorApp();
switch (option) {
case 1 :
appC.add(n1, n2);
break;
case 2 :
appC.sub(n1, n2);
break;
case 3 :
appC.div(n1, n2);
break;
case 4 :
appC.mul(n1, n2);
break;
default :
System.out.println("Invalid option.");
}
} //end of main() method
//Method to add two numbers and display results
public void add(double n1, double n2) {
System.out.println("Adding...");
double result = n1 + n2;
System.out.println(result);
}
//Method to subtract two numbers and display results
public void sub(double n1, double n2) {
System.out.println("Subtracting...");
double result = n1 - n2;
System.out.println(result);
}
//Method to divide two numbers and display results
public void div(double n1, double n2) {
System.out.println("Dividing...");
double result = n1 / n2;
System.out.println(result);
}
//Method to multiply two numbers and display results
public void mul(double n1, double n2) {
System.out.println("Multiplying...");
double result = n1 * n2;
System.out.println(result);
}
}
Testing:
=============How to Compile using java command line without Eclipse
javac CalculatorApp.java
The above command will compile CalculatorApp.java to CalculatorApp.class
=============How to Run using Command prompt (such as dos/unix/linux)
java CalculatorApp 1 2
The above command will run CalculatorApp application with 2 arguments (Number1 and Number2)
=============Output: For Add option [other 3 options (Sub, Div, Mul) ]
Menu
(1=Add, 2=Sub, 3=Div, 4=Mul )
1
Adding...