Friday, December 25, 2009

Open Source in Developing Countries - I

The "Free Software Movement" that Richard Stallman started in 1983 was brought a big leap forward by "Linus Torvalds" in 1991 with the invention of Linux kernal. Other open source evangelists like Eric S. Raymond and Bruce Perens have major contributions to the open source movement. Have a look at the history of free and open source software in wikipedia. (There are philosophical differences between Free Software and Open Source Software, discussion of which is not the intention of this writing.)

Open source has emerged as viable alternative to the expensive proprietary software. From operating system to user application, from eye candy multimedia applications to serious research projects, from embedded system to large server, from desktop to web application there exist suitable software applications to serve the purposes. In the contemporary world, corporations are constantly fighting for lowering operating costs. Many have already opted for Open Source software stack.

Software is a tangible staff. It is easy to copy and run in other machines. Most developing countries don't have any strict laws to protect the intellectual property (IP). Even if there exist some laws, they are very rarely enforced. Many societies also don't consider it bad to pirate (aka steal) software and don't discourage this practice.

But the world is changing. Many developing countries are immensely contributing  to the advancement of the IT industry. The software development was once mostly centered in the US. But the landscape is rapidly changing. India and China have emerged as software development power houses. Other developing countries including Bangladesh are also continuously progressing in this area. These countries produce world class software engineers who are frequently creating sophisticated software applications. Companies are spending money to build software and they will definitely want return from their investment. If their products are pirated, they can't be happy. Therefore, pressure is mounting to derive laws for protecting the intellectual properties of their own software in many countries.

Globalization has also huge impact on pursuing laws to protect IP. Under the hood of Globalization, the dispersed national economies worldwide are integrated into a interdependent global economy. The multinational corporations are very much concerned over how their IPs are protected in those countries where no IP protection laws exist. These companies often have immense power on the political system of many developing countries. They together with their respective home countries will influence or pressurize to derive new laws for IP protection.

So the days are not very far when most countries on the Earth will have more or less homogeneous IP protection laws. The citizen would be obliged to pay for using each commercial software. This will obviously be a huge burden for the society which is accustomed to pirate software. The moral setup will be more difficult. If someone is comfortable with software piracy, how to resist himself from it.

It is now necessary to realize that there are potential alternatives to usual proprietary software. There are replaceable open source software for almost every commercial applications. In the next post, I will explain how to change the mind set and get accustomed to open source software.


Thursday, December 24, 2009

3D effects in Avatar are mind-blowing

Just watched James Cameron's epic film "Avatar". The 3-D visualization effects are mind-blowing. Otherwise, the story is not much different from usual Hollywood movies. A brilliant guy (mostly a white man) from so called civilized world arrives in an unfamiliar and uncivilized world (in this case in a moon called Pandora), learns all tricks to survive in this very hostile society in few days and becomes its leader, and finally, leads the society into victory from the aggression of a civilized society (in this case, which he is originated from).

Anyway, the film is worth watching, if possible in 3-D capable theater. James Cameron has produced yet another sci-fi masterpiece.

Wednesday, December 23, 2009

Sun Microsystems: Java EE 6 and GlassFish Enterprise Server v3 Virtual Conference

A comprehensive set of Webinars from the virtual conference held on December 15 is made available. Great source if you want to start learning Java EE 6 and Glassfish v3, first reference implementation of Java EE 6 standards.

Sun Microsystems: Java EE 6 and GlassFish Enterprise Server v3 Virtual Conference

Monday, December 21, 2009

Value Type vs. Reference Type + Boxing vs. Unboxing


This post is part of my ongoing effort to familiarize the readers with the commonalities & variabilities of C++/Java/C# programming languages. A tentative outline of this series can be found at Contents.

I was initially thinking to discuss the compound types today, but have decided to briefly explain two important concepts that will be helpful to understand the building blocks and inner workings of compound types. Let's start with the first concept - value type vs. reference type.

Value Type: Variable of value type directly contains value. If you assign a variable of a value type to another, the data will be copied to the target variable. If you pass a variable by value to a method, changes made to the argument will not be changed since a copy of it will be passed to the function. All basic types except string (but it's also copied) discussed earlier are value types.

int a = 10;
int
 b = 20;

b = a; // value of a copied to b
b++; //increment b by 1

printf(" a = %d,\n b = %d", a,b);

Output:

a = 10;
b = 11;

Reference Type: Objects of this type hold a marker to the real data values. So assigning a variable of this type, points to the same data value. This type is more common in object oriented programming.

class A
{

private
:
    int
 m_value = 10;

public
:
   /* Return the value */

   int
 GetValue()
   {

      return
 m_value;
   }

   /* Set a new value */

   void
 SetValue(int value)
   {

      m_value = value;
   }
}


A* a = new A();
A* b = a; // b points to a

b->SetValue(11);

printf(" a = %d\n b = %d", a->GetValue(), b->GetValue);

delete
 a; // delete object, only once

Output:

a = 11;
b = 11;


Value types are stored on the stack and Reference types are stored on the managed heap.

Another important concept very important in managed environment like Java and C# is Boxing and Unboxing. Each value type in Java and C# has a corresponding reference type. Boxing/Unboxing deals with the conversion of a value type to its corresponding reference type and vice versa.

Boxing:
Boxing is the process of converting a value type (e.g. primitive type) to a reference type. Java has reference types for its primitive value types. Conversion from value type of the corresponding reference type is automatic. C# primitive types are converted to the corresponding .Net types as listed in Basic/Primitive Type post.

Unboxing:Unboxing is, as expected, opposite to boxing. It converts a reference type to the corresponding value type. In both C# and Java, the conversion is implicit, therefore, doesn't need explicit casting.

Java
/* Boxing Example */

int
 i = 10;

Object obj = (Object) i; //explicit boxing

Integer intObj = (Integer) i; // explicit boxing

Object obj2 = i; //implicit boxing

Integer intObj2 = i; //implicit boxing


/* Unboxing Example */


int
 i = 10;
Object obj = i; //boxing

/* Following will not compile. 
No explicit conversion from object to int 
*/
/*int j = (int) obj;*/


Integer intObj2 = i; //boxing
j = intObj2.intValue(); //unboxing
j = intObj2; //auto unboxing
C#
/* Boxing Example */

int
 i = 10;

object
 obj = (object) i; //explicit boxing

Int32 intObj = (Int32) i; // explicit boxing

object
 obj2 = i; //implicit boxing

Int32 intObj2 = i; //implicit boxing



/* Unboxing Example */


int
 i = 10;
object
 obj = i; //boxing
int j = (int) obj; //unboxing

Int32 intObj2 = i; //boxing
j = (int) intObj2; //unboxing


References: The Frequently Asked Questions (faq) series on various programming languages are great resources to deepen knowledge in the respective languages.

Programming Language FAQC++ FAQs (2nd Edition)The Java Faq (Java Series)

Friday, December 18, 2009

Operators


This post is part of my ongoing effort to familiarize the readers with the commonalities & variabilities of C++/Java/C# programming languages. A tentative outline of this series can be found at Contents.

An Operator is a syntactic element which specifies that certain arithmetic or logical manipulation must be performed on some data. C++/Java/C# all support the basic operators of C.

Following are some lists of the operators in these languages grouped by their types. The empty cell means the operator is not directly supported by some particular language and the name in bracket besides it shows what can be used as a replacement.

Arithmetic Operators

C++
Java
C#
Meaning
Example
++
++
++
increment by 1
i++; or ++i;
--
--
--
decrement by 1
i--; or --i;
+
+
+
addition
z = x +y;
-
-
-
subtraction
z=x-y
-
-
-
negation
-x or -5
*
*
*
multiplication
z = x*y
/
/
/
division
z = x/y
%
%
%
modulus (remainder)
z = x % y
Conditional Operators

C++
Java
C#
Meaning
Example
&&
&&
&&
conditional AND
if (x && y)
||
||
||
conditional OR
if (x || y)
!
!
!
conditional NOT
 if (!x)
Logical (bitwise) Operators

C++
Java
C#
Meaning
Example
&
&
&
logical AND
z = (x & y);
| |
|
|
logical OR
z = (x | y);
^
^
^
logical XOR
 z = (x ^ y);
~
~
~
logical complement
 z = ~x;
Relational (conditional) Operators

C++
Java
C#
Meaning
Example
>
>
>
greater than
if (x > y);
>=
>=
>=
greater than or equal to
if (x >= y);
<
<
<
less than
if (x < y);
<=
<=
<=
less than or equal to
if (x > y);
==
==
==
equal to
if (x == y);
!=
!=
!=
not equal to
if (x != y);

/*Add two integers and return the result*/
int
 doSum(int x, int y)
{

    int
 x = 10;
    int
 y = 12;
  
    x++; // increment x by 1
    y--; // decrement y by 1

    return
 x + y; // return 22
}

References:

Thursday, December 17, 2009

Bangladeshi's in Rome

Our trip to Rome this week was a big surprise for my 4-year old daughter. On the time of arrival, the first conversation that took place to ask the way to taxi stand, was in Bangla. She was surprised and continuously declaring that "the uncle can speak Bangla".

Apart from the richness of Vatican City and ruins of Roman Dynasty, I was amazed by the presence of immense number of Bangladeshis in Rome. They were scattered around everywhere. We lived on Bangladeshi food during our stay there.

Thursday, December 10, 2009

"Papa, I don't want to grow up. I want to hold your hand and walk forever".
My 4-year old daughter

10 Minute Guide

Software technology is rapidly changing. New technologies are arriving every now-and-then. Getting up-to-date with all the new innovations is very challenging. There are things which you can very quickly start with. My series of posts entitled "10 Minute Guide..." will present some of these new techniques.
  1. 10 Minute Guide to Chromimum OS Installation
  2. 10 Minute Guide to Google Maps
  3. 10 Minute Guide to DirectX 11 Programming in C++

Wednesday, December 09, 2009

Just Installed Google Chrome Browser Beta for Linux

The beta versions for google chrome browser for Linux and Mac OS are out. I have just downloaded the debian/ubuntu package from chrome site. Switch to the download directory and run the following command from console:

$sudo dpkg -i google-chrome-beta_current_i386.deb

Run google chrome

$google-chrome

It is super fast!!!

10 Minute Guide to Google Maps

Map-based applications are becoming parts of our digital life. Visualizations of travel data, current news events, graphical presentation of weather reports, historical sites, concert locations, public transportation, corporate offices, and real estate sites through maps are very frequently used applications now-a-days. It is extremely simple to add map support to your web site. I have compiled here a 10 min guide for a map with a simple balloon marker on the location based on your IP.

Prerequisites: You need very basic knowledge of the following:
  1. HTML
  2. JavaScript

Step 1. Create Page: Create a new HTML page in your favorite text editor or HTML page. The page will roughly contain the following text (You can better copy these lines to a text file and save it with .htm or .html extension):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>My Location</title>
</head>
<body>

</body>
</html>

2 min.
Step 2. Add Canvas:  Add a <div> tag inside the <body> where the map will be displayed and give it an id (e.g. canvas in the following example) 

<body>
<div id="canvas" style="height: 400px; width: 400px;"></div>
</body>

2 min.
Step3. Reference maps API: Add the following <script> tag just under <title> tag. This line will set a reference to JavaScript API libraries provided by google.

<script type="text/javascript" src="http://www.google.com/jsapi"></script>
1 min.

Step4. Create Map: Load API library and initialize the map in a <script> tag inside <head> tag (or copy & paste the following lines of code).

<script type="text/javascript">

function initialize() {
    /* give a default zoom level*/
    var zoom = 8;
    /*default center of the map*/
    var latlng = new google.maps.LatLng(49.008054, 8.388969);

   /* get the your location with geocoding */
   var location;
    if (google.loader.ClientLocation) {
      latlng = new google.maps.LatLng(google.loader.ClientLocation.latitude, google.loader.ClientLocation.longitude);
     /* Build a text of the location to display */
     location = "Latitude: " + google.loader.ClientLocation.latitude + "<br/>";
     location += "Longitude: " + google.loader.ClientLocation.longitude + "<br/>";
     location +=  "City: " + google.loader.ClientLocation.address.city + "<br/>";
     if (google.loader.ClientLocation.address.region)
         location += "Region: " + google.loader.ClientLocation.address.region + "<br/>";
     location += "Country: " + google.loader.ClientLocation.address.country;
    }

    /* Options for the map*/
    var myOptions = {
      zoom: zoom,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }

    /* Create the map on the canvas */
    var map = new google.maps.Map(document.getElementById("canvas"), myOptions);

   /* Create an info window to display the location text */
   var infowindow = new google.maps.InfoWindow({
        content: location
    });

   /* Create a default balloon marker at your location */
   var marker = new google.maps.Marker({
        position: latlng,
        map: map,
        title:"My Location"
    });
 
    /* Display the location text when marker is clicked */
google.maps.event.addListener(marker, 'click', function() {
      infowindow.open(map,marker);
    });
  }
</script>
5 min.

Browse and download the complete HTML page from here. Click the balloon on the following map to see your current location.






There are a lot of tutorials, samples and demos available at the Google Maps API web site.

Monday, December 07, 2009

Basic/Primitive Types


This post is part of my ongoing effort to familiarize the readers with the commonalities & variabilities of C++/Java/C# programming languages. A tentative outline of this series can be found at Contents.

In a previous post Identifiers, I have discussed identifiers that we can write in the source. Type restricts the identifier to size & kind of data and in addition, the operations that can be performed on that data. Both Java and C# support almost all basic data types of C++ but change characteristics of some. C++/Java/C# are strongly typed languages meaning each object must have a predefined data type.

Type Modifier:
C++ has 7 basic data types. They are int, float, double, bool, char, wchar_t, void. It uses four type modifiers to restrict the signedness and size of data that can be stored on a data type identifier -
  1. signed: Defines a positive (+ve) or negative (-ve) type. All types are implicitly positive.
  2. unsigned: A positive (+ve) type. Variable of this type can't hold negative values.
  3. short: Types are implicitly short if not otherwise specified.
  4. long: Extends the size of basic type to holder larger value.
.Net programs are compiled into Microsoft Intermediate Language (MSIL). Similar to Java Byte Code, it is  independent of hardware architecture and is translated to machine code during or before execution. But unlike Java, it brings a common type system (CTS) which means the actual program can be written in arbitrary language which can compile for .Net and the respective compiler is responsible for translating it to MSIL. MSIL defines a set of data types and each .Net compatible compiler must translate its own data types to the corresponding data types in MSIL. In other words, each data type in C# resembles a data type in MSIL.

In Java and C#, the primitive types are value types (more in Value Type vs. Reference Type and Boxing vs. Unboxing) which are allocated in the stack and copied during assignment. There are corresponding reference types in both the languages with implicit conversion from each other. The boxed types of Java are listed in "Java Boxed" and that of C# are in ".Net Type" columns.

Following is a list of the primitive data types in these languages. The sign "-" means the type is not directly supported by some particular language and the type in bracket beside it shows what can be used as a replacement.


C++
Java
Java Boxed
C#
.Net
- (unsigned char)
- (byte)
- (Byte)
byte (8)
Byte
- (signed char)
byte
Byte
sbyte (8)
SByte
int
int(32)
Integer
int (32)
Int32
unsigned int
-
-
uint (32)
UInt32
short
(short int)
short
Short
short (16)
Int16
unsigned short
-
-
ushort (16)
UInt16
long (long int)
long (64)
Long
long (64)
Int64
unsigned long
-
-
ulong (64)
UInt64
float
float
Float
float (32)
Single
double
double
Double
double (64)
Double
wchar_t
char
Character
char (16)
Char
bool
boolean
Boolean
bool (8)
Boolean
- (void*)
Object
Object
object
Object
-
-
decimal (128)
Decimal
- (std::wstring)
java.lang.String
java.lang.String
string
String

Type Qualifiers

Further keywords are used to guarantee data safety or specify data vulnerability. const

C++
Java
C#
.Net
const
final
const
-
-
-
readonly
-
volatile
-
volatile
-

Storage Class Specifiers
Further keywords are used to guarantee data safety or to specify data vulnerability.

C++
Java
C#
.Net
auto
-
-
-
register
-
-
-
static
-
-
-
extern
-
extern
-

Variables

A variable has a name and we keep some information in it. The information can be altered afterward, hence the name variable. C++/Java/C# are strongly typed languages which means each variable has a defined type i.e. you cannot assign value of a type to a variable of another type.

int textLength = 0; //defines a variable of integer type
float average = 1.0; // defines a variable of float type 

Function (aka method/routine/subroutine/operation/procedure) Names

A function or method defines a particular responsibility of the program. It has a return type and a set of parameters.

/*Add two integers and return the result*/
int
 doSum(int x, int y)
{

    return
 x + y;
}


Sunday, December 06, 2009

10 Minute Guide to Chromium OS Installation

A recent hype in the technology media is "Google Chrome OS", a new Internet based operating system by Google for Netbooks. This is a significant ingredient to Google's push for cloud computing. The search giant has already partnered with several major Netbook vendors for system equipped with this OS as early as last quarter of 2010.

I have recently tried the OS and wanted to share my experience with the readers. This post will cover a 10-minute guide for the installation of the OS and get it running in your Windows or Linux OS. Let's start with our journey-

Step 1: You need a virtual machine to run Chrome OS. For Windows systems, download free VMware player or Virtual Box. I prefer Virtual Box from Sun because it doesn't need registration and is very simple to use. The rest of the post assumes that you have also preferred virtual box over VMware Player.

For Linux, download Virtual Box from http://www.virtualbox.org/wiki/Linux_Downloads. In my Kubuntu system, as always, I needed only the apt-get command:

$: sudo apt-get install virtualbox-ose-qt
It comes under KDE Menu: Applications -> Utilities -> PC Virtualization Solutions
3 min.
Step 2: Download Chrome OS image from pirates bay torrent. If you have no torrent client installed in your system, download image from gdgt.
4 min.
Add Hard disk to Virutal Box:
  1. Start the virtual box. Click New to add new virtual machine.
  2. Give it a name. Select the OS type as Linux and version as Ubuntu.
  3. Leave other options as default. 





Now start your Chrome OS-


Login with your google account-



3 min.

Enjoy your chrome OS!!!

Sunday, November 29, 2009

Structure of an Object Oriented Program


This post is part of my ongoing effort to familiarize the readers with the commonalities & variabilities of C++/Java/C# programming languages. A tentative outline of this series can be found at Contents.

In the last post of this series, we have seen how the structure of a very simple program in C++/Java/C# looks like. The C++ program was written in procedural manner, since at least the entry point function main must be global (not member of any class). Java is a near pure OO language (apart from the non-object primitive datatypes like int, long, float). So every method must be part of a class. Many argue that C# is a pure OO language since its primitive types are implicitly derived from System.ValueType which is, in turn, derived from System.Object. Anyway, like Java all data members and methods in C# belong to some class. In today's post, we will see the various elements of an OO program, their commonalities and variabilites.

Namespace/Package: Sets context for an item

A namespace groups a set of related elements and guarantees identity of their names. It draws a conceptual boundary around the items which distinguishes them from elements with the same name in other namespace. It is not mandatory to use namespace, but absence of it can confuse you even for moderate size programs. If you are a library programmer, you should use namespaces, otherwise user of the library can easily be confused due to frequent compiler errors caused by duplicate names. The namespace is hierarchical in nature. You can have a namespace containing another namespace.

Let's take a code snippet as an example. The following C++ code will not compile due to duplicate identifiers.

int doSum(int x, int y);
int doSum(int x, int y);


If you really need the same identifier name, you can distribute them in different namespaces.

namespace Aggregate
{

    int
 doSum(int x, int y);
}


namespace Separate
{

    int
 doSum(int x, int y);
}

Java has implemented namespace concept bit differently than C++/C#. Java uses package to organize the classes of the program and defines the hierarchy how the class files will be deployed in the file system of target machine. Package inherently defines the namespace. A package name java.lang has the following meanning:
  1. java.lang must be the first executable line in the source file
  2. java.lang is a namespace
  3. java is a namespace
  4. There exists a directory named java (may be only virtual if e.g. inside a .jar file)
  5. There is a subdirectory named lang inside java (java\lang)

Using Namespace/Package: The namespace (in C++/C#) or package (in Java) must be imported before they can be used in the source. The using namespace keyword serves the purpose in C++ and C# while in Java import keyword must be used. You can also obviously use the fully qualified name without using the keyword in all these languages.
C++
Definition:
namespace HexEditorApp
{

  public
:
      int
 count = 10;
      int
 doSum(int x, int y)
      {

        return
 x + y;
      }
}

Usage:
using namespace HexEditorApp;

public class
 HexConverter 
{
 
public
: 
    void
 Convert() 
    {
 
        int
 countElement = HexEditorApp::count; // fully qualified name 
        int sum = doSum(10, 20); // call directly, as the method is already imported 
    } 
}

Java
Definition:
package HexEditorApp; // it must be the first executable line of the file 

/* In java (as well as C#), you can't declare a variable or method outside a User Defined Type (UDT) definition e.g. class, enum, struct (for C#)*/


public class
 HexEditor {
  public
 int count = 10;
  public
 int doSum(int x, int y){
     return
 x + y;
  }
}

Usage:
import HexEditorApp; // import keyword for type inclusion

public class
 HexConverter {
  public
 void Convert() {
     int
 countElement = HexEditorApp::count; // fully qualified name
     int sum = doSum(10, 20); // call directly, as the method is already imported
   }
}

C#
Definition:
namespace HexEditorApp
{


/* In java (as well as C#), you can't declare a variable or method outside // a User Defined Type (UDT) definition e.g. class, enum, struct (for C#)*/


    class
 HexEditor
    {

        public
 int count = 10;
        public
 int doSum(int x, int y)
        {

            return
 x + y;
        }
    }
}

Usage:
using namespace HexEditorApp;

public class
 HexConverter
{

    public
 void Convert()
    {

        int
 countElement = HexEditorApp::count; // fully qualified name
        int sum = doSum(10, 20); // call directly, as the method is already imported
    }
}

Definition vs. Declaration: Make identifier known to compiler w/o values
We declare an identifier to make it known to the compiler so that we can use it afterwards. But this is only the half of the story. With definition, we give a concrete meaning to this identifier.

// declaration of variable
int count;
// definition of the variable
count = 0;
//declaration + definition of variable
int count = 0;

//declaration of a method
int doSum(int x, int y);

//definition of a method
int doSum(int x, int y)
{

     return
 x+y;
}

Both Java and C# assign default value to any primitive type declaration i.e. these identifiers are defined in place of declarations (more on this in the next post). These languages also differ from C++ on how they define methods. In C++ it is possible to declare the methods at one place and define them at some other place. But in Java and C#, you must define the method at the place of declaration.

Saturday, November 21, 2009

Running Notepad++ on Linux

I use Windows and Linux interchangeably. Windows 7 is currently installed on my office computer and Kubuntu 9.10 (codenamed karmic) on the laptop. I need very often similar applications on both platforms. For simple .Net development MonoDevelop is my choice which is cross-platform so doesn't need any special treatments. Eclipse IDE serves me the same for the Java development. For my blog writing, specially for C++/Java/C# posts, I need to write C++/Java/C# code very frequently and use Notepad++ in Windows. It helps me to write source code in the desired language and export to basic HTML with simple embedded style.

I needed a similar editor on Linux. I could have used cross-platform ports of emacs or vi for the same purpose. Being a long time Notepad++ user for HTML and blogging, I don't want to use emacs or vi for blog posts. I have briefly explained here the steps to install Notepad++ in the Kubuntu system over Wine.


1. Install Wine if it's not installed yet
      $sudo apt-get install wine
2. Download Notepad++ windows executable from its download site.
3. Open the console and change to the download location.
       $cd

4. Run the installer with wine
      $wine npp.5.5.1.Installer.exe

Now I can use notepad++ from windows as well as from kubuntu.

Friday, November 20, 2009

Identifiers


This post is part of my ongoing effort to familiarize the readers with the commonalities & variabilities of C++/Java/C# programming languages. A tentative outline of this series can be found at Contents.

I want to start today's post with a good news. Microsoft has announced to open source .Net Micro Framework under the Apache 2.0 license. It is a big news for Open Source enthusiasts. MS has come forward a step closer to open source the complete .Net Framework in the future. Does it mean the beginning of the end of Mono? I believe Mono is more than .Net framework. Our cross-platform hexeditor will conver both .Net and Mono platform, so it is a good experiment to distinguish the commonalities and variabilities of these two platforms.

Today's post will be short. I will explain the most important constituents of programming syntax - the identifiers. We need words in natural language to communicate with each other. Similarly, in programming languages Identifiers are used to communicate with the system. The compiler gives us a set of names called Keywords which have predefined meanings. In contrast, identifiers are the names we supply to our programs. The identifiers and keywords form statements that define the syntax of the program similar to sentences of natural languages. The identifiers must be different from the keywords. So it is important that you have a fair amount of knowledge on the keywords in your programming language.

There are various types of identifiers we use for naming the entities of the programs - variables, function names, constants, user-defined types, labels etc. Most programmers follow some convention for naming them. There are some well accepted naming conventions in different languages for identifiers e.g. - Pascal Case, Camel Case, Hungarian Notation. There are coding guidelines from Microsoft for C# and from Sun for Java. Many programmers who code both in Java and C++, uses the Java conventions. If I can hold my energy, I will write a complete post on various coding conventions.

Identifier naming rules common to C++/Java/C#:
  1. case sensitive
  2. only characters(A-Z, a-z), digits(0-9), underscore(_) can be used
  3. can't start with a digit
  4. no space is allowed inside the identifier name
Examples:
   doSum, _tryit, transform3D
   do Sum (space inside), /tryit (invalid char), 3DTransform (can't start with digit)



Wednesday, November 18, 2009

3D Line chart with JavaScript

For a recent project, I was looking for a free 3-dimensional line chart library in JavaScript. There are a lot of server side charting applications both free and commercial. I have been using Google Charts API for quite a long time. But I had a very unique requirements that my web application must be highly interactive. So user should be able to click every single line on the chart and show/hide certain information.

First thing comes in my mind to program some JavaScript code for drawing operations. So I created a simple drawing library. But later I noticed, jsdraw2d does much better job. It is open sourced and good tested. So I have decided to use it instead.

I always consider OO important for any of my projects. In my web applications, I frequently use some wrapper library around JavaScript's prototype-based programming model. I like Mootools over Dojo, jQuery or YUI due to its pure object orientation appeal. The motto of Mootools states in its web site — ”MooTools is a compact, modular, Object-Oriented JavaScript framework designed for the intermediate to advanced JavaScript developer. It allows to write powerful, flexible, and cross-browser code with its elegant, well-documented, and coherent API”. Furthermore, it provides a large set of plug-ins for the development of many GUI components such as Progress Bar, Drag-Drop, Slider, Tooltip etc.

The library with all dependent files can be downloaded from the project web site at here. You can also browse the source at google code. Good luck with 3d-charting in JavaScript.

Show Example

Tuesday, November 17, 2009

Writing your first program - Hello World


This post is part of my ongoing effort to familiarize the readers with the commonalities & variabilities of C++/Java/C# programming languages. A tentative outline of this series can be found at Contents.

Enough words. Now it is time for some code. We are going to write our first program. Fire up your favorite text editor. We will write code in this simple editor till our work grows to several files. If you already have some IDE for the intended language, you can use them as well. Here we go-
  • C++
  • Java
  • C#
#include
using namespace std;

int main()
{
cout << "Hello World!\n"; }

namespace HelloWorldApp
{
public class HelloWorld
{
    public static void Main()
    {
        System.Console.WriteLine("Hello World!");
    }
}
}

Each program, when compiled, specifies a special function as the Entry Point. When a program starts, the platform looks for this function and all other functionalities must be reachable from this point. C++, Java and C# differ how they specify the entry point function, even though the standard entry point function is named main1 in all these languages.
  • C++
  • Java
  • C#
int main(void);
int main(int argc, char *argv[]);
public static void main(String[] args)
public static void main(String... args)
static void Main();
static void Main(string[] args);
static int Main();
static int Main(string[] args);
1. You can specify other function as Entry Point though this is very rare.

Compilation:
I have already discussed various compilers and IDEs for C++/Java/C# in the previous post. Now its time to compile and run our first program.
  • C++
  • Java
  • C#
Windows:
cl.exe helloworld.cpp
Run the program
helloworld.exe

Linux (GCC): There are 3 variants to compile a C++ program with gcc -
  1. gcc helloworld.cpp -lstdc++ -o helloworld
    gcc is usually used for C compilation.
    -lstdc++: compile in C++ mode.
    -o: indicates the output filename. If omitted, output filename is always a.out
  2. g++ helloworld.cpp -o helloworld
    uses gcc to compile in C++ mode.
  3. c++ helloworld.cpp -o helloworld
    Most systems install this program which is identical to g++.
Run the program
./helloworld
Sun Java:

javac HelloWorld

GCC (very rare):

gcj --main=HelloWorld HelloWorldApp.java -o HelloWorldApp

Run the program (Remember Java as well as Linux-File-System is case sensitive)
java HelloWorld
Windows (MS SDK):
csc.exe HelloWorld.cs
Run the program HelloWorld.exe

Linux(Mono):
gmcs HelloWorld.cs
Run the program ./HelloWorld.exe


Source Code for the Posts:
I have hosted a project in google code. The project page is cpp-java-csharp. Click the source tab. You can browse the source code by clicking browse tab or can download to your machine by clicking Downloads tab.

Structure of the Source Code:

As discussed in the previous post, I will use the following compilers and IDEs for our exercise works-

C++
  1. Windows:
    Visual C++ 2008 Express Edition
    Just open the solution file on Visual C++ 2008 Expression Edition or Visual Studio 2008. Compile and run.

    Command Line
    A batch (.bat) file will accompany each project. You need to run this batch file which will generate the executable .exe file. I assume that you already have the C++ compiler (cl.exe) in the path.
  2. Linux:
    Command Line
    A shell script (.sh) will accompany each project which will generate the executable file. The gcc must be in the path. Change the mode of the .sh file to executable with the following command:

    sudo chmod +x xyz.sh

    Run the file with the following command if you are in the project directory:
    ./xx.sh

Java
Eclipse:
  1. First create a new Java project in your workspace.
  2. Click "File->Import..." menu item.
  3. Import the project from the downloaded package
Netbeans:
Open the project from the downloaded package with "File->Open Project" menu item.


C#
Windows:
Visual C# 2008 Express Edition
Same as C++

Command Line
Same as C++

Linux:
MonoDevelop
Open the monodevelop project in the IDE. Compile and Run.

Command Line
A shell script (.sh) will be provided with the project. Consult C++ section to know how to run shell script in console.