Posts Tagged ‘java’

Matching or stable marriage problem

April 28, 2016

Stable marriage problem or stable matching problem is a popular algorithm to match two set of entities where both have preferences over the other set. Elements of a set give ratings or state their choices from the other set. The problem is to find better match (if not best). Here is a java implementation of the algorithm (in a modified form, where one set prioritizes elements from another set, but another set does not have any say on how they get prioritized).

(more…)

Write a simple jini application

November 20, 2014

Introduction

In this tutorial, I plan to show how to write a very simple jini application. This tutorial uses code from Chapter 9 of Jan Newmarch‘s website. For this tutorial you will create 4 files. You can download the source code from this link. If you want a more comprehensive example, you can view the source code for this project (medical_report_system-final).

  1. HelloIntf.java is an interface that has two very simple methods (to send hello message and to receive hello message).
  2. HelloImpl.java implements these methods, and are hosted on server side. Client interact with this class through the HelloIntf interface.
  3. HelloServer is our service provider. It registers itself with the lookup service, and sends the object of the interface to this lookup service, when it is requested (In other words Server knows about HelloImpl).
  4. HelloClient is our client. It uses the HelloIntf  interface methods to send and receive messages from the service provider (our server) using the lookup service. It knows nothing about the implementation. Client however needs a proxy object (which in our case is just a jar created using HelloIntf.java interface). This proxy object is provided during our implementation through a webserver which we host using a tool that comes with jini.

Whenever we transfer message using RMI it has to implement java.io.Serializable interface. Also the HelloIntf.java methods throw RemoteException. Therefore, during implementation in both HelloServer and HelloClient we need to catch RemoteException while implementing or calling these methods.

(more…)

C to Java

November 17, 2011

Well it is not an advanced topic by any means, just a place to recall some jargons of these languages (C, C++ and Java). This might be part one of two posts in a series. Some of the topics I haven’t covered are related to Generics, String, Networking, I/O (Even Google’s I/O), Concurrency etc. This post is a starter, yet complete on its own respect.

1. In C starting point is off course pointers.

What is a pointer?
A pointer is a variable that can hold the address of a variable. Here address means location in memory where the object or value of a particular variable is stored. We can get address of any variable with “&”.

E.g
int x = 5;
int *ptr = &x;

/*
int *ptr;
ptr = &x;
*/

int y = *(&x);
y = *ptr;

Lets look at above example. First we create a variable called x and gave it a value of 5. The variable can be located anywhere in the memory, the location which we don’t know. Next we create a pointer to integer called *ptr and put the address of variable x into it. Thus ptr is a pointer (a variable that can store an address). If this line makes no sense next line dissects it into two steps. First we create a pointer to integer with int *ptr and then put address of x (which we get using &x) and put it in ptr pointer. Next we create another variable y and dereference (return object or value) that is at address of x using the dereferencing operator * (don’t confuse this with our earlier pointer declaration as in int *ptr, both are different).

A pointer is thus a variable which is always the size of the underlying architecture (it does not depend upon the type of variable e.g int, char or double, because address size of first block to any of these types is same), though pointers are aware of the type they are pointing. They must know how much bytes they must occupy according to the type. E.g if integer takes 4 bytes (32-bit) and char takes 1 byte (8-bit) then if ptr is pointer to integer *(ptr + 1) will give value of next pointer location which could be four bytes ahead of current value and if ptr is pointer to  char, then it might need to go only one byte ahead to get value of next consecutive memory location.
(more…)