Search This Blog

Thursday, 17 August 2023

Developing a MIDlet that Adds given Two Numbers

0 comments

MIDlet for Adding Two Numbers


Program:

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

public class HelloMidlet extends MIDlet implements CommandListener{
private Form form;
private TextField txtA, txtB;
private Command cmdDisp;
private Display display;
public StringItem st;

public HelloMidlet(){ super(); }

public void startApp() {
     form = new Form("Addition MIDlet");
     //String msg = "Hello World!!!!!!!";
     txtA = new TextField("Value of A", "", 5, 0);
     txtB = new TextField("Value of B", "", 5, 0);
     cmdDisp = new Command("Add",Command.OK,1);
     form.append(txtA);
     form.append(txtB);
     form.addCommand(cmdDisp);
     form.setCommandListener(this);
     display = Display.getDisplay(this);
     st = new StringItem("",""); 
     display.setCurrent(form);
    }
   
    public void pauseApp() {
    }
   
    public void destroyApp(boolean unconditional) {
        notifyDestroyed();
    }
    public void commandAction(Command command,Displayable displayable) {
        int a, b, res;
        if(command==cmdDisp) {
            a = Integer.parseInt(txtA.getString());
            b = Integer.parseInt(txtB.getString());
            res = a + b;
            st.setText(""); 
            st.setText("Addition of " + txtA.getString() + " and " + txtB.getString() + " is " + res ); 
            form.append(st); 
        }
    }
}

Output:



Leave a Reply