Advertisement
Guest User

Untitled

a guest
Sep 28th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.89 KB | None | 0 0
  1. //mathServer//import java.rmi.*;import java.rmi.Naming.*;import java.rmi.server.*;import java.rmi.registry.*;
  2. import java.net.*;
  3. import java.util.*;
  4.  
  5. interface mathInterface extends Remote
  6. {
  7. public int add(int a,int b) throws RemoteException;
  8. public int subt(int a,int b) throws RemoteException;
  9. public int mult(int a,int b) throws RemoteException;
  10. public int div(int a,int b) throws RemoteException;
  11. }
  12.  
  13. public class mathServer extends UnicastRemoteObject implements
  14. mathInterface
  15.  
  16. {
  17. public mathServer() throws RemoteException
  18. {
  19. System.out.println("Initializing Server");
  20. }
  21. public int add(int a,int b)
  22. {
  23. return(a+b);
  24. }
  25. public int subt(int a,int b)
  26. {
  27. return(a-b);
  28. }
  29. public int mult(int a,int b)
  30. {
  31. return(a*b);
  32. }
  33. public int div(int a,int b)
  34. {
  35. return(a/b);
  36. }
  37. public static void main(String args[])
  38. {
  39. try
  40. {
  41. mathServer ms=new mathServer();
  42. java.rmi.Naming.rebind("MathServ",ms);
  43. System.out.println("Server Ready");
  44. }
  45. catch(RemoteException RE)
  46. {
  47. System.out.println("Remote Server Error:"+ RE.getMessage());
  48. System.exit(0);
  49. }
  50. catch(MalformedURLException ME)
  51. {
  52. System.out.println("Invalid URL!!");
  53. }
  54. }
  55. }
  56. //mathClient//
  57. import java.rmi.*;
  58. import java.rmi.registry.*;
  59. import java.awt.*;
  60. import java.awt.event.*;
  61. public class mathClient extends Frame implements ActionListener
  62. {
  63. Button B1=new Button("Sum");
  64. Button B2=new Button("Subtract");
  65. Button B3=new Button("Multiply");
  66. Button B4=new Button("Divide");
  67. Label l1=new Label("Number 1");
  68. Label l2=new Label("Number 2");
  69. Label l3=new Label("Result");
  70. TextField t1=new TextField(20);
  71. TextField t2=new TextField(20);
  72. TextField t3=new TextField(20);
  73. public mathClient()
  74. {
  75. super("Calculator");
  76. setLayout(null);
  77. l1.setBounds(20,50,55,25);
  78. add(l1);
  79. l2.setBounds(20,100,55,25);
  80. add(l2);
  81. l3.setBounds(20,150,55,25);
  82. add(l3);
  83. t1.setBounds(150,50,100,25);
  84. add(t1);
  85. t2.setBounds(150,100,100,25);
  86. add(t2);
  87. t3.setBounds(150,150,100,25);
  88. add(t3);
  89. B1.setBounds(20,200,80,25);
  90. add(B1);
  91. B2.setBounds(100,200,80,25);
  92. add(B2);
  93. B3.setBounds(180,200,80,25);
  94. add(B3);
  95. B4.setBounds(260,200,80,25);
  96. add(B4);
  97. B1.addActionListener(this);
  98. B2.addActionListener(this);
  99. B3.addActionListener(this);
  100. B4.addActionListener(this);
  101. addWindowListener(
  102. new WindowAdapter()
  103. {
  104. public void windowClosing(WindowEvent e)
  105. {
  106. System.exit(0);
  107. }
  108. }
  109. );
  110. }
  111. public void actionPerformed(ActionEvent AE)
  112. {
  113. if(AE.getSource()==B1)
  114. {
  115. sum();
  116. }
  117. else if(AE.getSource()==B2)
  118. {
  119. subt();
  120. }
  121. else if(AE.getSource()==B3)
  122. {
  123. mult();
  124. }
  125. else if(AE.getSource()==B4)
  126. {
  127. div();
  128. }
  129. }
  130. public void sum()
  131. {
  132. int i=Integer.parseInt(t1.getText());
  133. int j=Integer.parseInt(t2.getText());
  134. int val;
  135. try
  136. {
  137. String ServerURL="MathServ";
  138. mathInterface MI=(mathInterface)Naming.lookup(ServerURL);
  139. val=MI.add(i,j);
  140. t3.setText(""+val);
  141. }
  142. catch(Exception ex)
  143. {
  144. System.out.println("Exception:"+ex);
  145. }
  146. }
  147. public void subt()
  148. {
  149. int i=Integer.parseInt(t1.getText());
  150. int j=Integer.parseInt(t2.getText());
  151. int val;
  152. try
  153. {
  154. String ServerURL="MathServ";
  155. mathInterface MI=(mathInterface)Naming.lookup(ServerURL);
  156. val=MI.subt(i,j);
  157. t3.setText(""+val);
  158. }
  159. catch(Exception ex)
  160. {
  161. System.out.println("Exception:"+ex);
  162. }
  163. }
  164. public void mult()
  165. {
  166. int i=Integer.parseInt(t1.getText());
  167. int j=Integer.parseInt(t2.getText());
  168. int val;
  169. try
  170. {
  171. String ServerURL="MathServ";
  172. mathInterface MI=(mathInterface)Naming.lookup(ServerURL);
  173. val=MI.mult(i,j);
  174. t3.setText(""+val);
  175. }
  176. catch(Exception ex)
  177. {
  178. System.out.println("Exception:"+ex);
  179. }
  180. }
  181. public void div()
  182. {
  183. int i=Integer.parseInt(t1.getText());
  184. int j=Integer.parseInt(t2.getText());
  185. int val;
  186. try
  187. {
  188. String ServerURL="MathServ";
  189. mathInterface MI=(mathInterface)Naming.lookup(ServerURL);
  190. val=MI.div(i,j);
  191. t3.setText(""+val);
  192. }
  193. catch(Exception ex)
  194. {
  195. System.out.println("Exception:"+ex);
  196. }
  197. }
  198. public static void main(String args[])
  199. {
  200. mathClient MC=new mathClient();
  201. MC.setVisible(true);
  202. MC.setSize(600,500);
  203. };
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement