Interface.java
import java.rmi.*; // Provides remote method invocation.
import java.sql.*; //Communicates with a database.
//creation of interface
public interface interfacestud extends Remote
{
void Connect() throws RemoteException;
void setData() throws RemoteException;
String[] getIDstud() throws RemoteException;
String[] getNamestud() throws RemoteException;
String[] getCitystud() throws RemoteException;
String[] getIDBook() throws RemoteException;
String[] getNameBook() throws RemoteException;
String[] getCostBook() throws RemoteException;
}
Implstud.java
import java.rmi.*; // Provides remote method invocation.
import java.rmi.server.*; // supports remote method invocation.
import java.sql.*; //Communicates with a database.
import java.io.*; //provides support for I/O operations
public class implstud extends UnicastRemoteObject implements interfacestud //implementation of interfacestud interface
{
//creation of resultset
ResultSet std=null;
ResultSet lib=null;
//variable declaration
int i=0;
String Idstud[]=new String[5];
String Namestud[]=new String[5];
String Citystud[]=new String[5];
String Idbook[]=new String[5];
String Namebook[]=new String[5];
String Costbook[]=new String[5];
Statement st,st1=null; //statement creation
public implstud() throws RemoteException
{
}
//connection establishment with database
public void Connect() throws RemoteException
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:stud");
st=con.createStatement();
st1=con.createStatement();
String sav1="Select * from stud";
String sav2="Select * from Library";
std=st.executeQuery(sav1);
lib=st1.executeQuery(sav2);
}catch(ClassNotFoundException e){}
catch(SQLException e){}
}
//method to insert data in database
public void setData() throws RemoteException
{
try
{
int i=0;
while(std.next())
{
Idstud[i]=std.getString(1);
Namestud[i]=std.getString(2);
Citystud[i]=std.getString(3);
i=i+1;
}
i=0;
while(lib.next())
{
Idbook[i]=lib.getString(1);
Namebook[i]=lib.getString(2);
Costbook[i]=lib.getString(3);
i=i+1;
}
}catch(Exception e){}
}
//methods to read data from database
public String[] getIDstud() throws RemoteException
{
return(Idstud);
}
public String[] getNamestud() throws RemoteException
{
return(Namestud);
}
public String[] getCitystud() throws RemoteException
{
return(Citystud);
}
public String[] getIDBook() throws RemoteException
{
return(Idbook);
}
public String[] getNameBook() throws RemoteException
{
return(Namebook);
}
public String[] getCostBook() throws RemoteException
{
return(Costbook);
}
}
Server.java
//server program with main method
import java.net.*;
import java.rmi.*; // Provides remote method invocation.
public class server
{
public static void main(String args[])
{
try
{
implstud dispImpl1 = new implstud();
Naming.rebind("Display1", dispImpl1);
implstud dispImpl2 = new implstud();
Naming.rebind("Display2", dispImpl2);
}
catch(Exception e)
{
System.out.println("Exception: " + e);
}
}
}
Client.java
import java.rmi.*;
import java.sql.*;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class dClient extends JFrame
{
JTabbedPane jtp=null;
dClient()
{
super("Database Details");
Container c = getContentPane();
jtp= new JTabbedPane();
jtp.addTab("Student Details", new StudentPanel());
jtp.addTab("Library", new LibraryPanel());
c.add(jtp);
setVisible(true);
setSize(850,850);
}
}
class StudentPanel extends JPanel implements ActionListener
{
JButton Save,Exit;
String Idstud[]=new String[5];
String Namestud[]=new String[5];
String Citystud[]=new String[5];
String[][] data=new String[5][3];
String[] colHeads = {"Id","Name","City"};
JScrollPane jsp=null;
JTable table=null;
int i=0;
//function to communicate with server
public StudentPanel()
{
try
{
interfacestud dispIntf =(interfacestud)Naming.lookup("rmi://localhost/Display1");
dispIntf.Connect();
dispIntf.setData();
Save=new JButton("Save");
Exit=new JButton("Exit");
Idstud=dispIntf.getIDstud();
Namestud=dispIntf.getNamestud();
Citystud=dispIntf.getCitystud();
for(i=0;i<=4;i++)
{
System.out.print("ID="+Idstud[i]);
System.out.print(" Name="+Namestud[i]);
System.out.print(" City="+Citystud[i]);
System.out.println("");
}
for(i=0;i<=4;i++)
{
for(int j=0;j<1;j++)
{
data[i][j]=Idstud[i];
data[i][j+1]=Namestud[i];
data[i][j+2]=Citystud[i];
}
}
}catch(Exception e){}
table = new JTable(data, colHeads);
int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;
int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS;
jsp = new JScrollPane(table, v, h);
add(jsp);
add(Save);
add(Exit);
Exit.addActionListener(this);
Save.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==Exit)
System.exit(0);
if(ae.getSource()==Save)
{
String space="\t";
String line="\n";
FileOutputStream fos=null;
DataOutputStream dos=null;
try
{
File in=new File("File1.txt");
fos=new FileOutputStream(in);
}catch(FileNotFoundException e){}
try
{
for(i=0;i<=4;i++)
{
for(int j=0;j<1;j++)
{
fos.write(data[i][j].getBytes());
fos.write(space.getBytes());
fos.write(data[i][j+1].getBytes());
fos.write(space.getBytes());
fos.write(data[i][j+2].getBytes());
}
fos.write(line.getBytes());
}
}catch(IOException e){}
}
}
}
class LibraryPanel extends JPanel implements ActionListener
{
JButton Save,Exit;
String Idbook[]=new String[5];
String Namebook[]=new String[5];
String Costbook[]=new String[5];
String[][] data=new String[5][3];
String[] colHeads = {"Id","Name","Cost"};
JScrollPane jsp=null;
JTable table=null;
int i=0;
public LibraryPanel()
{
try
{
interfacestud dispIntf =(interfacestud)Naming.lookup("rmi://localhost/Display2");
dispIntf.Connect();
dispIntf.setData();
Save=new JButton("Save");
Exit=new JButton("Exit");
Idbook=dispIntf.getIDBook();
Namebook=dispIntf.getNameBook();
Costbook=dispIntf.getCostBook();
for(i=0;i<=4;i++)
{
System.out.print("ID="+Idbook[i]);
System.out.print(" Name="+Namebook[i]);
System.out.print(" Cost="+Costbook[i]);
System.out.println("");
}
for(i=0;i<=4;i++)
{
for(int j=0;j<1;j++)
{
data[i][j]=Idbook[i];
data[i][j+1]=Namebook[i];
data[i][j+2]=Costbook[i];
}
}
}catch(Exception e){}
table = new JTable(data, colHeads);
int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;
int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS;
jsp = new JScrollPane(table, v, h);
add(jsp);
add(Save);
add(Exit);
Exit.addActionListener(this);
Save.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==Exit)
System.exit(0);
if(ae.getSource()==Save)
{
String space="\t";
String line="\n";
FileOutputStream fos=null;
DataOutputStream dos=null;
try
{
File in=new File("File2.txt");
fos=new FileOutputStream(in);
}catch(FileNotFoundException e){}
try
{
for(i=0;i<=4;i++)
{
for(int j=0;j<1;j++)
{
fos.write(data[i][j].getBytes());
fos.write(space.getBytes());
fos.write(data[i][j+1].getBytes());
fos.write(space.getBytes());
fos.write(data[i][j+2].getBytes()); }
fos.write(line.getBytes());
}
}catch(IOException e){}
}
}
}
//class of client program with main method
public class client
{
public static void main(String args[])
{
dClient sc=new dClient();
}
}
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){}
}
}
}
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){}
}
}
}
IMPLEMENTATION OF QUEUE
/*Server Program */
import java.net.*; //provides support for networking
import java.io.*; //provides support for I/O operations
class qser
{
//Declaration of variables
ServerSocket ss=null;
String queue[]=new String[5];
int x;
Socket s=null;
String str=null;
int i;
//Function to perform operations on queue
public void check()
{
for(i=0;i<=4;i++)
{ queue[i]="";
}
try{
//Creates server socket on the specified port
ss=new ServerSocket(2000);
}
catch(IOException e)
{
System.out.println(e);
}
try{
System.out.println("Connection Established......");
s=ss.accept();
while(true)
{
//Input stream that translates bytes to characters
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
str=br.readLine();
x=Integer.parseInt(str);
if(x==1)//For the insert operation on queue
{
System.out.println("Insert");
str=br.readLine();
for(i=0;i<=4;i++)
{
if(queue[i]=="")
break;
}
if(queue[4]!="")
System.out.println("Queue full...");
else
{
System.out.println("Insert Operation Performed");
queue[i]=str;
}
}
if(x==2)//For the delete operation on queue
{
System.out.println("Delete");
for(i=1;i>=5;i--)
{
if(queue[i]!="")
break;
}
System.out.println("Delete Operation Performed -" +queue[i]);
queue[i]="";
}
if(x==3) //returns the values to the client
{
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
for(i=4;i>=0;i--)
{
pw.println(queue[i]);
pw.flush();
}
}
if(x==4)
{
System.exit(0);
//terminates the program
}
}
}
catch(IOException e)
{
System.out.println(e);
}
}
}
class QueueServer //class with main method
{
public static void main(String args[])
{
qser qs=new qser();
qs.check();
}
}
/* Client Program*/
import java.net.*; //provides support for networking
import java.io.*; //provides support for I/O operations
class qclient
{
//Declaration of variables
Socket s=null;
String queue[]=new String[5];
String st;
String str;
int i,x;
public void check()
{
//initialization of stack
for(i=0;i<=4;i++)
{
queue[i]="";
}
try{
//establish connection with server
s=new Socket(InetAddress.getLocalHost(),2000);
}
catch(IOException e)
{
System.out.println(e);
}
}
public void Contents() throws IOException
{
System.out.println("1 Insert");
System.out.println("2 Delete");
System.out.println("3 Display");
System.out.println("4 Exit");
Values();
}
public void Values() throws IOException
{
System.out.print("Select any 1 operation: ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
str=br.readLine();
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
pw.println(str);
x=Integer.parseInt(str);
if(x==1)
{
Insert(); // insert operation
}
if(x==2)
{
Values(); // delete operation
}
if(x==3)
{
Display();// displays the result
}
if(x==4)
{
System.exit(0); //exit from the program
}
}
public void Insert() throws IOException
{
System.out.println("Enter data: ");
BufferedReader br1=new BufferedReader(new InputStreamReader(System.in));
str=br1.readLine();
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
pw.println(str);
Values();
}
public void Display() throws IOException
{
System.out.println("Items in queue");
BufferedReader br2=new BufferedReader(new InputStreamReader(s.getInputStream()));
for(i=0;i<=4;i++)
{
queue[i]=br2.readLine();
System.out.println(queue[i]);
}
Values();
}
}
class QueueClient
{
public static void main(String args[]) throws IOException
{
qclient cl=new qclient();
cl.check();
cl.Contents();
}
}
import java.net.*; //provides support for networking
import java.io.*; //provides support for I/O operations
class qser
{
//Declaration of variables
ServerSocket ss=null;
String queue[]=new String[5];
int x;
Socket s=null;
String str=null;
int i;
//Function to perform operations on queue
public void check()
{
for(i=0;i<=4;i++)
{ queue[i]="";
}
try{
//Creates server socket on the specified port
ss=new ServerSocket(2000);
}
catch(IOException e)
{
System.out.println(e);
}
try{
System.out.println("Connection Established......");
s=ss.accept();
while(true)
{
//Input stream that translates bytes to characters
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
str=br.readLine();
x=Integer.parseInt(str);
if(x==1)//For the insert operation on queue
{
System.out.println("Insert");
str=br.readLine();
for(i=0;i<=4;i++)
{
if(queue[i]=="")
break;
}
if(queue[4]!="")
System.out.println("Queue full...");
else
{
System.out.println("Insert Operation Performed");
queue[i]=str;
}
}
if(x==2)//For the delete operation on queue
{
System.out.println("Delete");
for(i=1;i>=5;i--)
{
if(queue[i]!="")
break;
}
System.out.println("Delete Operation Performed -" +queue[i]);
queue[i]="";
}
if(x==3) //returns the values to the client
{
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
for(i=4;i>=0;i--)
{
pw.println(queue[i]);
pw.flush();
}
}
if(x==4)
{
System.exit(0);
//terminates the program
}
}
}
catch(IOException e)
{
System.out.println(e);
}
}
}
class QueueServer //class with main method
{
public static void main(String args[])
{
qser qs=new qser();
qs.check();
}
}
/* Client Program*/
import java.net.*; //provides support for networking
import java.io.*; //provides support for I/O operations
class qclient
{
//Declaration of variables
Socket s=null;
String queue[]=new String[5];
String st;
String str;
int i,x;
public void check()
{
//initialization of stack
for(i=0;i<=4;i++)
{
queue[i]="";
}
try{
//establish connection with server
s=new Socket(InetAddress.getLocalHost(),2000);
}
catch(IOException e)
{
System.out.println(e);
}
}
public void Contents() throws IOException
{
System.out.println("1 Insert");
System.out.println("2 Delete");
System.out.println("3 Display");
System.out.println("4 Exit");
Values();
}
public void Values() throws IOException
{
System.out.print("Select any 1 operation: ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
str=br.readLine();
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
pw.println(str);
x=Integer.parseInt(str);
if(x==1)
{
Insert(); // insert operation
}
if(x==2)
{
Values(); // delete operation
}
if(x==3)
{
Display();// displays the result
}
if(x==4)
{
System.exit(0); //exit from the program
}
}
public void Insert() throws IOException
{
System.out.println("Enter data: ");
BufferedReader br1=new BufferedReader(new InputStreamReader(System.in));
str=br1.readLine();
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
pw.println(str);
Values();
}
public void Display() throws IOException
{
System.out.println("Items in queue");
BufferedReader br2=new BufferedReader(new InputStreamReader(s.getInputStream()));
for(i=0;i<=4;i++)
{
queue[i]=br2.readLine();
System.out.println(queue[i]);
}
Values();
}
}
class QueueClient
{
public static void main(String args[]) throws IOException
{
qclient cl=new qclient();
cl.check();
cl.Contents();
}
}
Simulating Network Operating System commands
A) IMPLEMENTATION OF STACK
/*Server Program */
import java.net.*; //provides support for networking
import java.io.*; //provides support for I/O operations
class stser
{
//Declaration of variables
ServerSocket ss=null;
String stack[]=new String[5];
int x;
Socket s=null;
String str=null;
int i;
//Function to perform operations on stack
public void check()
{
for(i=0;i<=4;i++)
{
stack[i]="";
}
try
{
//Creates server socket on the specified port
ss=new ServerSocket(2000);
}
catch(IOException e)
{
System.out.println(e);
}
try
{
System.out.println("Connection Established......");
s=ss.accept();
while(true)
{//Input stream that translates bytes to characters
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
str=br.readLine();
x=Integer.parseInt(str);
if(x==1) //For the Push operation on stack
{
System.out.println("Push");
str=br.readLine();
for(i=0;i<=4;i++)
{
if(stack[i]=="")
break;
}
if(stack[4]!="")
System.out.println("Stack full...");
else
{
System.out.println("Push Operation Performed");
stack[i]=str;
}
}
if(x==2) //For the Pop operation on stack
{
System.out.println("Pop");
for(i=4;i>=0;i--)
{
if(stack[i]!="")
break;
}
System.out.println("Pop Operation Performed-" +stack[i]);
stack[i]="";
}
if(x==3)
//returns the values to the client
{//Output stream that contains print( ) and println( )
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
for(i=4;i>=0;i--)
{
pw.println(stack[i]);
pw.flush();
}
}
if(x==4) //terminates the program
{
System.exit(0);
}
}
}
catch(IOException e)
{
System.out.println(e);
}
}
}
class StackServer //class which contains the main method
{
public static void main(String args[])
{
stser st=new stser();
st.check();
}
}
/* Client Program */
import java.net.*; //provides support for networking
import java.io.*; //provides support for I/O operations
class stclient
{
//Declaration of variables
Socket s=null;
String stack[]=new String[5];
String st;
String str;
int i,x;
public void check()
{
//initialization of stack
for(i=0;i<=4;i++)
{
stack[i]="";
}
try
{
s=new Socket(InetAddress.getLocalHost(),2000);
}
catch(IOException e)
{
System.out.println(e);
}
}
public void Contents() throws IOException
{
System.out.println("1 Push");
System.out.println("2 Pop");
System.out.println("3 Display");
System.out.println("4 Exit");
Values();
}
public void Values() throws IOException
{
System.out.print("Select any 1 operation: ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
str=br.readLine();
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
pw.println(str);
x=Integer.parseInt(str);
if(x==1)
{
Push();//push operation
}
if(x==2)
{
Values(); // pop operation
}
if(x==3)
{
Display (); // displays the result
}
if(x==4)
{
System.exit(0); //exit from the program
}
}
public void Push() throws IOException
{
System.out.print("Enter data: ");
BufferedReader br1=new BufferedReader(new InputStreamReader(System.in));
str=br1.readLine();
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
pw.println(str);
Values();
}
public void Display() throws IOException
{
System.out.println("Items in Stack");
BufferedReader br2=new BufferedReader(new InputStreamReader(s.getInputStream()));
for(i=0;i<=4;i++)
{
stack[i]=br2.readLine();
System.out.println(stack[i]);
}
Values();
}
}
class StackClient
{
public static void main(String args[]) throws IOException
{
stclient cl=new stclient();
cl.check();
cl.Contents();
}
}
/*Server Program */
import java.net.*; //provides support for networking
import java.io.*; //provides support for I/O operations
class stser
{
//Declaration of variables
ServerSocket ss=null;
String stack[]=new String[5];
int x;
Socket s=null;
String str=null;
int i;
//Function to perform operations on stack
public void check()
{
for(i=0;i<=4;i++)
{
stack[i]="";
}
try
{
//Creates server socket on the specified port
ss=new ServerSocket(2000);
}
catch(IOException e)
{
System.out.println(e);
}
try
{
System.out.println("Connection Established......");
s=ss.accept();
while(true)
{//Input stream that translates bytes to characters
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
str=br.readLine();
x=Integer.parseInt(str);
if(x==1) //For the Push operation on stack
{
System.out.println("Push");
str=br.readLine();
for(i=0;i<=4;i++)
{
if(stack[i]=="")
break;
}
if(stack[4]!="")
System.out.println("Stack full...");
else
{
System.out.println("Push Operation Performed");
stack[i]=str;
}
}
if(x==2) //For the Pop operation on stack
{
System.out.println("Pop");
for(i=4;i>=0;i--)
{
if(stack[i]!="")
break;
}
System.out.println("Pop Operation Performed-" +stack[i]);
stack[i]="";
}
if(x==3)
//returns the values to the client
{//Output stream that contains print( ) and println( )
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
for(i=4;i>=0;i--)
{
pw.println(stack[i]);
pw.flush();
}
}
if(x==4) //terminates the program
{
System.exit(0);
}
}
}
catch(IOException e)
{
System.out.println(e);
}
}
}
class StackServer //class which contains the main method
{
public static void main(String args[])
{
stser st=new stser();
st.check();
}
}
/* Client Program */
import java.net.*; //provides support for networking
import java.io.*; //provides support for I/O operations
class stclient
{
//Declaration of variables
Socket s=null;
String stack[]=new String[5];
String st;
String str;
int i,x;
public void check()
{
//initialization of stack
for(i=0;i<=4;i++)
{
stack[i]="";
}
try
{
s=new Socket(InetAddress.getLocalHost(),2000);
}
catch(IOException e)
{
System.out.println(e);
}
}
public void Contents() throws IOException
{
System.out.println("1 Push");
System.out.println("2 Pop");
System.out.println("3 Display");
System.out.println("4 Exit");
Values();
}
public void Values() throws IOException
{
System.out.print("Select any 1 operation: ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
str=br.readLine();
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
pw.println(str);
x=Integer.parseInt(str);
if(x==1)
{
Push();//push operation
}
if(x==2)
{
Values(); // pop operation
}
if(x==3)
{
Display (); // displays the result
}
if(x==4)
{
System.exit(0); //exit from the program
}
}
public void Push() throws IOException
{
System.out.print("Enter data: ");
BufferedReader br1=new BufferedReader(new InputStreamReader(System.in));
str=br1.readLine();
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
pw.println(str);
Values();
}
public void Display() throws IOException
{
System.out.println("Items in Stack");
BufferedReader br2=new BufferedReader(new InputStreamReader(s.getInputStream()));
for(i=0;i<=4;i++)
{
stack[i]=br2.readLine();
System.out.println(stack[i]);
}
Values();
}
}
class StackClient
{
public static void main(String args[]) throws IOException
{
stclient cl=new stclient();
cl.check();
cl.Contents();
}
}
Subscribe to:
Posts (Atom)