Advance Java Application

/* Write a program in Java that takes two integer input from user then add them both and displays the output.*/

import java.util.Scanner; //program uses class scanner
public class Calculator{
          public static void main(String[ ] args)
          {
               int ,a,b,total;
               //create an Scanner object to read input from user.
               Scanner input =new Scanner(System.in);
             
               System.out.println("Enter first number:");
               a=input.nextInt(); //read first number

               System.out.println("Enter second number:");
               b=input.nextInt(); //read second number
           
               total=a+b;   //calculates the total

               // prints the total result
              System.out.println("Total sum is: "+total);

           }

}

Output:
Enter first number: 5
Enter second number: 5
Total sum is: 10

Above, the first statement which is written between /*........*/ is the given question. it won't execute while we execute the source code because it is written between /*.....*/. It is way of writing comment in program. That above signs are use for multiple line comment. For single line comment we use // sign. You can see in above source code // sign is used to comment single line only.

A Scanner  class is pre-defined class grouped under java .util package. It contains the method to read data input either from the keyboard of files. To import the class, import  declaration is used to import the class from the package.

All the needed packages must be imported before the declaration of class and the class name must starts from Capital letter because java programming language is case sensitive and the class name and file name must be same (Calculator.java).

   public static void main(String[ ] args)
          {

          }

This statement refers the beginning of the main method. main method is the body of the class and codes in the main method are intended another level of readability.

   int ,a,b,total;

Above we have declared three variables in the program. And the variable type is integer. Variable is location in computer's memory where a value can be stored for later use in program. To make a variable we should declare the variable type and then name, if there are many  variables then (,) is used to separate the variables. At last (;) must be used because the java statement must be ended with semi colon (;). And the declaration of variable in above program is the example of primitive data type variable. Primitive data types are special data types built into the language. They are not objects created from the class.

  Scanner input =new Scanner(System.in);

This is the process of declaration of an object input of type Scanner.in. It is class which we have imported above the class name. Object declaration requires initialization with new keyword followed by the constructor of the object. A new keyword is necessary in all object declaration because it will instruct the computer to allocate the required memory space for the object.

  System.out.println("Enter first number:");

System.out.println(""); this code is used to display the message. What ever we write between the inverted comma(" "), it simply displays the written statement. Above it is used to display("Enter first number:"). We know that all the class starts from the Capital letter. System is a class and it is part of package java.lang. We should not have to import this package because it is loaded by default.

  a=input.nextInt();

input object or type of scanner, it is used to read an input from keyboard, using nextInt() method from Scanner object. After pressing the input data, it is stored in variable a.

  total=a+b; 

This line is used to compute a addition of  a and b and stores the result in variable total.

Comments

Popular posts from this blog

Logical operator in JAVA

Class and Object in Java

Loop Structure