# Addition of two Number in Class

In this program we will learn how to calculate 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.

Ads

Tutorials
Ads

# 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`

Advertisements

Online Training Classes

Share on Google+

# Addition of two Number in Class

Posted on: June 25, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Related Tutorials

Discuss: Addition of two Number in Class

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):

Comments:6
siddhartha deka
August 14, 2011
bca

thanx
husensab
November 14, 2011
java

how declard variables
naga
December 23, 2011
hai

plz send me addition program without try catch block
ASWATHY
January 15, 2012
java

very useful answer.....
Samuel
June 27, 2012
Addinton of Two Numbers in Java

Are there other ways of writing this? I would welcome that please o.k!
Avinash
September 4, 2012
C++

Ple tell interview based question

Ads

Ads