0% found this document useful (0 votes)
6 views

Assignment 5

Uploaded by

Karan Margaje
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Assignment 5

Uploaded by

Karan Margaje
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

5. Create the following programs using RMI

a. Calculator application

Code:

Icalc.java

package rmicalc;

import java.rmi.*;

/**

* @author Karan

*/

public interface icalc extends Remote

public double add(double a,double b) throws RemoteException;

public double sub(double a,double b) throws RemoteException;

public double mul(double a,double b) throws RemoteException;

public double div(double a,double b) throws RemoteException;

public double mod(double a,double b) throws RemoteException;

Calremote.java

package rmicalc;

import java.rmi.*;

import java.rmi.server.UnicastRemoteObject;

/**

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

* @author Karan

*/

public class calremote extends UnicastRemoteObject implements icalc

public calremote() throws RemoteException

super();

@Override

public double add(double a, double b) throws RemoteException {

return (a+b);

@Override

public double sub(double a, double b) throws RemoteException {

return (a-b);

@Override

public double mul(double a, double b) throws RemoteException {

return (a*b);

@Override

public double div(double a, double b) throws RemoteException {

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

return (a/b);

@Override

public double mod(double a, double b) throws RemoteException {

return (a%b);

Server.java

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package rmicalc;

import java.rmi.*;

import java.rmi.registry.LocateRegistry;

import java.rmi.registry.Registry;

/**

* @author Karan

*/

public class server extends calremote

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

public server() throws RemoteException

public static void main(String args[]) throws RemoteException{

try

Registry reg = LocateRegistry.createRegistry(9999);

calremote s = new calremote();

reg.rebind("Server",s);

System.out.println("Server is ready!");

catch(Exception e)

System.out.println(e);

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

Client.java

package rmicalc;

import java.io.*;

import java.util.*;

import java.rmi.*;

import java.rmi.registry.Registry;

import java.rmi.registry.LocateRegistry;

import java.awt.Cursor;

import java.awt.Font;

import java.util.regex.Pattern;

import java.awt.Color;

import javax.swing.*;

import java.lang.Math;

/**

* @author Karan

*/

public class client {

private static final int WINDOW_WIDTH = 410;

private static final int WINDOW_HEIGHT = 600;

private static final int BUTTON_WIDTH = 80; // Button width

private static final int BUTTON_HEIGHT = 70; // Button height

private static final int MARGIN_X = 20;

private static final int MARGIN_Y = 60;

private JFrame window; // Main window

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

private JTextField inText; // Input text

private JButton btnC, btnBack, btnMod, btnDiv, btn7, btn8, btn9,

btnMul, btn4, btn5, btn6, btnSub, btn1, btn2, btn3, btnAdd, btnPoint, btn0,
btnEqual, btnRoot, btnPower, btnLog, btnSwitchThemes, btnSwitchToScientificMode;

private char opt = ' '; // Save the operator

private boolean go = true; // For calculate with Opt != (=)

private boolean addWrite = true; // Racordé des Nombres dans l'Affichage

private double val = 0; // Save value typed for calculation

private double result;

private client() {

window = new JFrame("Calculator");

window.setSize(WINDOW_WIDTH, WINDOW_HEIGHT); // Set the width and the


Height of the window

window.setLocationRelativeTo(null); // Move Window To Center

// Button fonts

Font btnFont = new Font("Comic Sans MS", Font.PLAIN, 28);

Font smallTxtBtnFont = new Font("Comic Sans MS", Font.PLAIN, 24);

int j = -1;

int k = -1;

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

int[] x = {MARGIN_X, MARGIN_X + 90, 200, 290, 380};

int[] y = {MARGIN_Y, MARGIN_Y + 100, MARGIN_Y + 180, MARGIN_Y + 260,


MARGIN_Y + 340, MARGIN_Y + 420};

inText = new JTextField("0");

inText.setBounds(x[0], y[0], 350, 70);

inText.setEditable(false);

inText.setBackground(Color.WHITE);

inText.setFont(new Font("Comic Sans MS", Font.PLAIN, 33));

window.add(inText);

btnC = new JButton("C");

btnC.setBounds(x[0], y[1], BUTTON_WIDTH, BUTTON_HEIGHT);

btnC.setFont(btnFont);

btnC.setCursor(new Cursor(Cursor.HAND_CURSOR));

btnC.addActionListener(event -> {

repaintFont();

inText.setText("0");

opt = ' ';

val = 0;

});

window.add(btnC);

btnBack = new JButton("<-");

btnBack.setBounds(x[1], y[1], BUTTON_WIDTH, BUTTON_HEIGHT);

btnBack.setFont(btnFont);

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

btnBack.setCursor(new Cursor(Cursor.HAND_CURSOR));

btnBack.addActionListener(event -> {

repaintFont();

String str = inText.getText();

StringBuilder str2 = new StringBuilder();

for (int i = 0; i < (str.length() - 1); i++) {

str2.append(str.charAt(i));

if (str2.toString().equals("")) {

inText.setText("0");

} else {

inText.setText(str2.toString());

});

window.add(btnBack);

btnMod = new JButton("%");

btnMod.setBounds(x[2], y[1], BUTTON_WIDTH, BUTTON_HEIGHT);

btnMod.setFont(btnFont);

btnMod.setCursor(new Cursor(Cursor.HAND_CURSOR));

btnMod.addActionListener(event -> {

repaintFont();

if (Pattern.matches("([-]?\\d+[.]\\d*)|(\\d+)", inText.getText()))

if (go) {

val = calc(val, inText.getText(), opt);

if (Pattern.matches("[-]?[\\d]+[.][0]*", String.valueOf(val))) {

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

inText.setText(String.valueOf((int) val));

} else {

inText.setText(String.valueOf(val));

opt = '%';

go = false;

addWrite = false;

});

window.add(btnMod);

btnDiv = new JButton("/");

btnDiv.setBounds(x[3], y[1], BUTTON_WIDTH, BUTTON_HEIGHT);

btnDiv.setFont(btnFont);

btnDiv.setCursor(new Cursor(Cursor.HAND_CURSOR));

btnDiv.addActionListener(event -> {

repaintFont();

if (Pattern.matches("([-]?\\d+[.]\\d*)|(\\d+)", inText.getText()))

if (go) {

val = calc(val, inText.getText(), opt);

if (Pattern.matches("[-]?[\\d]+[.][0]*", String.valueOf(val))) {

inText.setText(String.valueOf((int) val));

} else {

inText.setText(String.valueOf(val));

opt = '/';

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

go = false;

addWrite = false;

} else {

opt = '/';

});

window.add(btnDiv);

btn7 = new JButton("7");

btn7.setBounds(x[0], y[2], BUTTON_WIDTH, BUTTON_HEIGHT);

btn7.setFont(btnFont);

btn7.setCursor(new Cursor(Cursor.HAND_CURSOR));

btn7.addActionListener(event -> {

repaintFont();

if (addWrite) {

if (Pattern.matches("[0]*", inText.getText())) {

inText.setText("7");

} else {

inText.setText(inText.getText() + "7");

} else {

inText.setText("7");

addWrite = true;

go = true;

});

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

window.add(btn7);

btn8 = new JButton("8");

btn8.setBounds(x[1], y[2], BUTTON_WIDTH, BUTTON_HEIGHT);

btn8.setFont(btnFont);

btn8.setCursor(new Cursor(Cursor.HAND_CURSOR));

btn8.addActionListener(event -> {

repaintFont();

if (addWrite) {

if (Pattern.matches("[0]*", inText.getText())) {

inText.setText("8");

} else {

inText.setText(inText.getText() + "8");

} else {

inText.setText("8");

addWrite = true;

go = true;

});

window.add(btn8);

btn9 = new JButton("9");

btn9.setBounds(x[2], y[2], BUTTON_WIDTH, BUTTON_HEIGHT);

btn9.setFont(btnFont);

btn9.setCursor(new Cursor(Cursor.HAND_CURSOR));

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

btn9.addActionListener(event -> {

repaintFont();

if (addWrite) {

if (Pattern.matches("[0]*", inText.getText())) {

inText.setText("9");

} else {

inText.setText(inText.getText() + "9");

} else {

inText.setText("9");

addWrite = true;

go = true;

});

window.add(btn9);

btnMul = new JButton("*");

btnMul.setBounds(x[3], y[2], BUTTON_WIDTH, BUTTON_HEIGHT);

btnMul.setFont(btnFont);

btnMul.setCursor(new Cursor(Cursor.HAND_CURSOR));

btnMul.addActionListener(event -> {

repaintFont();

if (Pattern.matches("([-]?\\d+[.]\\d*)|(\\d+)", inText.getText()))

if (go) {

val = calc(val, inText.getText(), opt);

if (Pattern.matches("[-]?[\\d]+[.][0]*", String.valueOf(val))) {

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

inText.setText(String.valueOf((int) val));

} else {

inText.setText(String.valueOf(val));

opt = '*';

go = false;

addWrite = false;

} else {

opt = '*';

});

window.add(btnMul);

btn4 = new JButton("4");

btn4.setBounds(x[0], y[3], BUTTON_WIDTH, BUTTON_HEIGHT);

btn4.setFont(btnFont);

btn4.setCursor(new Cursor(Cursor.HAND_CURSOR));

btn4.addActionListener(event -> {

repaintFont();

if (addWrite) {

if (Pattern.matches("[0]*", inText.getText())) {

inText.setText("4");

} else {

inText.setText(inText.getText() + "4");

} else {

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

inText.setText("4");

addWrite = true;

go = true;

});

window.add(btn4);

btn5 = new JButton("5");

btn5.setBounds(x[1], y[3], BUTTON_WIDTH, BUTTON_HEIGHT);

btn5.setFont(btnFont);

btn5.setCursor(new Cursor(Cursor.HAND_CURSOR));

btn5.addActionListener(event -> {

repaintFont();

if (addWrite) {

if (Pattern.matches("[0]*", inText.getText())) {

inText.setText("5");

} else {

inText.setText(inText.getText() + "5");

} else {

inText.setText("5");

addWrite = true;

go = true;

});

window.add(btn5);

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

btn6 = new JButton("6");

btn6.setBounds(x[2], y[3], BUTTON_WIDTH, BUTTON_HEIGHT);

btn6.setFont(btnFont);

btn6.setCursor(new Cursor(Cursor.HAND_CURSOR));

btn6.addActionListener(event -> {

repaintFont();

if (addWrite) {

if (Pattern.matches("[0]*", inText.getText())) {

inText.setText("6");

} else {

inText.setText(inText.getText() + "6");

} else {

inText.setText("6");

addWrite = true;

go = true;

});

window.add(btn6);

btnSub = new JButton("-");

btnSub.setBounds(x[3], y[3], BUTTON_WIDTH, BUTTON_HEIGHT);

btnSub.setFont(btnFont);

btnSub.setCursor(new Cursor(Cursor.HAND_CURSOR));

btnSub.addActionListener(event -> {

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

repaintFont();

if (Pattern.matches("([-]?\\d+[.]\\d*)|(\\d+)", inText.getText()))

if (go) {

val = calc(val, inText.getText(), opt);

if (Pattern.matches("[-]?[\\d]+[.][0]*", String.valueOf(val))) {

inText.setText(String.valueOf((int) val));

} else {

inText.setText(String.valueOf(val));

opt = '-';

go = false;

addWrite = false;

} else {

opt = '-';

});

window.add(btnSub);

btn1 = new JButton("1");

btn1.setBounds(x[0], y[4], BUTTON_WIDTH, BUTTON_HEIGHT);

btn1.setFont(btnFont);

btn1.setCursor(new Cursor(Cursor.HAND_CURSOR));

btn1.addActionListener(event -> {

repaintFont();

if (addWrite) {

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

if (Pattern.matches("[0]*", inText.getText())) {

inText.setText("1");

} else {

inText.setText(inText.getText() + "1");

} else {

inText.setText("1");

addWrite = true;

go = true;

});

window.add(btn1);

btn2 = new JButton("2");

btn2.setBounds(x[1], y[4], BUTTON_WIDTH, BUTTON_HEIGHT);

btn2.setFont(btnFont);

btn2.setCursor(new Cursor(Cursor.HAND_CURSOR));

btn2.addActionListener(event -> {

repaintFont();

if (addWrite) {

if (Pattern.matches("[0]*", inText.getText())) {

inText.setText("2");

} else {

inText.setText(inText.getText() + "2");

} else {

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

inText.setText("2");

addWrite = true;

go = true;

});

window.add(btn2);

btn3 = new JButton("3");

btn3.setBounds(x[2], y[4], BUTTON_WIDTH, BUTTON_HEIGHT);

btn3.setFont(btnFont);

btn3.setCursor(new Cursor(Cursor.HAND_CURSOR));

btn3.addActionListener(event -> {

repaintFont();

if (addWrite) {

if (Pattern.matches("[0]*", inText.getText())) {

inText.setText("3");

} else {

inText.setText(inText.getText() + "3");

} else {

inText.setText("3");

addWrite = true;

go = true;

});

window.add(btn3);

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

btnAdd = new JButton("+");

btnAdd.setBounds(x[3], y[4], BUTTON_WIDTH, BUTTON_HEIGHT);

btnAdd.setFont(btnFont);

btnAdd.setCursor(new Cursor(Cursor.HAND_CURSOR));

btnAdd.addActionListener(event -> {

repaintFont();

if (Pattern.matches("([-]?\\d+[.]\\d*)|(\\d+)", inText.getText()))

if (go) {

val = calc(val, inText.getText(), opt);

if (Pattern.matches("[-]?[\\d]+[.][0]*", String.valueOf(val))) {

inText.setText(String.valueOf((int) val));

} else {

inText.setText(String.valueOf(val));

opt = '+';

go = false;

addWrite = false;

} else {

opt = '+';

});

window.add(btnAdd);

btnPoint = new JButton(".");

btnPoint.setBounds(x[0], y[5], BUTTON_WIDTH, BUTTON_HEIGHT);

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

btnPoint.setFont(new Font("Comic Sans MS", Font.BOLD, 32));

btnPoint.setCursor(new Cursor(Cursor.HAND_CURSOR));

btnPoint.addActionListener(event -> {

repaintFont();

if (addWrite) {

inText.setText(inText.getText() + ".");

} else {

inText.setText("0.");

addWrite = true;

go = true;

});

window.add(btnPoint);

btn0 = new JButton("0");

btn0.setBounds(x[1], y[5], BUTTON_WIDTH, BUTTON_HEIGHT);

btn0.setFont(btnFont);

btn0.setCursor(new Cursor(Cursor.HAND_CURSOR));

btn0.addActionListener(event -> {

repaintFont();

if (addWrite) {

if (Pattern.matches("[0]*", inText.getText())) {

inText.setText("0");

} else {

inText.setText(inText.getText() + "0");

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

} else {

inText.setText("0");

addWrite = true;

go = true;

});

window.add(btn0);

btnEqual = new JButton("=");

btnEqual.setBounds(x[2], y[5], 2 * BUTTON_WIDTH + 10, BUTTON_HEIGHT);

btnEqual.setFont(btnFont);

btnEqual.setCursor(new Cursor(Cursor.HAND_CURSOR));

btnEqual.addActionListener(event -> {

if (Pattern.matches("([-]?\\d+[.]\\d*)|(\\d+)", inText.getText()))

if (go) {

val = calc(val, inText.getText(), opt);

if (Pattern.matches("[-]?[\\d]+[.][0]*", String.valueOf(val))) {

inText.setText(String.valueOf((int) val));

} else {

inText.setText(String.valueOf(val));

opt = '=';

addWrite = false;

});

window.add(btnEqual);

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

window.setLayout(null);

window.setResizable(false);

window.setBackground(Color.blue);

window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // If Click into The Red


Button => End The Processus

window.setVisible(true);

private double calc(double x, String input, char opt) {

inText.setFont(inText.getFont().deriveFont(Font.BOLD));

double y = Double.parseDouble(input);

try

Registry reg=LocateRegistry.getRegistry("127.0.0.1",9999);

icalc ic = (icalc)reg.lookup("Server");

if (opt == '+') {

return ic.add(x, y);

} else if (opt == '-') {

return ic.sub(x, y);

} else if (opt == '*') {

return ic.mul(x, y);

} else if (opt == '/') {

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

return ic.div(x, y);

} else if (opt == '%') {

return ic.mod(x, y);

catch(Exception e)

System.out.println(e);

inText.setFont(inText.getFont().deriveFont(Font.PLAIN));

return y;

private void repaintFont() {

inText.setFont(inText.getFont().deriveFont(Font.PLAIN));

public static void main(String[] args) {

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

new client();

Output:

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

b. Retrieve Date and Time from Server

Code:

Idate.java

package rmidate;

import java.rmi.*;

/**

* @author Karan

*/

public interface idate extends Remote

public String date() throws RemoteException;

public String time() throws RemoteException;

Serveridt.java

package rmidate;

import java.rmi.*;

import java.rmi.server.*;

import java.util.Date;

import java.text.DateFormat;

import java.text.SimpleDateFormat;

/**

* @author Karan

*/

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

public class serveridt extends UnicastRemoteObject implements idate

DateFormat df;

serveridt() throws RemoteException

super();

@Override

public String date() throws RemoteException {

df = new SimpleDateFormat("dd/MM/yyyy");

return(df.format(new Date()));

@Override

public String time() throws RemoteException {

df = new SimpleDateFormat("HH:mm:ss");

return(df.format(new Date()));

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

Server.java

package rmidate;

import java.rmi.Naming;

import java.rmi.registry.LocateRegistry;

import java.rmi.registry.Registry;

/**

* @author Karan

*/

public class server {

public static void main(String ar[]){

try

Registry reg = LocateRegistry.createRegistry(9999);

serveridt d =new serveridt();

reg.rebind("Server",d);

System.out.println("Server is ready!");

catch (Exception e)

e.printStackTrace();

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

Client.java

package rmidate;

import java.rmi.*;

import java.rmi.registry.LocateRegistry;

import java.rmi.registry.Registry;

import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

/**

* @author Karan

*/

public class client implements ActionListener{

public static JFrame window;

public static JTextArea inText;

public static JButton btn_dt;

public static final int WINDOW_WIDTH = 400;

public static final int WINDOW_HEIGHT = 400;

public static final int BUTTON_WIDTH = 200; // Button width

public static final int BUTTON_HEIGHT = 70; // Button height

String date1,time1;

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

public static void main(String args[]){

client c = new client();

window = new JFrame("Date Time");

window.setSize(WINDOW_WIDTH, WINDOW_HEIGHT); // Set the width and the


Height of the window

window.setLocationRelativeTo(null); // Move Window To Center

Font btnFont = new Font("Comic Sans MS", Font.PLAIN, 28);

btn_dt = new JButton("Date/Time");

btn_dt.setBounds(100, 50, BUTTON_WIDTH, BUTTON_HEIGHT);

btn_dt.setFont(btnFont);

window.add(btn_dt);

inText = new JTextArea();

inText.setBounds(50,150 , 300, 100);

inText.setEditable(false);

inText.setBackground(Color.WHITE);

inText.setFont(new Font("Comic Sans MS", Font.PLAIN, 24));

window.add(inText);

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

btn_dt.addActionListener(c);

window.setLayout(null);

window.setResizable(false);

window.setBackground(Color.blue);

window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // If Click into The Red


Button => End The Processus

window.setVisible(true);

@Override

public void actionPerformed(ActionEvent e) {

try

Registry reg=LocateRegistry.getRegistry("127.0.0.1",9999);

idate idt =(idate)reg.lookup("Server");

date1 = idt.date();

time1 = idt.time();

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

inText.setText("Current Date: " +date1 + "\nCurrent Time: " +time1);

catch(Exception ex)

System.out.println(ex);

Output:

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

c. Temperature Conversion

Code:

Tempinterface.java

package rmitemp;

import java.rmi.*;

/**

* @author Karan

*/

public interface tempinterface extends Remote

public double cel_fh(double x) throws RemoteException;

public double fh_cel(double x) throws RemoteException;

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

public double cel_kel(double x) throws RemoteException;

public double kel_cel(double x) throws RemoteException;

public double kel_fh(double x) throws RemoteException;

public double fh_kel(double x) throws RemoteException;

Tempimpl.java

package rmitemp;

import java.rmi.*;

import java.rmi.server.UnicastRemoteObject;

/**

* @author Karan

*/

public class tempimpl extends UnicastRemoteObject implements tempinterface

tempimpl() throws RemoteException

super();

@Override

public double cel_fh(double x) throws RemoteException {

return ((x*9/5)+32);

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

@Override

public double fh_cel(double x) throws RemoteException {

return ((x-32)*5/9);

@Override

public double cel_kel(double x) throws RemoteException {

return (x+273.15);

@Override

public double kel_cel(double x) throws RemoteException {

return (x-273.15);

@Override

public double kel_fh(double x) throws RemoteException {

return ((x-32) * 5/9 + 273.15 );

@Override

public double fh_kel(double x) throws RemoteException {

return ((x-273.15) * 9/5 + 32 );

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

Server.java

package rmitemp;

import java.rmi.*;

import java.rmi.registry.LocateRegistry;

import java.rmi.registry.Registry;

/**

* @author Karan

*/

public class server {

public server() throws RemoteException

public static void main(String args[]) throws RemoteException{

try

Registry reg = LocateRegistry.createRegistry(9999);

tempimpl s = new tempimpl();

reg.rebind("Server",s);

System.out.println("Server is ready!");

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

catch(Exception e)

System.out.println(e);

Client.java

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package rmitemp;

import java.rmi.*;

import java.rmi.registry.LocateRegistry;

import java.rmi.registry.Registry;

import java.util.*;

import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.lang.Math;

/**

* @author Karan

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

*/

public class client implements ActionListener{

public static JFrame window;

public static JTextField input1,input2;

public static JButton btn_submit;

public static JComboBox combobox1,combobox2;

public static final int WINDOW_WIDTH = 600;

public static final int WINDOW_HEIGHT = 600;

public static final int BUTTON_WIDTH = 100; // Button width

public static final int BUTTON_HEIGHT = 30; // Button height

String str1,str2;

double num,result;

public static void main(String args[])

client c =new client();

window = new JFrame("Temperature Converter");

window.setSize(WINDOW_WIDTH, WINDOW_HEIGHT); // Set the width and the


Height of the window

window.setLocationRelativeTo(null); // Move Window To Center

Font btnFont = new Font("Comic Sans MS", Font.PLAIN, 20);

input1 = new JTextField();

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

input1.setBounds(50,100 , 150, 30);

input1.setEditable(true);

input1.setBackground(Color.WHITE);

input1.setFont(new Font("Comic Sans MS", Font.PLAIN, 24));

window.add(input1);

input2 = new JTextField();

input2.setBounds(50,250 , 150, 30);

input2.setEditable(true);

input2.setBackground(Color.WHITE);

input2.setFont(new Font("Comic Sans MS", Font.PLAIN, 24));

window.add(input2);

combobox1 = new JComboBox();

combobox1.setBounds(250,100 , 200, 30);

combobox1.setEditable(false);

combobox1.setBackground(Color.WHITE);

combobox1.setFont(new Font("Comic Sans MS", Font.PLAIN, 24));

combobox1.addItem("Celsius");

combobox1.addItem("Fahrenheit");

combobox1.addItem("Kelvin");

combobox1.setSelectedIndex(0);

window.add(combobox1);

combobox2 = new JComboBox();

combobox2.setBounds(250,250 , 200, 30);

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

combobox2.setEditable(false);

combobox2.setBackground(Color.WHITE);

combobox2.setFont(new Font("Comic Sans MS", Font.PLAIN, 24));

combobox2.addItem("Celsius");

combobox2.addItem("Fahrenheit");

combobox2.addItem("Kelvin");

combobox2.setSelectedIndex(1);

window.add(combobox2);

btn_submit = new JButton("Submit");

btn_submit.setBounds(200, 350, BUTTON_WIDTH, BUTTON_HEIGHT);

btn_submit.setFont(btnFont);

window.add(btn_submit);

btn_submit.addActionListener(c);

window.setLayout(null);

window.setResizable(false);

window.setBackground(Color.blue);

window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // If Click into The Red


Button => End The Processus

window.setVisible(true);

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

@Override

public void actionPerformed(ActionEvent e) {

try

Registry reg=LocateRegistry.getRegistry("127.0.0.1",9999);

tempinterface in =(tempinterface)reg.lookup("Server");

str1 = combobox1.getSelectedItem().toString();

str2 = combobox2.getSelectedItem().toString();

num = Double.parseDouble(input1.getText().toString());

if(str1.equals("Celsius") && str2.equals("Fahrenheit"))

result = in.cel_fh(num);

input2.setText(Math.round(result)+ "");

if(str1.equals("Fahrenheit") && str2.equals("Celsius"))

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

result = in.fh_cel(num);

input2.setText(Math.round(result)+ "");

if(str1.equals("Celsius") && str2.equals("Kelvin"))

result = in.cel_kel(num);

input2.setText(Math.round(result)+ "");

if(str1.equals("Kelvin") && str2.equals("Celsius"))

result = in.kel_cel(num);

input2.setText(Math.round(result)+ "");

if(str1.equals("Fahrenheit") && str2.equals("Kelvin"))

result = in.fh_kel(num);

input2.setText(Math.round(result)+ "");

if(str1.equals("Kelvin") && str2.equals("Fahrenheit"))

result = in.kel_fh(num);

input2.setText(Math.round(result)+ "");

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

catch(Exception ex)

System.out.println(ex);

Output:

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020

You might also like