|home| Next lesson 2/6

Remote Method Invocation (Level I)

Starting ...

This tutorial will assume that you have some experience with Java programming, as well as understanding basic syntax of interface, implementation class and method signatures. The code examples used in this tutorial can be compiled and executed directly in Windows system. If you use other operating system, the commands used to execute may be different.

This short lesson has the following sections.

  1. Design a Remote Interface
  2. Design a Remote Object
  3. Design a Server
  4. Design a Client
  5. Compilation and Execution

A scenario

Betty just got a job in a mortgage company. Her supervisor Joy asked her to make the mortgage calculator available to the public. That is to say the mortgage calculator will be put on an intranet or the internet. The mortgage calculator is an existing software and installed on an individual computer. If it is installed on the server, it will be easier to maintain and update. This is Betty's first assignment. She felt so excited. She decided to use Java Remote Method Invocation (RMI) technology. At first, she went to http://java.sun.com to see if the knowledge she learnt in college has been changed or not. Later, she found out that the jdk 1.5.0 new version has been released and the RMI technology has got new features.

What is an RMI application?

An RMI application is a distributed object application. It comprised of two separate programs: a server and a client. The runtime environment for the server and client is Java Virtual Machine(JVM).

She downloaded the jdk 1.5.0 version from http://java.sun.com/j2se/1.5.0/download.jsp site and the Remote Method Invocation (RMI) documentation from http://java.sun.com/j2se/1.5.0/docs/guide/rmi/index.html site.

She was ready to start now. She started to look at the Mortgage calculator class. The key method in the Mortgage class is the calculatePayment() method listed below:

 
public double calculatePayment(double principal, double annualRate, int years){
    double monthlyInterest = annualRate / 12;
    double monthlyPayment = (principal * monthlyInterest)
            / (1 - Math.pow(1/ (1 + monthlyInterest), years * 12));
    return monthlyPayment;
}

To calculate the monthly payment for a mortgage, you need three data: principal, annual rate and term. When a user provided these three data to an application, the monthly payment would be returned.

In order to try new RMI features, she quickly wrote a Mortgage program. She was going to use this command-line program to try the RMI features. The whole program is listed below:

 
//Mortgage.java

public class Mortgage {
    
    public static void main (String[] args){
        double payment, principal = 80000;
        double annualInterest = .065;
        int years = 15;
        
        if (args.length == 3) {
           try {
              principal = Double.parseDouble(args[0]);
              annualInterest = Double.parseDouble(args[1]);
              years = Integer.parseInt(args[2]);
           }
           catch (Exception e) {
              System.out.println("Wrong input " + e.getMessage() );
              System.exit(0);
           }
           print(principal, annualInterest, years);
        
        } else {
            System.out.println("Usage: java Mortgage principal annualInterest years ");
            System.out.println("\nFor example: java Mortgage 80000 .065 15 ");
            System.out.println("\nYou will get the output like the following: \n");
	    print(principal, annualInterest, years);
            System.exit(0);
        }
    }
    public static double calculatePayment(double principal, double annRate, int years){
        double monthlyInt = annRate / 12;
        double monthlyPayment = (principal * monthlyInt)
                    / (1 - Math.pow(1/ (1 + monthlyInt), years * 12));
        return format(monthlyPayment, 2);
    }
    public static double format(double amount, int places) {
        double temp = amount; 
        temp = temp * Math.pow(10, places);
        temp = Math.round(temp);
        temp = temp/Math.pow(10, places);
        return temp;
    }
    public static void print(double pr, double annRate, int years){
        double mpayment = calculatePayment(pr, annRate, years);
        System.out.println("The principal is $" + (int)pr);
        System.out.println("The annual interest rate is " + format(annRate * 100, 2) +"%");
        System.out.println("The term is " + years + " years");
        System.out.println("Your monthly payment is $" + mpayment);
    }
}

She saved the Mortgage.java in the C:\myrmi\ subdirectory, and executed it like the following:

 C:\   Command Prompt
 
C:\myrmi\javac Mortgage.java
C:\myrmi\java Mortgage
Usage: java Mortgage principal annualInterest years

For example: java Mortgage 80000 .065 15

You will get the output like the following:

The principal is $80000
The annual interest rate is 6.5%
The term is 15 years
Your monthly payment is $696.89

C:\myrmi>java Mortgage 150000 .060 15
The principal is $150000
The annual interest rate is 6.0%
The term is 15 years
Your monthly payment is $1265.79

C:\myrmi>

The program works well. Now it is ready to put Mortgage to work with RMI technology.

Check your skill

We strongly recommend you to try it out to get familiar with the functionality of mortgage calculation. It is a good way to test your system as well.

If you have a problem when you first install jdk1.5.0, please consult online installation information on SUN's site.

page 1  page 2  page 3  page 4  page 5  page 6  Next lesson 2/6