Java Tutorial

 
 
 
 
----------------------------------------------------------------------------------
                    IDL
----------------------------------------------------------------------------------
module CalciApp
{
    interface Calci
         {
               void Calculator();
               oneway void shutdown();
          };
};

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

import CalciApp.*;
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 CalciImpl extends CalciPOA 
{
          private ORB orb;

          public void setORB(ORB orb_val) 
          {
                orb = orb_val; 
          }    
          public void Calculator() 
          {
                   System.out.print(\"Calculator Starts....\");               
          }
          public void shutdown() 
          {
                  orb.shutdown(false);
          }
}

public class CalciServer 
{
           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();  

                      CalciImpl calciImpl = new CalciImpl();
                      calciImpl.setORB(orb); 

                      org.omg.CORBA.Object ref = rootpoa.servant_to_reference(calciImpl);
                      Calci href = CalciHelper.narrow(ref);  

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

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

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

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

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

import CalciApp.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import java.awt.Graphics;
import java.awt.*;

public class CalciApplet extends java.applet.Applet
{
          TextField t1;
          Button b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, bp,bm, bml, bd, be, bst;
          int m=0;
          int op=0;
          public void init() 
          { 
        t1=new TextField(20);
                b0=new Button(\"0\");
                b1=new Button(\"1\");
                b2=new Button(\"2\");
                b3=new Button(\"3\");
                b4=new Button(\"4\");
                b5=new Button(\"5\");
                b6=new Button(\"6\");
                b7=new Button(\"7\");
                b8=new Button(\"8\");
                b9=new Button(\"9\");
                bp=new Button(\"+\");
                bm=new Button(\"-\");
                bml=new Button(\"*\");
                bd=new Button(\"/\");
                be=new Button(\"=\");
                bst=new Button(\"AC\");

        add(t1);
                add(b0); add(b1); add(b2); add(b3);
                add(b4); add(b5); add(b6); add(b7);
                add(b8); add(b9); add(bp); add(bm);
                add(bml); add(bd); add(be); add(bst);
                t1.setText(\"0\");

                try
        {
                   ORB orb = ORB.init(this, null);

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

            NameComponent nc = new NameComponent(\"Calci\", \"\");
                        NameComponent path[] = {nc};    
            Calci calciRef = CalciHelper.narrow(ncRef.resolve(path));

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

       }

       public boolean action(Event e1, java.lang.Object o1)
       {
                   if(e1.arg.equals(\"0\"))
                          t1.setText(t1.getText()+\"0\");
                   if(e1.arg.equals(\"1\"))
                          t1.setText(t1.getText()+\"1\");
                   if(e1.arg.equals(\"2\"))
                          t1.setText(t1.getText()+\"2\");
                   if(e1.arg.equals(\"3\"))
                          t1.setText(t1.getText()+\"3\");
                   if(e1.arg.equals(\"4\"))
                          t1.setText(t1.getText()+\"4\");
                   if(e1.arg.equals(\"5\"))
                          t1.setText(t1.getText()+\"5\");
                   if(e1.arg.equals(\"6\"))
                           t1.setText(t1.getText()+\"6\");
                   if(e1.arg.equals(\"7\"))
                          t1.setText(t1.getText()+\"7\");
                   if(e1.arg.equals(\"8\"))
                          t1.setText(t1.getText()+\"8\");
                   if(e1.arg.equals(\"9\"))
                          t1.setText(t1.getText()+\"9\");
                   if(e1.arg.equals(\"+\"))
                   {
            m=Integer.parseInt(t1.getText());
                        t1.setText(\"0\");
                        op=0;
           }
                   if(e1.arg.equals(\"-\"))
                   {
                       m=Integer.parseInt(t1.getText());
                        t1.setText(\"0\");
                        op=1;
                   }
                   if(e1.arg.equals(\"*\"))
                   {
            m=Integer.parseInt(t1.getText());
                        t1.setText(\"0\");
                        op=2;
                   }
                   if(e1.arg.equals(\"/\"))
                   {
                       m=Integer.parseInt(t1.getText());
                        t1.setText(\"0\");
                        op=3;
                   }
                   if(e1.arg.equals(\"=\"))
                   {
                       switch(op)
                        {
                            case 0:
                                    m=m+Integer.parseInt(t1.getText());
                                        break;
                                  case 1:
                                    m=m-Integer.parseInt(t1.getText());
                                        break;
                                  case 2:
                                        m=m*Integer.parseInt(t1.getText());
                                        break;
                                  case 3:
                    m=m/Integer.parseInt(t1.getText());
                                        break;
                         }
                         t1.setText(String.valueOf(m));
                   }
                   if(e1.arg.equals(\"AC\"))
                        t1.setText(\"0\");
                   return true;
         }       
}

-------------------------------------------------------------------------------------------------
                    Applet (HTML File)
-------------------------------------------------------------------------------------------------

<HTML><HEAD>
<TITLE>Java IDL Getting Started: Running Calculator Applet</TITLE>
</HEAD>
<BODY BGCOLOR=\"#FFFFFF\">
<H1 ALIGN=CENTER>Running the Calculator Applet</H1>
<HR><P>If all goes well, the applet appears below:<P>
<APPLET CODE=CalciApplet.class       WIDTH=500        HEIGHT=300>
<PARAM name=\"org.omg.CORBA.ORBInitialHost\" value=localhost>
<PARAM name=\"org.omg.CORBA.ORBInitialPort\" value=1050>
</APPLET>
</BODY>
</HTML>

    Related Post:
  1. Program to show an example of using constructors in a class to initialize its data members

  2. RMI application for getting the final total price of the shopping list with some of the following items and quantities chosen by the user

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

  4. Program to show an example of modifying String objects

  5. Program to determine the sum of the following harmonic series for a given value of n

  6. Program of linear search

  7. An applet program to display Vertical Color Bar

  8. corba program of client and a DNS server where given a URL the server sends back an IP address

  9. Program to show the use of While Loop

  10. Program to check the start and end of a String

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

  12. Program for calculating area of a circle on server side,when a client sends request along with given radius and then displaying result on client side

  13. Program to show an example of using a recursive method in a class

  14. Program of reading and writing primitive data

  15. Program to show the use of Logical or Boolean Negation i.e. NOT (!)

  16. An applet program to display Horizontal Color Bar

  17. Program that accepts a shopping list of five items from the command line and stores them in vector

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

  19. Program to show the use of operator (&gt;&gt;&gt;) Shift Right, filling with zeros from the left

  20. Program to compute the sum of the digits of a given integer number. eg: I/o=4 then O/p=4+3+2+1=10

 
 
Didn't find what you were looking for? Find more on Program that enabled applet calculator