Java Tutorial

 
 
 
 
----------------------------------------------------------------------------------
                    IDL
----------------------------------------------------------------------------------
module CalcApp
{
    interface Calc
    {
        float add(in float value1,in float value2);
        float sub(in float value1,in float value2);
        float multi(in float value1,in float value2);
        float div(in float value1,in float 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;

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


public class CalcServer 
{

    public static void 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.*;

public class CalcClient
{
    public void 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 : \");

    }
    public float 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;
    }
    public int 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;
    }



    public static void 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

    Related Post:
  1. Program to read the price of an item in decimal form (like 75.95) and print the output in paise (like 7595 paise).

  2. Define an array of ten String elements each containing an arbitrary string of the form “month/day/year”

  3. Program to display reverse $ tree

  4. Develop a game application in CORBA for which the player will guess a number between 1 to 100, which will be compared to the random number generated b

  5. Program that will read real number from the keyboard and print the following output in one line: small Integer not less The Given Number Largest Int

  6. Program to show an example of Driving and using a Class

  7. Corba program of UDP client server application which sends the news to the client. Server takes the news from the NewsDataFile located at the server

  8. Program of an applet to illustrate BarChart

  9. Program to evaluate following investment equation V=P(1+R)^N

  10. CORBA program for displaying the date and time of the server machine, client machine and the difference between these two date and time

  11. An applet program showing Clicked Event on canvas

  12. Program of concatenating and Buffering files

  13. An applet program that displays random dot

  14. Program to show the use of Bitwise Operator (&) AND

  15. Program to show an example of using a Non-Static Nested Class

  16. Program to sort the contents of an array using Bubble Sort

  17. Program which creates an Array of character. Make one function with one argument as a character and that function throw a user defined exception

  18. Program to show some properties of StringBuffer object

  19. Program to show an example of Calling a Base Class Method from a Derived Class

  20. Program that will count the number of lines in each file that is specified on the command line