Logo 
Search:

Java Articles

Submit Article
Home » Articles » Java » CorbaRSS Feeds

Develop a simple calculator with addition, subtraction, multiplication and division capabilities, data collection from client side, executed on server

Posted By: Lexie Hughes     Category: Java     Views: 27719

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 Develop a simple calculator with addition, subtraction, multiplication and division capabilities, data collection from client side, executed on server in Java


----------------------------------------------------------------------------------
                    IDL
----------------------------------------------------------------------------------
module CalcApp
{
    interface Calc
    {
        float add(infloat value1,infloat value2);
        float sub(infloat value1,infloat value2);
        float multi(infloat value1,infloat value2);
        float div(infloat value1,infloat value2);
        oneway void shutdown();
    };
    
};

----------------------------------------------------------------------------------
                    Server
----------------------------------------------------------------------------------
import CalcApp.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import org.omg.PortableServer.*;
import org.omg.PortableServer.POA;

import java.util.Properties;

class CalcImpl extends CalcPOA 
{
    private ORB orb;

    publicvoid setORB(ORB orb_val) 
    {
        orb = orb_val; 
    }
    publicfloat add(float val1, float val2)
    {
        float res = val1 + val2;
        return res;
    }
    publicfloat sub(float val1, float val2)
    {
        float res = val1 - val2;
        return res;
    }
    publicfloat multi(float val1, float val2)
    {
        float res = val1 * val2;
        return res;
    }
    publicfloat div(float val1, float val2)
    {
        float res = val1 / val2;
        return res;
    }
    publicvoid shutdown() 
    {
        orb.shutdown(false);
    }
}


publicclass CalcServer 
{

    publicstaticvoid main(String args[]) 
    {
        try
        {

              ORB orb = ORB.init(args, null);


              POA rootpoa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
              rootpoa.the_POAManager().activate();
              CalcImpl calcImpl = new CalcImpl();
              calcImpl.setORB(orb); 
              org.omg.CORBA.Object ref = rootpoa.servant_to_reference(calcImpl);
              Calc href = CalcHelper.narrow(ref);
              org.omg.CORBA.Object objRef =
              orb.resolve_initial_references("NameService");
              NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
              String name = "Calc";
              NameComponent path[] = ncRef.to_name( name );
              ncRef.rebind(path, href);

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

              orb.run();
        } 
    
        catch (Exception e) 
        {
            System.err.println("ERROR: " + e);
            e.printStackTrace(System.out);
        }

        System.out.println("CalculatorServer Exiting ...");
      }
}

-------------------------------------------------------------------------------------------------
                    Client
-------------------------------------------------------------------------------------------------

import CalcApp.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import java.io.*;

publicclass CalcClient
{
    publicvoid showMenu()
    {
        System.out.println("----------------------------------");
        System.out.println("0.Exit");
        System.out.println("1.Addition");
        System.out.println("2.Subtraction");
        System.out.println("3.Multiplicatin");
        System.out.println("4.Division");
        System.out.println("----------------------------------");
        System.out.print("Enter your choice : ");
        
    }
    publicfloat getValue() throws IOException
    {
        float val=0;
        try
        {
            System.out.print("Enter the value : ");
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            String s = br.readLine();
            val = Float.parseFloat(s);
        }
        catch (IOException e)
        {
            System.out.println(e);
        }
        return val;
    }
    publicint getChoice() throws IOException
    {
        int val=0;
        try
        {

            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            val = Integer.parseInt(br.readLine());
            System.out.println("----------------------------------");
        }
        catch (IOException e)
        {
            System.out.println(e);
        }
        return val;
    }

  

    publicstaticvoid main(String args[])
    {

        Calc calcImpl = null;
        CalcClient cc = new CalcClient();

        try
        {

            ORB orb = ORB.init(args, null);
                org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
            NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
                 String name = "Calc";
            calcImpl = CalcHelper.narrow(ncRef.resolve_str(name));
            float val1 = 0;
            float val2 = 0;
            float res = 0;
            int ch = 1;

            while (ch != 0 )
            {
                cc.showMenu();
                ch = cc.getChoice();

                val1 = cc.getValue();
                val2 = cc.getValue();

                switch (ch)
                {
                    case 1:
                        res = calcImpl.add(val1, val2);
                        break;
                    case 2:
                        res = calcImpl.sub(val1, val2);
                        break;
                    case 3:
                        res = calcImpl.multi(val1, val2);
                        break;
                    case 4:
                        res = calcImpl.div(val1, val2);
                        break;
                    case 0:
                        exit(0);
                }
                System.out.println("----------------------------------");
                System.out.println("Result : "+res);
                System.out.println("----------------------------------");
            }

                calcImpl.shutdown();
        }
        catch (Exception e) 
        {
            System.out.println("ERROR : " + e) ;
            e.printStackTrace(System.out);
        }
    }

}

-------------------------------------------------------------------------------------------------
                    Output
-------------------------------------------------------------------------------------------------

----------------------------------
0.Exit
1.Addition
2.Subtraction
3.Multiplicatin
4.Division
----------------------------------
Enter your choice : 2
----------------------------------
Enter the value : 20.5
Enter the value : 10.2
----------------------------------
Result : 10.3
----------------------------------
----------------------------------
0.Exit
1.Addition
2.Subtraction
3.Multiplicatin
4.Division
----------------------------------
Enter your choice : 0
  
Share: 



Lexie Hughes
Lexie Hughes author of Develop a simple calculator with addition, subtraction, multiplication and division capabilities, data collection from client side, executed on server is from London, United Kingdom.
 
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].

 
Anurag Dani from United States Comment on: May 13
Hello,

I am very new to CORBA and RMI. I am trying to learn and execute this program. I want to run this program on local host and upon execution I am getting this error "COMM_FAILURE) Connection failure: socketType: IIOP_CLEAR_TEXT; hostname: 127.0.1.1; port: 900"
Can you please tell me how can I execute this on local machine and how can solve this error?

View All Comments