Java Program to Add two Numbers By Chaitanya Singh Filed Under: Java Examples Here we will see two programs to add two numbers, In the first program we specify the value of both the numbers in the program itself.
17 Mar 2011CC (ASA 3U)
This is a J2ME beginner article that describes how to write your Hello World J2ME application
Introduction
Mobile technology is a day-to-day technology that almost everyone has. Some even use this technology more than PC and they are always in need for new features and functionality. These features and functionality are provided using Mobile Applications, so I decided to write a series of articles about building mobile applications.
Background
J2ME applications are simply Java applications with a limited functionality that are designed to run on mobile devices. The standard Java runtime environment for these devices is provided using Mobile Information Device Profile (MIDP) combined with the Connected Limited Device Configuration (CLDC). MIDP and CLDC simply provide the core application functionality required by mobile applications. To write J2ME applications, you need the Java Platform Micro Edition SDK and an IDE, which can be Netbeans or Eclipse.
Creating MIDlet
All J2ME applications must have a main class which should be derived from a special class called
MIDlet
. It is like the entry point for the application and manages the life cycle of the application. The MIDlet
can be in one of three states: active, paused and destroyed. MIDlet
is put into a paused state when the application manager calls pauseApp()
method, and when it calls startApp()
method the MIDlet
is put into the active state. The destroyed state is entered when the MIDlet
destroyApp()
method is called or when the MIDlet
itself calls notifyDestroyed()
method.The
MIDlet
class is located in the javax.microedition.midlet
package, so the first thing to do is to import this package in addition to the javax.microedition.lcdui
which contains the J2ME UI components.The next step is to create a new class derived from
MIDlet
class, and to implement the three abstract
methods: startApp()
, pauseApp()
and destroyApp()
.The Display and Displayable
The J2ME application can run in the background or interact with the user. Interactive applications need access to mobile display by obtaining an instance of
Display
class. The Display
object is used to display the required user interface which should be derived from the Displayable
class, in this article an instance of the Form
class will be used. The Form
is a derived class of the Displayable
class, which has a title and you can add contents to it using other UI components.So the next step is to define two data members of the
Display
and Form
classes.In the constructor of
MIDlet
class, creates an instance of Form
class by calling the constructor and passing the form title to it.Display The Form
The next step is to obtain an instance of the
Display
class and display the form. To get an instance of the Display
class, you call the Display
class static
method getDisplay()
and passing a reference to the MIDlet
object to it. Next, use the Display
object setCurrent()
method to display the Form
.Conclusion
This article describes how to create a J2ME Hello World mobile application. The first step which is required for all J2ME applications is to create the
MIDlet
class, which is the main class in your application. Then you can use an instance of the Form
class as the main UI in the application, which should be displayed using an object of the Display
class.Following is the complete application code:
History
- 17th March, 2011: Initial version
Factorial Program in Java: Factorial of n is the product of all positive descending integers. Factorial of n is denoted by n!. For example:
Here, 4! is pronounced as '4 factorial', it is also called '4 bang' or '4 shriek'.
The factorial is normally used in Combinations and Permutations (mathematics).
There are many ways to write the factorial program in java language. Let's see the 2 ways to write the factorial program in java.
- Factorial Program using loop
- Factorial Program using recursion
Factorial Program using loop in java
Let's see the factorial Program using loop in java.
Output:
Factorial Program using recursion in java
Let's see the factorial program in java using recursion.
Output:
Next TopicJava Programs