0% found this document useful (0 votes)
23 views6 pages

Experiment 6

The document outlines the creation of a basic calculator using Java RMI, consisting of a remote interface, server implementation, and client program. The server performs arithmetic operations such as addition, subtraction, multiplication, and division, while the client interacts with the user to execute these operations. Instructions for compiling and running the program are also provided.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views6 pages

Experiment 6

The document outlines the creation of a basic calculator using Java RMI, consisting of a remote interface, server implementation, and client program. The server performs arithmetic operations such as addition, subtraction, multiplication, and division, while the client interacts with the user to execute these operations. Instructions for compiling and running the program are also provided.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

EXPERIMENT-6

AIM: To create a basic calculator using Java RMI (Remote Method Invocation). This
program includes the client and server, where the server performs the arithmetic operations
and the client interacts with the user.

1. Create the Remote Interface

The remote interface defines the methods that the client can invoke remotely.

import java.rmi.Remote;
import java.rmi. RemoteException;

public interface Calculator extends Remote {


// Method to add two numbers
public double add (double a, double b) throws RemoteException;

// Method to subtract two numbers


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

// Method to multiply two numbers


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

// Method to divide two numbers


public double divide(double a, double b) throws RemoteException;
}

2. Create the Remote Object (Server)

1
G.Devans,23HQ1A4614
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class CalculatorImpl extends UnicastRemoteObject implements Calculator {

public CalculatorImpl() throws RemoteException {


super();
}

@Override
public double add(double a, double b) throws RemoteException {
return a + b;
}

@Override
public double subtract(double a, double b) throws RemoteException {
return a - b;
}

@Override
public double multiply(double a, double b) throws RemoteException {
return a * b;
}

@Override
public double divide(double a, double b) throws RemoteException {

2
G.Devans,23HQ1A4614
if (b == 0) {
throw new ArithmeticException("Division by zero is not allowed.");
}
return a / b;
}
}

3. Create the Server Program

import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;
public class CalculatorServer {
public static void main(String[] args) {
try {
// Start the RMI registry
LocateRegistry.createRegistry(1099);

// Create an instance of CalculatorImpl


CalculatorImpl calculator = new CalculatorImpl();

// Bind the calculator object to the RMI registry


Naming.rebind("CalculatorService", calculator);

System.out.println("Calculator Server is running...");


} catch (Exception e) {
e.printStackTrace();
}
3
G.Devans,23HQ1A4614
}
}

4. Create the Client Program

import java.rmi.Naming;
import java.util.Scanner;
public class CalculatorClient {
public static void main(String[] args) {
try {
// Look up the CalculatorService from the RMI registry
Calculator calculator = (Calculator)
Naming.lookup("rmi://localhost/CalculatorService");

Scanner scanner = new Scanner(System.in);


int choice;
do {
System.out.println("\nBasic Calculator Menu:");
System.out.println("1. Add");
System.out.println("2. Subtract");
System.out.println("3. Multiply");
System.out.println("4. Divide");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();

if (choice != 5) {

4
G.Devans,23HQ1A4614
System.out.print("Enter first number: ");
double num1 = scanner.nextDouble();
System.out.print("Enter second number: ");
double num2 = scanner.nextDouble();

switch (choice) {
case 1:
System.out.println("Result: " + calculator.add(num1, num2));
break;
case 2:
System.out.println("Result: " + calculator.subtract(num1, num2));
break;
case 3:
System.out.println("Result: " + calculator.multiply(num1, num2));
break;
case 4:
try {
System.out.println("Result: " + calculator.divide(num1, num2));
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
break;
default:
System.out.println("Invalid choice, please try again.");
}
}

5
G.Devans,23HQ1A4614
} while (choice != 5);

scanner.close();
System.out.println("Exiting the calculator...");
} catch (Exception e) {
e.printStackTrace();
}
}
}

Steps to Run the Program:

1. Compile the Java files:


o javac Calculator.java
o javac CalculatorImpl.java
o javac CalculatorServer.java
o javac CalculatorClient.java
o
2. Sample Output:
3. rmiregistry
4. java CalculatorServer
5. java CalculatorClient

6. Calculator Server is running...

7. Basic Calculator Menu:


8. 1. Add
9. 2. Subtract
10. 3. Multiply
11. 4. Divide
12. 5. Exit
13. Enter your choice: 1
14. Enter first number: 10
15. Enter second number: 5
16. Result: 15.0

6
G.Devans,23HQ1A4614

You might also like