Membuat Project Kalkulator dengan Sun Java (TM) Wireless Toolkit 2.5.2


awal

awal


Seperti biasanya seperti pada pembahasan sebelumnya dalam memulainya kita buat dulu Project Namenya “Project Kalkulator” dan nama MIDlet class Name nya “kalkulator” klik create project dan OK
selanjutnya ketikkan kode dibawah ini


import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class kalkulator extends MIDlet implements CommandListener
{
Display display;
Form formcalc, formhasil, formabout;
TextField input1, input2, output;
StringItem labelhasil, pesan;
Command exit, quit, about, tambah, kurang, kali, bagi, selesai;
Ticker ticker1;

public kalkulator()
{
formcalc=new Form("Kalkulator");
ticker1= new Ticker ("Kalkulator Kecil");
formcalc.setTicker(ticker1);
exit=new Command("Exit", Command.EXIT,0);
about=new Command("About", Command.SCREEN,1);
tambah=new Command("+", Command.SCREEN,1);
kurang=new Command("-", Command.SCREEN,1);
kali=new Command("x", Command.SCREEN,1);
bagi=new Command("/", Command.SCREEN,1);

formcalc.addCommand(exit);
formcalc.addCommand(about);
formcalc.addCommand(tambah);
formcalc.addCommand(kurang);
formcalc.addCommand(kali);
formcalc.addCommand(bagi);
formcalc.setCommandListener(this);

input1=new TextField("Input 1 :","",15, TextField.DECIMAL);
input2=new TextField("Input 2 :","",15, TextField.DECIMAL);

formcalc.append(input1);
formcalc.append(input2);

formhasil=new Form("Kalkulator");
selesai=new Command("Selesai", Command.SCREEN,1);
quit=new Command("Quit", Command.EXIT,0);

formhasil.addCommand(quit);
formhasil.addCommand(selesai);
formhasil.setCommandListener(this);

labelhasil=new StringItem(null, "Hasil :\n");
formhasil.append(labelhasil);
output=new TextField("","",15, TextField.UNEDITABLE);
formhasil.append(output);

formabout=new Form("Tentang Kami");

formabout.addCommand(exit);
formabout.setCommandListener(this);
pesan=new StringItem(null, "About :\n"+"Aplikasi ini untuk matakuliah Wireless.\n");
pesan.setFont(Font.getFont(0,Font.STYLE_BOLD,Font.SIZE_LARGE));
formabout.append(pesan);
}

public void startApp()
{
display=Display.getDisplay(this);
display.setCurrent(formcalc);
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
public void commandAction(Command c, Displayable s)
{
if(c==exit)
{
destroyApp(false);
notifyDestroyed();
}
else if(c==selesai)
{
display.setCurrent(formcalc);
}
else if(c==quit)
{
destroyApp(false);
notifyDestroyed();
}
else
{
double hasil;
double a1=Double.parseDouble(input1.getString());
double a2=Double.parseDouble(input2.getString());
if(c==tambah)
{
hasil=a1+a2;
}
else if(c==kurang)
{
hasil=a1-a2;
}
else if(c==kali)
{
hasil=a1*a2;
}
else if(c==bagi)
{
hasil=a1/a2;
}
else
{
hasil=0;
}
String akhir=Double.toString(hasil);
if(akhir.length()<=15)
output.setString(akhir);
else output.setString(akhir.substring(0,15));
display.setCurrent(formhasil);
}
}
}


simpan dengan nama “kalkulator.java”
silahkan praktekkan, semoga bermanfaat 😀

hasilnya adalah sbb :

Preview

Preview

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s