Let's learn how to create a calculator in java programming language and we will learn how it is working step by step. So hang-tight, today we will clear our basic concepts and get familiar with java by creating a basic calculator.
In my previous blog when I compared the java and python syntax, then I wrote that java’s syntax is little bit complex as it includes some more lines of code in comparison to python. Let us analyze it briefly.
Basically our calculator will work like this – on starting user will Enter two number one by one and then we will ask the user about the operators that are pre-defined inside the program as cases and the one user will choose will be applied on those two operands. If user entered a = 2 and b=4 and say chosen ‘+’ as operator, then a and b will be operand on which operator will operate the ‘+’ will perform the operation and finally we will output the result by assigning the value to result variable and then program will print it out and we will finally test the program.
So for this I am using VS Code but you can use any basic IDE (Integrated Development Environment) to write programs but you’ll need to have jdk (java development kit) installed. So let’s start writing step by step.
Step 1 – Declaring a suitable class and declaring a public static method inside it as:
Step 2 – Now we will take input and declare the variables result. But most important thing to be noted here is we need to update the library which contain the Scanner method to take the input from user and the library which have it is utility and we can import it as ‘import java.util.Scanner;’ and it need to be included at the top of the program because libraries crawls before the program start running.
We have asked user to enter the numbers one by one then fetched the numbers and assigned to a and b respectively. we are taking the integer as input. Now our next step will be to ask about what operations need to do on the numbers given by user. So, for this we will use switch case statement to verify the operator id. Basically we will give the id to operators like if user press 1 then program adds, 2 for subtraction, etc.
Step 3 – Here we are adding one more variable to capture the user’s choice and apply the operation on numbers and we will verify that user enter the correct choice or not using default case.
Step 4 – Finally after validation of correct choice we go through the switch case process and finally we will store the output to result variable and then display it to user and finally our program looks like this.
Complete Code:
See the output image below:

*Note:
- Please make sure you do not forget to give any ‘;’ as it will cause
error.
- Don’t forget to declare the variable before using.
- Make sure the number of cases you write is applicable or not?
- Try to keep a class name that suits your program to easily identify its class file after compiling.
We hope you liked the blog..Any comments or doubts are always welcome.
In my previous blog when I compared the java and python syntax, then I wrote that java’s syntax is little bit complex as it includes some more lines of code in comparison to python. Let us analyze it briefly.
Basically our calculator will work like this – on starting user will Enter two number one by one and then we will ask the user about the operators that are pre-defined inside the program as cases and the one user will choose will be applied on those two operands. If user entered a = 2 and b=4 and say chosen ‘+’ as operator, then a and b will be operand on which operator will operate the ‘+’ will perform the operation and finally we will output the result by assigning the value to result variable and then program will print it out and we will finally test the program.
So for this I am using VS Code but you can use any basic IDE (Integrated Development Environment) to write programs but you’ll need to have jdk (java development kit) installed. So let’s start writing step by step.
Step 1 – Declaring a suitable class and declaring a public static method inside it as:
Step 2 – Now we will take input and declare the variables result. But most important thing to be noted here is we need to update the library which contain the Scanner method to take the input from user and the library which have it is utility and we can import it as ‘import java.util.Scanner;’ and it need to be included at the top of the program because libraries crawls before the program start running.
import java.util.Scanner;
//declaring class
class calculator {
//declaring
the main function which returns nothing.
//main
function is executed first when program executes
public static void main (String args [])
{
int result,
a, b;
Scanner x = new Scanner(System.in);
System.out.println("Pease
Enter the First Number : ");
a = x.nextInt();
System.out.println("Please
Enter the Second Number :");
b = x.nextInt();
}
}
|
We have asked user to enter the numbers one by one then fetched the numbers and assigned to a and b respectively. we are taking the integer as input. Now our next step will be to ask about what operations need to do on the numbers given by user. So, for this we will use switch case statement to verify the operator id. Basically we will give the id to operators like if user press 1 then program adds, 2 for subtraction, etc.
Step 3 – Here we are adding one more variable to capture the user’s choice and apply the operation on numbers and we will verify that user enter the correct choice or not using default case.
import java.util.Scanner;
//declaring class
class calculator{
//declaring
the main function which returns nothing.
//main
function is executed first when program executes
public static void main(String args[]){
int result,a,b,choice;
Scanner x = new Scanner(System.in);
System.out.println("Pease
Enter the First Number : ");
a = x.nextInt();
System.out.println("Please
Enter the Second Number :");
b = x.nextInt();
System.out.println("Please
Enter number before operator : ");
System.out.println("1
. Addition");
System.out.println("2.
Subtraction");
System.out.println("3.
Multiplication");
System.out.println("4.
Division");
choice = x.nextInt();
System.out.println("Please
Enter Your Choice : ");
switch(choice){
case 1:
result =
a + b;
break;
case 2:
result =
a -b;
break;
case 3:
result =
a *b;
break;
case 4:
result =
a /b;
break;
default:
System.out.println("Please
Enter number from 1 to 4.");
}
}
}
|
Step 4 – Finally after validation of correct choice we go through the switch case process and finally we will store the output to result variable and then display it to user and finally our program looks like this.
Complete Code:
import java.util.Scanner;
//declaring class
class calculator{
//declaring the main function which returns nothing.
//main function is executed first when program executes
public static void main(String args[]){
int result,a,b,choice;
Scanner x = new Scanner(System.in);
System.out.println("Pease Enter the First Number : ");
a = x.nextInt();
System.out.println("Please Enter the Second Number :");
b = x.nextInt();
System.out.println("Please Enter number before operator : ");
System.out.println("1 . Addition");
System.out.println("2. Subtraction");
System.out.println("3. Multiplication");
System.out.println("4. Division");
choice = x.nextInt();
switch(choice){
case 1:
result = a + b;
System.out.println("Result = "+result);
break;
case 2:
result = a -b;
System.out.println("Result = "+result);
break;
case 3:
result = a *b;
System.out.println("Result = "+result);
break;
case 4:
result = a /b;
System.out.println("Result = "+result);
break;
default:
System.out.println("Please Enter number from 1 to 4.");
}
}
}
See the output image below:
*Note:
error.
- Don’t forget to declare the variable before using.
- Make sure the number of cases you write is applicable or not?
- Try to keep a class name that suits your program to easily identify its class file after compiling.
We hope you liked the blog..Any comments or doubts are always welcome.


2 Comments
What if the number, on which we are performing some action exceeds to 3 or 4....?
ReplyDeleteCan you please clarify your question, by 3 and 4 do you mean the number of digits?
DeleteIf you have any doubts, please let me know.