add numbers using class and object concept program in java:
program :
with explenation:
import java.util.*;public class addno{ //we firstly create one class public static int a,b,sum; //then declare variables public void add() //then create one method { sum=a+b; //here we add a+b } public static void main(String args[]){ //here is main method Scanner sc=new Scanner(System.in); //here is scanner class System.out.println("enter two numbers"); a=sc.nextInt(); //taking input 1 as no b=sc.nextInt(); //taking input 2 addno num=new addno(); //here we created the object of class num.add(); //then we call the method System.out.println("the sum of numbers is= "+sum);//print the sum }}
import java.util.*;
public class addno{ //we firstly create one class
public static int a,b,sum; //then declare variables
public void add() //then create one method
{
sum=a+b; //here we add a+b
}
public static void main(String args[]){ //here is main method
Scanner sc=new Scanner(System.in); //here is scanner class
System.out.println("enter two numbers");
a=sc.nextInt(); //taking input 1 as no
b=sc.nextInt(); //taking input 2
addno num=new addno(); //here we created the object of class
num.add(); //then we call the method
System.out.println("the sum of numbers is= "+sum);//print the sum
}
}
Tags:
java