[an error occurred while processing this directive]

Addition of two Numbers in Class

                         

In this program we will learn how to calculate the sum of any two number. Java is a object oriented  programming language in which uses of class consists of a collection of type of encapsulation instance variables and type of methods. We are going to use in this program of class method. Class is a cohesive package that consist of a particular values at compile time. The class  is describe the rules by which object are referred to as instance value. 

Description this program

Here in this program  we are going to use addition of  two number. First of all, we have to define class named "SumExample". We are going to define two integer type values in the class method. After that we are going to create buffer reader object for read the text charactor- input stream and buffer is also provide for the efficient read of charactor values. As in this program we are going to insert certain instruction by creating buffer reader class. it is necessary to use 'try' and 'catch'. Now we are going to create parseInt method for read text line. After that we have to create object and return the integer type values. So output will be display by using "sum= sum1.x+sum1.y".

Here is the code of this program

import java.io.*;
public class SumExample{
    public int x;
    public int y;
    public static void main(String args[]){
      try {
    SumExample sum1= new SumExample();
    int sum;
    BufferedReader object= new BufferedReader (new InputStreamReader(System.in));
    System.out.println("Enter number:");
    System.out.print("Enter number x:=""");
    sum1.x=Integer.parseInt(object.readLine());
    System.out.print("Enter number y:=""");
    sum1.y=Integer.parseInt(object.readLine());
    sum= sum1.x+sum1.y;
    System.out.println("Sum is:"+ sum);
      }
    catch(Exception e){}
    }
  }

 

Download this Example

                         

[an error occurred while processing this directive]