Create a Simple Java Calculater !

In this article , I will be teaching you the basics of Java Programming. I will be dealing with a program to find the sum of two numbers in Java. I am going to explain every statement in the program step by step. This Program is completely for Beginners.You can learn the basics for Java Programming through this tutorial on How to create a Simple Calculator in Java.

If you dont have a BlueJ (a complier for Java) ,You can download from Official BlueJ Site
Download the BlueJ  and comeback to learn the Program.

Program Code :

  1.          import java.util.*;  //Importing the utility Package containing Scanner Class
  2.          class Sum
  3.          {
  4.             public static void main()
  5.             {
  6.                  //Declaring a variable in Scanner Class to input data
  7.                  Scanner in=new Scanner(;
  8.                  System.out.print(“Input the first number : “);
  9.                 int a=0;//variable is intialised with zero to not let it store any absurd value
  10.                 //Syntax of input statement in Scanner Class method for int-type
  11.                 a=in.nextInt();
  12.                 System.out.print(“Input the second number : “);
  13.                 int b=0;
  14.                 b=in.nextInt();
  15.                 int sum=a+b;//adds the values of both numbers
  16.                System.out.println(“The sum of two numbers : “+sum);
  17.             }
  18.         }

This is Program written to find out the sum of two numbers.As this program is completly for absolute beginners,How the  code works is :

 Line 1 : As we are writing the program in Scanner Class method .We need to import the Package containing the Scanner Scanner i.e, Utility Package (java.util)
You can also write import java.util.Scanner;
It  imports only the Scanner class Where as  import java.util.*; imports the whole package into the class.

Line 2 : It includes the definition the class. You can have any name to it
Ex: class addition, class calculator_sum etc any name in a meaningful way for your understanding

LIne 3 : Opening of class with  {

Line 4 :  It contains the declaration of function .You can have any name to function in the place of main.
The keyword public denotes that the function access has no restriction.
The keyword static instructs the compiler to run this function before any other function in class.
The keyword void indicates that this function will not return any value

LIne 5 : Opening of function main() with  {

Line 6 : This is SIngle Line Comment ,Comments are made in program to make the user understand how the code is written.Comments are not executed during function call.

Line 7:  A variable must be declared in Scanner Class which is used to intake the values from the user.
It is declared in the syntax Scanner < variable name > = new Scanner (;

LIne 8 & LIne 12 :  They include the output statement which specify the type/needed input values during function call 

LIne 9 & Line 13 :    They include the declaration and  initialisation of the variables a and b needed to input the values from the user.
Here the variables are initialised with zero because when they are not initialised they may store some absurd  value  and cause for a run time error during the execution of the program.

Line 11 &  Line 14 :  They include the input statements of the variavles a and b . In Scanner Class Method, the  Syntax of the Input Statement of int data type is < variable name > = < Scanner Class variable >.nextInt();   which is in the present program is written as a=in.nextInt();               

 Similarly the input staements of float and double data  types are respectively float f=in.nextFloat();  &  double d=in.nextDouble();

Line 15 : It includes the declaration and initialisation of the  ‘sum’  varibale which is used to add the values of two input variables.

Line 16 :  It includes the Print Statement of the variable ‘sum’

LIne 17 : Closing of the function main() with  }

Line 18 : Closing of the class with  }

Sample Execution :

Note : If you want to find the sum of two real numbers i.e, numbers with decimal values.You must use Float and Double data type for the variables a and b.

In the same way, You can  write the program to Find  Difference,Product,Division of Two numbers.
Go! Give a trial .Try it yourself .Keep a Look to learn more!

Check this Video Tutorial for better understanding.Subscribe our YouTube Channel for more updates.

Buncy Shaddai

Buncy Shaddai


Share This

Share This

Share this post with your friends!

Subscribe To Our Newsletter and Never Miss an Update!

Subscribe To Our Newsletter and Never Miss an Update!

Leave us your Email ID and we will notify you whenever we release new Videos, Articles or Apps.

You have Successfully Subscribed!