Chris Pollett > Students >
Long

    ( Print View )

    [Bio]

    [Project Blog]

    [CS297 Proposal]

    [Del 1]

    [Del 2]

    [Del 3]

    [CS297Report-PDF]

    [CS298 Proposal]

                          

























Deliverable_1 J2ME "Hello World" Program

Description: This Deliverable is a simple J2ME program. The user will be asked for his/her name. After pressing the "Enter" button, the program will display a greeting "Hello .... Nice to meet you". In this deliverable, I learned how to setup environment to write and run a J2ME program. Moreover, I learned to use some of the user-interfaces for J2ME.

Example:This is what my code outputs on these inputs.

Asking User to enter his or her name

Prompting user to enter his/her name.

Saying hello to the user

Saying hello to the user.

/*
 Project          : Hello World J2ME Program (Deliverable #1)
 File Name        : HelloWorld.java
 Purpose          : This program will ask the user for his/her name,
                              then show a greeting messgage to the user.
 Created          : Sep 12, 2006
 Last Modified    : Oct 2, 2006
 Java Version     : 1.5.0_08
 */

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

/*
 @author Long N Vuong
 */

public class HelloWorld extends MIDlet implements CommandListener
{
   private Display display; //Reference to Display object
   private Form fmMain; //the main form
   private Command cmExit; //Button to exit the MIDlet
   private Command cmEnter; //Button to enter name
   private TextField tfName; //Person Name


   /*
     @Purpose: Create an instant of the program and setup UI .
     @param:
     @return:
  */
   public HelloWorld()
   {
      display = Display.getDisplay(this);

      cmExit = new Command ("Exit", Command.EXIT, 1);
      cmEnter = new Command ("Enter", Command.SCREEN, 1);

      tfName = new TextField("Your Name:", "", 20, TextField.ANY);
      fmMain = new Form("Hello World");
      fmMain.append(tfName);
      fmMain.addCommand(cmExit);
      fmMain.addCommand(cmEnter);
      fmMain.setCommandListener(this);

   }

   /*
     @Purpose: Called by application manager to start the MIDlet.
     @param:
     @return:
  */
   public void startApp()
   {
      display.setCurrent(fmMain);
   }

   /*
     @Purpose: Notify that the MIDlet is about to be placed into
                   paused state.
     @param:
     @return:
  */
   public void pauseApp() {}

   /*
     @Purpose: Signal the MIDlet that it is about to be shut down.
     @param:
     @return:
  */
   public void destroyApp(boolean unconditional) {}

   /*
     @Purpose: Define events handling to all commands occur.
     @param c: Command to be passed.
     @param s: Displayable object.
     @return: void
  */
   public void commandAction(Command c, Displayable s)
   {
      if (c==cmExit)
      {
         destroyApp(false);
         notifyDestroyed();
      }

      else if (c==cmEnter)
      {
         if (tfName.size() != 0)
         {
            String  tmp = "Hello ";
            tmp = tmp.concat(tfName.getString());
            tmp = tmp.concat(". Nice to meet you!");
            StringItem str = new StringItem(null, tmp);
            if (fmMain.size() == 1) //only textfield
               fmMain.append(str);
            else
               fmMain.set(1, str);  //replace previous StringItem
         }
      }
   }
}// end class HelloWorld