Java Tutorial

 
 
 
 
----------------------------------------------------------------------------------
                    IDL
----------------------------------------------------------------------------------
module CircleApp
{
    interface Circle
    {
        float area(in float val);
        oneway void shutdown();
    };
};

-------------------------------------------------------------------------------------------------
                    Server
-------------------------------------------------------------------------------------------------

import CircleApp.*;
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 CircleImpl extends CirclePOA
{
    private ORB orb;

    public void setORB(ORB orb_val)
    {
        orb = orb_val; 
    }

    public float area(float val)
    {
        float res = (float)( Math.PI * val * val );
        return res;
    }
    public void shutdown()
    {
        orb.shutdown(false);
    }
}


public class CircleServer
{

    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();

            CircleImpl circleImpl = new CircleImpl();
            circleImpl.setORB(orb); 

            org.omg.CORBA.Object ref = rootpoa.servant_to_reference(circleImpl);
            Circle href = CircleHelper.narrow(ref);

            org.omg.CORBA.Object objRef =orb.resolve_initial_references(\"NameService\");
            NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);

            String name = \"Circle\";
            NameComponent path[] = ncRef.to_name( name );
            ncRef.rebind(path, href);

            System.out.println(\"CircleServer ready and waiting ...\");

            orb.run();
        } 

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

        System.out.println(\"CircleServer Exiting ...\");
    }
}

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

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

public class CircleClient
{
    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 static void main(String args[])
    {
        Circle circleImpl=null;
        CircleClient cc = new CircleClient();

        try
        {
            ORB orb = ORB.init(args, null);
            org.omg.CORBA.Object objRef = orb.resolve_initial_references(\"NameService\");
            NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
            String name = \"Circle\";
            circleImpl = CircleHelper.narrow(ncRef.resolve_str(name));

            float val = 0, res = 0;
            val = cc.getValue();
            res=circleImpl.area(val);
            System.out.println(\"Result : \" + res);

            circleImpl.shutdown();

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

}

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

Enter the value : 2
Result : 12.566371

    Related Post:
  1. Program to read command line arguments

  2. Program to show an example of Polymorphism

  3. An applet program that concatenates two string entered in TextField

  4. GUI application in java, which enter the details of a student and on the submit display the details of the student

  5. Program to show the use of operator (>>) Shift Right, propagating the sign bit from the left

  6. Program to show an example of searching Strings for characters

  7. Add,delete,Update in LISTS

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

  9. Program of storing and retrieving integers using data streams on a single file

  10. Program of inventory management and demonstrating interactive input and output

  11. Program of telnet server - client

  12. Program to show an example of Overriding a Base Class Method

  13. Program to print a string on Standard Output Device

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

  15. Program, which creates a three thread. One thread display the numbers from 1 to 5, second thread display the square root of that number etc

  16. Program to show the use of the Continue Statement

  17. Program to show the use of Labeled Continue Statement

  18. Program to show the use of Increment and Decrement Operators

  19. Pass the marks of 10 students from the client side design a servlet, which takes these marks and returns the number of students who obtained first cls

  20. Program to show an example of overloading constructors in a class to initialize its data members