program to Add of two no. using method seter geter in java
package add_returnvalue;
import java.util.Scanner;
public class Add_returnvalue {
int a=0,b=0;
public
void setA(int c)
{
a=c;
}
public
int getA(){
return
a;
}
public
void setB(int d)
{
b=d;
}
public
int getB(){
return
b;
}
public
int sum(){
int
s=0;
s=a+b;
return
s;
}
public static void main(String[] args) {
// TODO code application logic here
Scanner obj= new Scanner(System.in);
Add_returnvalue
d1= new Add_returnvalue();
System.out.println("
this program for Add of 2 no. plz enter a and b value:");
int
a= obj.nextInt();
int
b= obj.nextInt();
d1.setA(a);
d1.setB(b);
System.out.println("Value
of A is :"+d1.getA());
System.out.println("Value
of B is :"+d1.getB());
System.out.println("Add
of 2 values is:"+d1.sum());
}
}
If you
declare private member in a class and want to call different class then you can
you setter getter concept.
Below the
program mention using setter getter method.
Create
multi.java class
package add_returnvalue;
public class multi {
private int a;
private int b;
public void setA(int a){
this.a= a;
}
public int getA(){
return a;
}
public void setB(int b){
this.b= b;
}
public int getB(){
return b;
}
}
create main class multi_mainclass.java
in this class calling the private member from the other
class
package add_returnvalue;
public class multi_mainclass {
public static void main(String[] args) {
multi ml = new multi();
ml.setA(20);
ml.setB(10);
System.out.println(ml.getA()*ml.getB());
}
}
Output:
0 comments: