Saturday, January 16, 2010

Remote Method Invocation supporting the distributed computing in java

A) CalcIntf.java

import java.rmi.*; // Provides remote method invocation.
import java.util.*; // Contains common utilities.

//creation of interface
interface CalcIntf extends Remote
{
double Calculator(float a,float b,char ch) throws RemoteException;
}


CalcImpl.java

import java.rmi.*; // Provides remote method invocation.
import java.rmi.server.*; // Supports remote method invocation.
import java.io.*; //provides support for I/O operations
import java.util.*; // Contains common utilities.

public class CalcImpl extends UnicastRemoteObject implements CalcIntf //implementation of CalcIntf class
{
CalcImpl() throws RemoteException
{}
public double Calculator(float x,float y,char op) throws RemoteException
{
double z=0;
switch(op)
{
case '+': //addition
z=x+y;
break;
case '-': //substraction
z=x-y;
break;
case '*': //multiplication
z=x*y;
break;
case '/': //division
z=x/y;
break;
}
return z; //returns the result
}
}
server.java

//server program with main method
import java.rmi.*;
import java.rmi.server.*;
import java.io.*;
public class server
{
public static void main(String arg[]) throws Exception
{
CalcImpl obj=new CalcImpl();
Naming.rebind("Calci",obj);
}
}


client.java

//client program to communicate with server
import javax.swing.*;
import java.rmi.*;
import java.io.*;
import java.util.*;
import java.awt.event.*;
import java.awt.*;
import java.rmi.server.*;

public class client
{
public static void main(String arg[]) throws Exception
{
new Calci();
}
}

class Calci extends JFrame implements ActionListener,KeyListener
{
// variable declaration
float a,b;
char op;
GridBagLayout gb=new GridBagLayout();
//sets layout of screen
GridBagConstraints gbc=new GridBagConstraints();
Container cp=getContentPane();
Component com;
//textfield
JTextField t1=new JTextField(10);

//buttons
JButton one=new JButton("1");
JButton two=new JButton("2");
JButton three=new JButton("3");
JButton four=new JButton("4");
JButton five=new JButton("5");
JButton six=new JButton("6");
JButton seven=new JButton("7");
JButton eight=new JButton("8");
JButton nine=new JButton("9");
JButton zero=new JButton("0");
JButton dot=new JButton(".");
JButton add=new JButton("+");
JButton sub=new JButton("-");
JButton mul=new JButton("*");
JButton div=new JButton("/");
JButton eql=new JButton("=");
JButton Clear=new JButton("Clear");

void addcom(int row,int col,int w,int h,Component cm)
{
gbc.gridx=col;
gbc.gridy=row;
gbc.gridwidth=w;
gbc.gridheight=h;
gb.setConstraints(cm,gbc);
cp.add(cm);
}

public Calci()
{
cp.setLayout(gb);
gbc.fill=GridBagConstraints.BOTH;
gbc.insets=new Insets(5,5,5,5);
gbc.weightx=1.0;
gbc.weighty=1.0;

t1.addActionListener(this);
one.addActionListener(this);
two.addActionListener(this);
three.addActionListener(this);
four.addActionListener(this);
five.addActionListener(this);
six.addActionListener(this);
seven.addActionListener(this);
eight.addActionListener(this);
nine.addActionListener(this);
zero.addActionListener(this);
add.addActionListener(this);
sub.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);
dot.addActionListener(this);
eql.addActionListener(this);
Clear.addActionListener(this);

t1.addKeyListener(this);
one.addKeyListener(this);
two.addKeyListener(this);
three.addKeyListener(this);
four.addKeyListener(this);
five.addKeyListener(this);
six.addKeyListener(this);
seven.addKeyListener(this);
eight.addKeyListener(this);
nine.addKeyListener(this);
zero.addKeyListener(this);
add.addKeyListener(this);
sub.addKeyListener(this);
mul.addKeyListener(this);
div.addKeyListener(this);
dot.addKeyListener(this);
eql.addKeyListener(this);
Clear.addKeyListener(this);

addcom(0,0,3,1,t1);
t1.setEnabled(false);
addcom(0,3,2,1,Clear);
addcom(1,0,1,1,one);
addcom(1,1,1,1,two);
addcom(1,2,1,1,three);
addcom(1,3,1,1,sub);
addcom(1,4,1,1,mul);
addcom(2,0,1,1,four);
addcom(2,1,1,1,five);
addcom(2,2,1,1,six);
addcom(2,3,1,3,eql);
addcom(2,4,1,1,div);
addcom(3,0,1,1,seven);
addcom(3,1,1,1,eight);
addcom(3,2,1,1,nine);
addcom(3,4,1,2,add);
addcom(4,0,2,1,zero);
addcom(4,2,1,1,dot);
requestFocus();
setSize(380,240);
setTitle("Calculator");
setVisible(true);
}


//methods to handle key events
public void keyReleased(KeyEvent ke)
{
char key = ke.getKeyChar();
if ( key == '+' )
{
t1.setText("");
}
else if ( key == '-' )
{
t1.setText("");
}
else if ( key == '*' )
{
t1.setText("");
}
else if ( key == '/' )
{
t1.setText("");
}
else if ( key == 'c' )
{
t1.setText("");
}
}

public void keyTyped(KeyEvent ke){}

public void keyPressed(KeyEvent ke)
{
char key = ke.getKeyChar();
if(ke.getKeyChar()>=ke.VK_0 && ke.getKeyChar()<=ke.VK_9)
{
t1.setText(t1.getText()+ke.getKeyChar());
}
if ( key == '+' )
{
a = Float.parseFloat(t1.getText());
op = '+';
t1.setText("");
}
if ( key == '-' )
{
a = Float.parseFloat(t1.getText());
op = '-';
}
if ( key == '*' )
{
a = Float.parseFloat(t1.getText());
op = '*';
}
if ( key == '/' )
{
a = Float.parseFloat(t1.getText());
op = '/';
}
if ( key == ke.VK_DELETE)
{
t1.setText("");
}
if ( key == ke.VK_ESCAPE)
{
t1.setText("");
}
if ( key == ke.VK_BACK_SPACE)
{
int bk;
bk=t1.getText().trim().length();
bk=bk-1;
if(bk<=0)
{
t1.setText("0.");
}
else
{
t1.setText(t1.getText().trim().substring(0,bk));
}
}
if ( key == ke.VK_ENTER)
{
b=(Float.valueOf(t1.getText())).floatValue();
try
{
CalcIntf A=(CalcIntf)Naming.lookup("Calci");
double ans=A.Calculator(a,b,op);
t1.setText(ans+"");
System.out.print(a);
System.out.print(op);
System.out.print(b);
System.out.println("="+ans);
}
catch(Exception e)
{}
}
if ( key == 'c' )
{
t1.setText("");
}
}

//methods to handle button events
public void actionPerformed(ActionEvent ae)
{
String str=ae.getActionCommand();

if(str.equals("1"))
t1.setText(t1.getText()+"1");
if(str.equals("2"))
t1.setText(t1.getText()+"2");
if(str.equals("3"))
t1.setText(t1.getText()+"3");
if(str.equals("4"))
t1.setText(t1.getText()+"4");
if(str.equals("5"))
t1.setText(t1.getText()+"5");
if(str.equals("6"))
t1.setText(t1.getText()+"6");
if(str.equals("7"))
t1.setText(t1.getText()+"7");
if(str.equals("8"))
t1.setText(t1.getText()+"8");
if(str.equals("9"))
t1.setText(t1.getText()+"9");
if(str.equals("0"))
t1.setText(t1.getText()+"0");
if(str.equals("."))
t1.setText(t1.getText()+".");
if(str.equals("Clear"))
t1.setText("");
if(str.equals("+"))
{
a=(Float.valueOf(t1.getText())).floatValue();
op='+';
t1.setText("");
}
if(str.equals("-"))
{
a=(Float.valueOf(t1.getText())).floatValue();
op='-';
t1.setText("");
}
if(str.equals("*"))
{
a=(Float.valueOf(t1.getText())).floatValue();
op='*';
t1.setText("");
}
if(str.equals("/"))
{
a=(Float.valueOf(t1.getText())).floatValue();
op='/';
t1.setText("");
}
if(str.equals("="))
{
b=(Float.valueOf(t1.getText())).floatValue();
try{
CalcIntf A=(CalcIntf)Naming.lookup("Calci");
double ans=A.Calculator(a,b,op);
t1.setText(" "+ans);
System.out.print(a);
System.out.print(op);
System.out.print(b);
System.out.println("="+ans);
t1.requestFocus();
}
catch(Exception e){}
}
}
}

No comments:

Post a Comment