Skip to content

Instantly share code, notes, and snippets.

@resarahadian
Created March 20, 2012 17:01
Show Gist options
  • Save resarahadian/2138217 to your computer and use it in GitHub Desktop.
Save resarahadian/2138217 to your computer and use it in GitHub Desktop.
Aplikasi Konversi Suhu dengan J2ME
/*
*@ author Resa C.R
*/
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class konverter extends MIDlet implements CommandListener
{
private Display tampil;
private Alert peringatan;
private Form formIsi,formHasil;
private StringItem sCelcius,sFahrenheit,sReamur,sHasilCelcius,sHasilFahrenheit,sHasilReamur;
private TextField txtCelcius,txtFahrenheit,txtReamur;
Command comKeluar,comHitung,comKembali;
public konverter()
{
sCelcius = new StringItem("","Celcius",StringItem.PLAIN);
txtCelcius = new TextField("","",20,TextField.NUMERIC);
sFahrenheit = new StringItem("","Fahrenheit",StringItem.PLAIN);
txtFahrenheit = new TextField("","",20,TextField.NUMERIC);
sReamur = new StringItem("","Reamur",StringItem.PLAIN);
txtReamur = new TextField("","",20,TextField.NUMERIC);
comHitung = new Command("Hitung",Command.OK,0);
comKeluar = new Command("Keluar",Command.EXIT,1);
}
public void startApp()
{
tampil = Display.getDisplay(this);
formIsi = new Form("Konversi Suhu V.01b");
formIsi.append(sCelcius);
formIsi.append(txtCelcius);
formIsi.append(sFahrenheit);
formIsi.append(txtFahrenheit);
formIsi.append(sReamur);
formIsi.append(txtReamur);
formIsi.addCommand(comHitung);
formIsi.addCommand(comKeluar);
formIsi.setCommandListener(this);
tampil.setCurrent(formIsi);
}
public void hitungCelcius()
{
formHasil = new Form("Hasil Konversi");
double celcius = Double.parseDouble(txtCelcius.getString());
double fahrenheit = (celcius * 1.8) + 32;
double reamur = celcius * 0.8;
sHasilFahrenheit = new StringItem("","",StringItem.PLAIN);
sHasilFahrenheit.setText("Konversi Celcius--->Fahrenheit : " + fahrenheit);
sHasilReamur = new StringItem("","",StringItem.PLAIN);
sHasilReamur.setText("Konversi Celcius--->Reamur : " + reamur);
comKembali = new Command("Kembali",Command.BACK,1);
comKeluar = new Command("Keluar",Command.EXIT,0);
formHasil.append(sHasilFahrenheit);
formHasil.append(sHasilReamur);
formHasil.addCommand(comKembali);
formHasil.addCommand(comKeluar);
formHasil.setCommandListener(this);
tampil.setCurrent(formHasil);
}
public void hitungFahrenheit()
{
Form formHasil = new Form("Hasil Konversi");
double fahrenheit = Double.parseDouble(txtFahrenheit.getString());
double celcius = (fahrenheit - 32) / 1.8;
double reamur = (fahrenheit - 32) / 2.25;
StringItem sHasilCelcius = new StringItem("","",StringItem.PLAIN);
sHasilCelcius.setText("Konversi Fahrenheit--->Celcius : " + celcius);
StringItem sHasilReamur = new StringItem("","",StringItem.PLAIN);
sHasilReamur.setText("Konversi Fahrenheit--->Reamur : " + reamur);
comKembali = new Command("Kembali",Command.BACK,1);
comKeluar = new Command("Keluar",Command.EXIT,0);
formHasil.append(sHasilCelcius);
formHasil.append(sHasilReamur);
formHasil.addCommand(comKembali);
formHasil.addCommand(comKeluar);
formHasil.setCommandListener(this);
tampil.setCurrent(formHasil);
}
public void hitungReamur()
{
Form formHasil = new Form("Hasil Konversi");
double reamur = Double.parseDouble(txtReamur.getString());
double celcius = reamur / 0.8;
double fahrenheit = reamur * 2.25 + 32;
StringItem sHasilCelcius = new StringItem("","",StringItem.PLAIN);
sHasilCelcius.setText("Konversi Reamur--->Celcius : " + celcius);
StringItem sHasilFahrenheit = new StringItem("","",StringItem.PLAIN);
sHasilFahrenheit.setText("Konversi Reamur--->Fahrenheit : " + fahrenheit);
comKembali = new Command("Kembali",Command.BACK,1);
comKeluar = new Command("Keluar",Command.EXIT,0);
formHasil.append(sHasilCelcius);
formHasil.append(sHasilFahrenheit);
formHasil.addCommand(comKembali);
formHasil.addCommand(comKeluar);
formHasil.setCommandListener(this);
tampil.setCurrent(formHasil);
}
public void Peringatan()
{
peringatan = new Alert("Pesan Kesalahan","Isi salah satu TextField",null,AlertType.INFO);
tampil.setCurrent(peringatan,formIsi);
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
notifyDestroyed();
}
public void commandAction(Command com,Displayable dsp)
{
if(dsp == formIsi)
{
if(com == comHitung)
{
if((!txtCelcius.getString().equals("")) &&
(txtFahrenheit.getString().equals("")) && (txtReamur.getString().equals("")))
{
hitungCelcius();
}
else if((!txtFahrenheit.getString().equals("")) &&
(txtCelcius.getString().equals("")) && (txtReamur.getString().equals("")))
{
hitungFahrenheit();
}
else if((!txtReamur.getString().equals("")) &&
(txtCelcius.getString().equals("")) && (txtFahrenheit.getString().equals("")))
{
hitungReamur();
}
else
{
Peringatan();
}
}
else if(com == comKeluar)
{
destroyApp(true);
notifyDestroyed();
}
}
else
{
if(com == comKembali)
{
tampil.setCurrent(formIsi);
}
else if(com == comKeluar)
{
destroyApp(true);
notifyDestroyed();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment