Java Tutorial

 
 
 
 
// IDL

module GameModule
{
    interface Game
    {
        double guess(in double operand1);
    };

};


// GameImpl
// Contains the implementation of the methods defined in the IDL file.

import GameModule.GamePOA;

class GameImpl extends GamePOA
{
    GameImpl()
    {
        super();
        System.out.println(\"game Object Created\");
    }

    public double guess(double num)
    {
        if(num < 50)
            return(-1);
        else if(num > 50)
            return(1);
        else
            return(0);
    }
}


// OUTPUT

1
1
You enter number is Lower
Enter no=
32
32
You enter number is Lower
Enter no=
34
34
You enter number is Lower
Enter no=
65
65
You enter number is Higher
Enter no=
99
99
You enter number is Higher
Enter no=
50
50
You enter Exact num in trial =5


// Server


import GameModule.Game;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import org.omg.PortableServer.*;

class GameServer
{

    public static void main(String[] args)
    {
        try
        {
            // initialize the ORB
            org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args,null);

            // initialize the BOA/POA
            POA rootPOA = POAHelper.narrow(orb.resolve_initial_references(\"RootPOA\"));
            rootPOA.the_POAManager().activate();

            // creating the calculator object
            GameImpl game = new GameImpl();

            // get the object reference from the servant class
            org.omg.CORBA.Object ref = rootPOA.servant_to_reference(game);

            System.out.println(\"Step1\");
            Game h_ref = GameModule.GameHelper.narrow(ref);
            System.out.println(\"Step2\");

            org.omg.CORBA.Object objRef = orb.resolve_initial_references(\"NameService\");
            System.out.println(\"Step3\");
            NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
            System.out.println(\"Step4\");

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

            System.out.println(\"Game Server reading and waiting....\");
            orb.run();


        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}


// Client

import GameModule.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import java.io.DataInputStream;
import java.lang.Integer;

class GameClient
{

    public static void main(String args[])
    {
        Game GameImpl=null;
        int count=0;
        int data;
        try
        {
            // initialize the ORB
            org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args,null);

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

            String name = \"Game\";
            GameImpl = GameHelper.narrow(ncRef.resolve_str(name));

            while(true)
            {
            int num;
            System.out.println(\"Enter no=\");
            DataInputStream in=new DataInputStream(System.in);
            num=Integer.parseInt(in.readLine());
            System.out.println(num);

             data=(int) GameImpl.guess(num);
                if(data == -1)
                {
                    System.out.println(\"You enter number is Lower\");
                    count=count+1;
                }
                else if(data == 1)
                {
                    System.out.println(\"You enter number is Higher\");
                    count=count+1;
                }
                else if(data == 0)
                {
                    System.out.println(\"You enter Exact num in trial =\"+count);
                    break;
                }
            }


        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }


}

    Related Post:
  1. Program that demonstrates type casting

  2. Program for Thread Priority

  3. Program in CORBA which takes a long sentence from client (at least 7 words), passes to the server, server separates out each word and sends back

  4. Program to show the comparing of Strings for equality

  5. Program to find a factorial of a given number

  6. Given a list of marks ranging form 0 to 100, write a program to compute and print the number of students marks

  7. Program to show an example of Concatination of Strings

  8. An applet program to perform merge sort

  9. Program that accepts a shopping list of items from the command line and stores them in a vector. Also provide facility to perform operation

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

  11. CORBA based application for addition of two mathematical expressions of the form (ax + by) and displaying the result on client side

  12. Program to show the use of operator (&gt;&gt;) Shift Right, propagating the sign bit from the left

  13. RMI application for counting factorial of a given number.

  14. Program to declare, initialize and print a String object

  15. An applet of displaying simple moving banner

  16. Program that enabled applet calculator

  17. Develop a CORBA application that takes a string from client and returns whether it is palindrome

  18. Program of reading and writing primitive data

  19. Program to show an example of Multiple Levels of Inheritance

  20. Program to show an example of getting at characters in a String

 
 
Didn't find what you were looking for? Find more on Corba program of game