Logo 
Search:

Java Articles

Submit Article
Home » Articles » Java » CorbaRSS Feeds

Corba program to develop a simple calculator with addition, subtraction, multiplication and division capabilities, data collection from client side

Posted By: Milind Mishra     Category: Java     Views: 9695

Develop a simple calculator with addition, subtraction, multiplication and division capabilities, data collection from client side, executed on server side along with appropriate validation and displaying result on client side using CORBA Programming.

Code for Corba program to develop a simple calculator with addition, subtraction, multiplication and division capabilities, data collection from client side in Java

// calc.idl

module calc_val
{
  interface calc
  {
    double addfn(indouble a,indouble b);
    double subfn(indouble a,indouble b);
    double mulfn(indouble a,indouble b);
    double divfn(indouble a,indouble b);
  };
};


// calc_client.java    
    
import calc_val.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import java.io.*;
publicclass calc_client
{
    static calc calcimpl;
    publicstaticvoid main(String args[])
    {
      try
      {
        double result=0.0,num1=0.0,num2=0.0;

        ORB orb=ORB.init(args,null);
        org.omg.CORBA.Object objref=orb.resolve_initial_references("NameService");

        NamingContextExt ncref=NamingContextExtHelper.narrow(objref);

        String pathname="calc";
        calcimpl=calcHelper.narrow(ncref.resolve_str(pathname));


       int ch=1;

       while(ch!=0)
       {
        System.out.println("1. Addition");
        System.out.println("2. Subtraction");
        System.out.println("3. Multiplication");
        System.out.println("4. Division");
        System.out.println("0. Exit");

        BufferedReader in1=new BufferedReader(new InputStreamReader(System.in));

        System.out.println("enter your choice:");
        ch=Integer.parseInt(in1.readLine());
        
        if(ch==0)
          break;

        BufferedReader in=new BufferedReader(new InputStreamReader(System.in));

        System.out.println("enter number1:");
        num1=Double.parseDouble(in.readLine());

        System.out.println("enter number2:");
        num2=Double.parseDouble(in.readLine());

        switch(ch)
        {
          case 1:
              result=calcimpl.addfn(num1,num2);
              break;

          case 2:
              result=calcimpl.subfn(num1,num2);
              break;

          case 3:
              result=calcimpl.mulfn(num1,num2);
              break;

          case 4:
              result=calcimpl.divfn(num1,num2);
              break;

        }
              System.out.println("result is:"+result);

        }

      }
      catch(Exception e)
      {
         System.out.println(e);
      }
    }

}

            -----------------                
            calc_server.java
            -----------------

import calc_val.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import org.omg.PortableServer.*;
import org.omg.PortableServer.POA;

class serverimpl extends calcPOA
{
   private ORB orb;
   publicvoid setorb(ORB orb_val)
   {
      orb=orb_val;
   }

   publicdouble addfn(double n1,double n2)
   {
     return(n1+n2);
   }
   publicdouble subfn(double n1,double n2)
   {
     return(n1-n2);
   }
   publicdouble mulfn(double n1,double n2)
   {
     return(n1*n2);
   }
   publicdouble divfn(double n1,double n2)
   {
     return(n1/n2);
   }


}
publicclass calc_server
{
   publicstaticvoid main(String args[])
   {
     try
     {
         ORB orb=ORB.init(args,null);

         org.omg.CORBA.Object objref1=orb.resolve_initial_references("RootPOA");
         POA rootpoa=POAHelper.narrow(objref1);
         rootpoa.the_POAManager().activate();


         serverimpl serverobj=new serverimpl();
         serverobj.setorb(orb);
         org.omg.CORBA.Object objref2=rootpoa.servant_to_reference(serverobj);
         calc href=calcHelper.narrow(objref2);


         org.omg.CORBA.Object objref3= orb.resolve_initial_references("NameService");
         NamingContextExt ncref=NamingContextExtHelper.narrow(objref3);
         String pathname="calc";
         NameComponent path[]=ncref.to_name(pathname);
         ncref.rebind(path,href);


         System.out.println("server ready and waiting...");

         orb.run();


     }
     catch(Exception e)
     {
       System.out.println(e);
     }
   }


}

************* OUTPUT *************
E:\DIPI1\corba prgs\calculator>java calc_client -ORBInitialPort 1050 -ORBInitial
Host a

1. Addition
2. Subtraction
3. Multiplication
4. Division
0. Exit
enter your choice:
1
enter number1:
12
enter number2:
45
result is:57.0

1. Addition
2. Subtraction
3. Multiplication
4. Division
0. Exit
enter your choice:
2
enter number1:
45
enter number2:
90
result is:-45.0

1. Addition
2. Subtraction
3. Multiplication
4. Division
0. Exit
enter your choice:
3
enter number1:
12
enter number2:
5
result is:60.0

1. Addition
2. Subtraction
3. Multiplication
4. Division
0. Exit
enter your choice:
4
enter number1:
35
enter number2:
2
result is:17.5
1. Addition
2. Subtraction
3. Multiplication
4. Division
0. Exit
enter your choice:
0
  
Share: 



Milind Mishra
Milind Mishra author of Corba program to develop a simple calculator with addition, subtraction, multiplication and division capabilities, data collection from client side is from India.
 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!