0% found this document useful (0 votes)
6 views3 pages

Week 6 Java Codes

The document contains a Java implementation of a remote calculator service using RMI (Remote Method Invocation). It defines a Calculator interface with methods for basic arithmetic operations, implements the interface in the CalculatorImpl class, and sets up a server and client for interaction. The client allows users to perform calculations by selecting operations and inputting numbers, while the server processes these requests remotely.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Week 6 Java Codes

The document contains a Java implementation of a remote calculator service using RMI (Remote Method Invocation). It defines a Calculator interface with methods for basic arithmetic operations, implements the interface in the CalculatorImpl class, and sets up a server and client for interaction. The client allows users to perform calculations by selecting operations and inputting numbers, while the server processes these requests remotely.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

import java.rmi.

Remote;
import java.rmi.RemoteException;

public interface Calculator extends Remote {


double add(double a, double b) throws RemoteException;
double subtract(double a, double b) throws RemoteException;
double multiply(double a, double b) throws RemoteException;
double divide(double a, double b) throws RemoteException;
}

import java.rmi.server.UnicastRemoteObject;
import java.rmi.RemoteException;

public class CalculatorImpl extends UnicastRemoteObject implements


Calculator {

protected 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 {
if (b == 0) throw new ArithmeticException("Division by zero is not
allowed.");
return a / b;
}
}
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class CalculatorServer {


public static void main(String[] args) {
try {
Calculator calculator = new CalculatorImpl();
Registry registry = LocateRegistry.createRegistry(1099);
registry.rebind("Calculatorservice", calculator);
System.out.println("Calculator Server is running ... ");
} catch (Exception e) {
e.printStackTrace();

}
}
}

import java.rmi.registry.LocateRegistry;

import java.rmi.registry.Registry;

import java.util.Scanner;

public class CalculatorClient {

public static void main(String[] args) {

try {

Registry registry = LocateRegistry.getRegistry("localhost", 1099);

Calculator calculator = (Calculator) registry.lookup("CalculatorService");

Scanner scanner = new Scanner(System.in);

while (true) {

System.out.println("\nSelect operation: 1. Add 2. Subtract 3. Multiply 4.Divide


5.Exit");

int choice = scanner.nextInt();

if (choice == 5) break;

System.out.print("Enter first number: ");

double a = scanner.nextDouble();

System.out.print("Enter second number: ");

double b = scanner.nextDouble();

double result = 0;
switch (choice) {

case 1: result = calculator.add(a, b); break;

case 2: result = calculator.subtract(a, b); break;

case 3: result = calculator.multiply(a, b); break;

case 4:

try {

result = calculator.divide(a, b);

} catch (ArithmeticException e) {

System.out.println("Error: " + e.getMessage());

continue;

break;

default: System.out.println("Invalid choice."); continue;

System.out.println("Result: "+ result);

scanner.close();

} catch (Exception e) {

e.printStackTrace();

You might also like