Java Tutorial

 
 
 
 
// Count.idl

module Counter
{
  interface Count
  { 
    attribute long sum;
    long increment();
    double check(in double a);
  };
}; 


// CountClient.java

import Counter.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import java.io.*;
class CountClient { 

  static Count countImpl;

  public static void main(String args[])
  { try
    {

       // Initialize the ORB

        org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args, null);

        // get the root naming context
        org.omg.CORBA.Object objRef = 
        orb.resolve_initial_references(\"NameService\");
        // Use NamingContextExt instead of NamingContext. This is 
        // part of the Interoperable naming Service.  
        NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);

        // resolve the Object Reference in Naming
        String name = \"Count\";
        countImpl = CountHelper.narrow(ncRef.resolve_str(name));

      // Set sum to initial value of 0
      countImpl.sum((int)0);

      int choice=0;
      double value1,num;
      BufferedReader b1=new BufferedReader(new InputStreamReader(System.in));


      while(true)
     {
      System.out.println(\"Do you want to guess a number(1:Yes / 2:No):\");
      choice=Integer.parseInt(b1.readLine());
      if(choice==2)
         break;
      System.out.println(\"Guess a number and enter that number!!\");
      num=Integer.parseInt(b1.readLine());    

      countImpl.increment();


      if(countImpl.sum()==5)
         {
           System.out.println(\"SORRY YOUR 5 TRIALS ARE OVER\");
           System.out.println(\"Trials = \" + countImpl.sum());    
           break;        
         }

      value1=countImpl.check(num);

      if(value1==0)
         {
            System.out.println(\"CONGRATULATIONS!!! GUESSED THE RIGHT NUMBER!!!\");
            System.out.println(\"Trials = \" + countImpl.sum());
        break;    
         }

     else if(value1<0)
         {
            System.out.println(\"NUMBER is less than the actual no!!!\");
            System.out.println(\"Trials = \" + countImpl.sum());
      }

     else if(value1>0)
         {
            System.out.println(\"NUMBER is greater than the actual no!!!\");
            System.out.println(\"Trials = \" + countImpl.sum());
      }

    } 
   }
    catch(Exception e)
    { System.err.println(\"System Exception\");
      System.err.println(e);
    }
  }
}


// CountServer.java: The Count Server main program

import Counter.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import org.omg.PortableServer.*;
import java.util.*;

class CountImpl extends Counter.CountPOA
{

    private int sum;
    int tempnum;
  // Constructors
  CountImpl(String name)
  {
    super();
    System.out.println(\"Count Object Created\");
    sum = 0;

    Random a=new Random();
    double a1;
    while(true)
     {
       a1=a.nextGaussian()*100;
       if(a1<100 && a1>0)
         break;
     }
    tempnum=(int)a1;
  }

  // get sum
  public  int sum()
  { return sum;
  }

  // set sum
  public  void sum(int val)
  { sum = val;
  }

  // increment method
  public int increment()
  { sum++;
    return sum;
  }

  public double check(double num)
  {
     return(num-tempnum);
 }

}

class CountServer
{
   static public void main(String[] args)
  {
    try
    { 
      // Initialize the ORB

       org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args, null);

      // Initialize the BOA

      POA rootpoa = POAHelper.narrow(orb.resolve_initial_references(\"RootPOA\"));
      rootpoa.the_POAManager().activate();

      // Create the Count object

      CountImpl count = new CountImpl(\"My Count\");

      // get object reference from the servant

      org.omg.CORBA.Object ref = rootpoa.servant_to_reference(count);
      Count href = Counter.CountHelper.narrow(ref);

      // get the root naming context
      // NameService invokes the name service

      org.omg.CORBA.Object objRef =
      orb.resolve_initial_references(\"NameService\");

      // Use NamingContextExt which is part of the Interoperable
      // Naming Service (INS) specification.

      NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);

      // bind the Object Reference in Naming

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

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

      // wait for invocations from clients
      orb.run();

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


OUTPUT 1 :

E:\\DIPI1\\corba prgs\\numgame>java CountClient -ORBInitialPort 1050 -ORBInitialHost a

Do you want to guess a number(1:Yes / 2:No):
1
Guess a number and enter that number!!
50
NUMBER is less than the actual no!!!
Trials = 1

Do you want to guess a number(1:Yes / 2:No):
1
Guess a number and enter that number!!
75
NUMBER is greater than the actual no!!!
Trials = 2

Do you want to guess a number(1:Yes / 2:No):
1
Guess a number and enter that number!!
73
NUMBER is greater than the actual no!!!
Trials = 3

Do you want to guess a number(1:Yes / 2:No):
1
Guess a number and enter that number!!
72
CONGRATULATIONS!!! GUESSED THE RIGHT NUMBER!!!
Trials = 4


OUTPUT 2 :

E:\\DIPI1\\corba prgs\\numgame>java CountClient -ORBInitialPort 1050 -ORBInitialHos
t a
Do you want to guess a number(1:Yes / 2:No):
1
Guess a number and enter that number!!
23
NUMBER is less than the actual no!!!
Trials = 1

Do you want to guess a number(1:Yes / 2:No):
1
Guess a number and enter that number!!
56
NUMBER is less than the actual no!!!
Trials = 2

Do you want to guess a number(1:Yes / 2:No):
1
Guess a number and enter that number!!
78
NUMBER is greater than the actual no!!!
Trials = 3

Do you want to guess a number(1:Yes / 2:No):
1
Guess a number and enter that number!!
67
NUMBER is greater than the actual no!!!
Trials = 4

Do you want to guess a number(1:Yes / 2:No):
1
Guess a number and enter that number!!
56
SORRY YOUR 5 TRIALS ARE OVER
Trials = 5

    Related Post:
  1. Develop a CORBA based application for addition of two complex numbers and displaying the result on client side

  2. Biological Sequence Allignment Using Dynamic Programing

  3. Socket program of multi client chat server

  4. An applet program to draw circle in center of the canvas

  5. Program to show the use of Labeled Break Statement

  6. RMI based application for login validation based on data saved in text file stored on the server and will display appropriate message to the client..

  7. Program sets up a String variable containing a paragraph of text of your choice. Extract the words from the text and sort them into alphabetical order

  8. Program to append a StringBuffer by StringBuffer, String objects and other Basic Data Types

  9. Program to show the use of Break Statement

  10. Program to display following things on the client machine a)date and time of the server machine b)date and time of the client machine and difference

  11. Corba program to encrypt the text that you want to send on the client side. The server side decrypts it after receiving. Append server time.

  12. Program to show an example of using a Static Nested Class outside the Top-Level Class

  13. Program of an applet to illustrate BarChart

  14. File handling program to copy characters from one file to another

  15. Corba program to Write a Echo server and client with UDP server and client

  16. Program to declare and initialize Boolean variables and print them on the Standard Output Device

  17. Create an interface coversions. Which have the following functions Inchestomillimeters(), hptowatts(),wattstohp(),poundstogram(),gramtopounds()

  18. RMI program of count server and client

  19. Game application in CORBA for which player will guess a number between 1 to 100, which will be compared to the random number generated by the class

  20. An applet program to find maximum of three numbers using textField