Kalkulator Berbasis Java MIDP Untuk Handphone

Bookmark and Share
Dalam latihan ini, kita akan membuat aplikasi kalkulator sederhana untuk digunakan pada perangkat mobile seperti handphone.

Berikut syntax kalkulatornya

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package hello;

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

/**
* @author andhy
*/
public class HelloMIDlet extends MIDlet implements CommandListener {

private boolean midletPaused = false;

//
private Command exitCommand;
private Command okCommand;
private Form form;
private TextField Ekspresi;
private Spacer spacer;
private TextField Hasil;
//


/**
* The HelloMIDlet constructor.
*/
public HelloMIDlet() {
}

//
//


//
/**
* Initilizes the application.
* It is called only once when the MIDlet is started. The method
is called before the startMIDlet method.
*/
private void initialize() {
// write pre-initialize user code here

// write post-initialize user code here
}
//


//
/**
* Performs an action assigned to the Mobile Device - MIDlet Started point.
*/
public void startMIDlet() {
// write pre-action user code here
switchDisplayable(null, getForm());
// write post-action user code here
}
//


//
/**
* Performs an action assigned to the Mobile Device - MIDlet Resumed point.
*/
public void resumeMIDlet() {
// write pre-action user code here

// write post-action user code here
}
//


//
/**
* Switches a current displayable in a display. The display instance is taken from getDisplay method. This method is used by all actions in the design for switching displayable.
* @param alert the Alert which is temporarily set to the display; if null, then nextDisplayable is set immediately
* @param nextDisplayable the Displayable to be set
*/
public void switchDisplayable(Alert alert, Displayable nextDisplayable) {
// write pre-switch user code here
Display display = getDisplay();
if (alert == null) {
display.setCurrent(nextDisplayable);
} else {
display.setCurrent(alert, nextDisplayable);
}
// write post-switch user code here
}
//


//
/**
* Called by a system to indicated that a command has been invoked on a particular displayable.
* @param command the Command that was invoked
* @param displayable the Displayable where the command was invoked
*/
public void commandAction(Command command, Displayable displayable) {
// write pre-action user code here
if (displayable == form) {
if (command == exitCommand) {
// write pre-action user code here
exitMIDlet();
// write post-action user code here
} else if (command == okCommand) {
// write pre-action user code here
String input;
char operator;
int operand1, operand2, start, end, res;
//pengambilan ekspresi yang dimasukan
input = Ekspresi.getString();
try{
//memecah operand ke 2
end = input.indexOf(' ',0);
operand1 = Integer.parseInt(input.substring(0, end));
//memecah operator
start = end + 1;
operator = input.charAt(start);
//memecah operand ke 2
start = start + 2;
operand2 = Integer.parseInt(input.substring(start));
//proses aritmatika
switch (operator){
case '+':
res = operand1 + operand2;
break;
case '-':
res = operand1 - operand2;
break;
case '*':
res = operand1 * operand2;
break;
case '/':
res = operand1 / operand2;
break;
default:
throw new Exception("Salah ekspresi.");
}
//Menampilkan Hasil perhitungan dilayar
Hasil.setString(Integer.toString(res));
}
catch(Exception ex){
Hasil.setString("Hasil Error ");
}

// write post-action user code here
}
}
// write post-action user code here
}
//


//
/**
* Returns an initiliazed instance of exitCommand component.
* @return the initialized component instance
*/
public Command getExitCommand() {
if (exitCommand == null) {
// write pre-init user code here
exitCommand = new Command("Exit", Command.EXIT, 0);
// write post-init user code here
}
return exitCommand;
}
//


//
/**
* Returns an initiliazed instance of form component.
* @return the initialized component instance
*/
public Form getForm() {
if (form == null) {
// write pre-init user code here
form = new Form("Kalkulator", new Item[] { getEkspresi(), getSpacer(), getHasil() });
form.addCommand(getExitCommand());
form.addCommand(getOkCommand());
form.setCommandListener(this);
// write post-init user code here
}
return form;
}
//




//
/**
* Returns an initiliazed instance of okCommand component.
* @return the initialized component instance
*/
public Command getOkCommand() {
if (okCommand == null) {
// write pre-init user code here
okCommand = new Command("Ok", Command.OK, 0);
// write post-init user code here
}
return okCommand;
}
//


//
/**
* Returns an initiliazed instance of Ekspresi component.
* @return the initialized component instance
*/
public TextField getEkspresi() {
if (Ekspresi == null) {
// write pre-init user code here
Ekspresi = new TextField("Ekspresi", null, 32, TextField.ANY);
// write post-init user code here
}
return Ekspresi;
}
//


//
/**
* Returns an initiliazed instance of spacer component.
* @return the initialized component instance
*/
public Spacer getSpacer() {
if (spacer == null) {
// write pre-init user code here
spacer = new Spacer(16, 1);
// write post-init user code here
}
return spacer;
}
//


//
/**
* Returns an initiliazed instance of Hasil component.
* @return the initialized component instance
*/
public TextField getHasil() {
if (Hasil == null) {
// write pre-init user code here
Hasil = new TextField("Hasil", null, 32, TextField.ANY);
// write post-init user code here
}
return Hasil;
}
//


/**
* Returns a display instance.
* @return the display instance.
*/
public Display getDisplay () {
return Display.getDisplay(this);
}

/**
* Exits MIDlet.
*/
public void exitMIDlet() {
switchDisplayable (null, null);
destroyApp(true);
notifyDestroyed();
}

/**
* Called when MIDlet is started.
* Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
*/
public void startApp() {
if (midletPaused) {
resumeMIDlet ();
} else {
initialize ();
startMIDlet ();
}
midletPaused = false;
}

/**
* Called when MIDlet is paused.
*/
public void pauseApp() {
midletPaused = true;
}

/**
* Called to signal the MIDlet to terminate.
* @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
*/
public void destroyApp(boolean unconditional) {
}

}

Dan Hasilnya Seperti ini :

{ 0 comment... Views All / Send Comment! }

Post a Comment