Thursday, December 11, 2008

Your First program in J2ME

a little introduction to the J2ME
well it stands for the java 2 Mobil Edition. There are other two versions , J2SE and J2EE.
However, the J2ME also runs on a virtual machine , but due to the constrains in the
hardware and software resources in the small devices ( what is J2ME target for ) , the J2ME
virtual machine is somewhat different from the J2SE and J2EE virtual machines. It's commonly
known as alias KVM ( the killobyte virtual machine ).

however today we are going to develop a simple J2ME mobil phone application. well J2ME has a
big scope than the writing just software for the mobil markert. well J2ME covers even writing
software for even your television , washing machines like these small or large size devices
that have small computing resources.( well computing resources means hardware resources like
memory storage and computing speed , and software resources like threads processes , operating
systems , libraries and so on ...) .Think this is different from the normal desktop computing environments and server computing environments(high end computing). The application
programming for these kind of devices are just normally doing with special tools , cross compilers
and simulators like software tools and in-circuit debuggers , circuit probes like hardware tools.
well that's how the software for the embedded computers are developing. But in the J2ME ,
we just use just the java compiler but with different libraries to compile it , and for testing we
are using simulators. well today J2ME simulators are compltely prefect actually have less bugs
.Trully speaking leass and leass bugs. so feel free to use them. and those simulators supports
a good amount of debugging. so it's very easy to find your bugs in your program. And the
IDE's like the Netbeans are there for free of charge to make this development more babish.
so unlike other embedded software development the J2ME is just easy.

Allright the tools like the netbeans already avilaible for you ne. Then feel free to use them. Just
go to the www.netbeans.org and download a Netbeans pack that have J2ME integraded.
Opps , before you install the netbeans you need to install the JDK 5.0 or higher. Well they
are free. Then download them and install them.

Come to the Netbeans then , here we goes.
well netbeans is a IDE , that makes the programmers life babish. Believe me you just only need
some more mouse clicks and moves to compile and run your first J2ME mobil phone application.

well first open the Netbeans.

well go to the file menu and click new project.
select mobility and MIDP application.


and then click next . and then next screen give project name , location and folder ( your choices ).
and make sure that "set as main project" and "Create Hello MIDlet" ticked. then press finish.



then compile the project using the 'build' menu 'build main project'. and run it using the 'run' menu 'run main project'

then on the simulator just laungh the midlet.
you can see "Hello World" on the simulator.

DEV C++ the problem with the system("PAUSE")

when you are writing a console mode win32 application using DEV C++ IDE , this is the
automatically generated main method that you get.

int main(int argc, char *argv[])
{
system("PAUSE");
return EXIT_SUCCESS;
}


some strange things are there , well nothing to worry , EXIT_SUCCESS is already defined as 0 .
so it's same like you saying

return 0;

anyway , what's the hell that system("PAUSE") , well
"PAUSE" is just a dos command , you can also "dir /p" or run any dos command using like that.

Just change your main method like this and compile and run.

int main(int argc, char *argv[])
{
system( "dir /p");
system ( "PAUSE");

return EXIT_SUCCESS;
}
then you will get the directory listing like this ,


so you now know what's this strange system("PAUSE"); really means is .
what it actually doing is ,
  1. suspend your program

  2. call the operating system

  3. open an operating system shell (relaunches the O/S in a sub-process)

  4. the O/S must now find the PAUSE command

  5. allocate the memory to execute the command

  6. execute the command and wait for a keystroke

  7. deallocate the memory

  8. exit the OS

  9. resume your program

so brother why this becomes a trouble for a learner , that who is newbie for the C++ programming language ?
well learning object oriented ... just write bellow simple program and see what happens .
#include
#include

using namespace std;

class A
{
public:
// the constructor
A()
{
cout<< " The constructor called " << '\n';
}

// the copy constructor
A(const A& a )
{
cout << " The copy constructor is called" << '\n';
}

~A()
{
cout << "The destructor is called "<< '\n';
}
};


void takeA(const A& a )
{

}
int main(int argc, char *argv[])
{
A a ;
// the constructor must be called.
system ( "PAUSE");
return EXIT_SUCCESS;
} // the destructor must be called.

well but the output is just like this ,

The constructor is called
Press any key to continue....
well where is the destructor call ? well my friend Press any key to continue is comes from the PAUSE dos command. If you are not sure just go to the command prompt and type PAUSE and
see what happened.
so the destructor will be called after that. But you will unable to see it unless you run this program
using the command prompt. But if you run this on the command prompt window , you will see like this . 13 Dir(s) 6,477,045,760 bytes free

G:\Dev-Cpp>main
The constructor called
Press any key to continue . . .
The destructor is called

G:\Dev-Cpp>


so that's all the magic of the system("pause") as same as system("PAUSE");