Monday 24 September 2012

Addition Example With Set Get variables

 // This example use to understand how to set & get variables in java

Add.java

package Testing;
import Testing.AddSetGet.*;
import java.util.*;
import java.io.*;
class Add2 extends AddSetGet  {

    public Add2() {
        // TODO Auto-generated constructor stub
    }
    public void addition(){
    try{  
         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
         System.out.print("Enter the integer A :");
         setA(Integer.parseInt(br.readLine()));
          System.out.print("Enter the integer B :");
         setB(Integer.parseInt(br.readLine()));
         c=a+b;
         System.out.print("Addition is :"+c);        
      }
  
    catch (Exception e){
        System.out.print("Exception occured "+e);
    }
    }

}
public class Add{
  
    public static void main(String a[]){
        Add2 obj1 = new Add2();
        obj1.addition();
    }
}

//----------------------------------------------------------------------

//Calladd.java

package Testing;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import Testing.AddSetGet.*;

class Calladd {

  public Calladd(AddSetGet obj2){
      this.obj2= obj2;
  }
  AddSetGet obj2;  
  public void addition () throws IOException{
    
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      System.out.print("Enter The number A = ");
      obj2.setA(Integer.parseInt(br.readLine()));     
      System.out.print("Enter The number B = ");
      obj2.setB(Integer.parseInt(br.readLine()));
      //int c = (obj2.getA())+(obj2.getB());
      int a1= (obj2.getA());
      int b1= (obj2.getB());
      int c = a1+b1;
      System.out.println("Addition is :"+c);
   
  }

}

public class AddCall
{
public static void main(String a[]) throws IOException{
AddSetGet obj = new AddSetGet();
Calladd obj1 = new Calladd(obj);
obj1.addition();
 }
}

//-----------------------------------------------------------------------------------------
 // AddSetGet.java

package Testing;

public class AddSetGet {
  
   protected  int a, b, c=0;

    public int getA(){
        return a;
    }
    public void setA(int a){
        this.a=a;
    }
    public int getB(){
        return b;
    }
    public void  setB(int b){
        this.b=b;
    }
}