Form Hasil Akhir |
Form Awal/Input Data |
public class KonversiSuhu extends MIDlet implements CommandListener{
Display disp;
Form frmInput,frmHasil;
StringItem strCelcius,strFahrenheit,strReamur,strHasil,strHasil2,strHasil3,strKelvin;
TextField txtCelcius,txtFahrenheit,txtReamur,txtKelvin;
Command cmdKeluar,cmdHitung,cmdKembali;
//Tampilan Awal/Input Data
public KonversiSuhu(){
frmInput = new Form("Input Satuan Suhu");
frmHasil = new Form("Hasil");
strCelcius = new StringItem("", "Celcius (0-100)", StringItem.PLAIN);
txtCelcius = new TextField("", "", 30, TextField.NUMERIC);
strFahrenheit = new StringItem("", "Fahrenheit (32-212)", StringItem.PLAIN);
txtFahrenheit = new TextField("", "", 30, TextField.NUMERIC);
strReamur = new StringItem("", "Reamur (0-80)", StringItem.PLAIN);
txtReamur = new TextField("", "", 30, TextField.NUMERIC);
strKelvin = new StringItem("", "Kelvin (273-373)", StringItem.PLAIN);
txtKelvin = new TextField("", "", 30, TextField.NUMERIC);
cmdKeluar = new Command("Keluar", Command.EXIT, 0);
cmdHitung = new Command("Hitung", Command.OK, 1);
frmInput.append(strCelcius);
frmInput.append(txtCelcius);
frmInput.append(strFahrenheit);
frmInput.append(txtFahrenheit);
frmInput.append(strReamur);
frmInput.append(txtReamur);
frmInput.append(strKelvin);
frmInput.append(txtKelvin);
frmInput.addCommand(cmdKeluar);
frmInput.addCommand(cmdHitung);
frmInput.setCommandListener(this);
}
public void startApp() {
disp = Display.getDisplay(this);
disp.setCurrent(frmInput);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable d) {
if(d == frmInput){
if(c == cmdHitung){
if(!txtCelcius.getString().equals("")){
hitungCelcius();
}
else if(!txtFahrenheit.getString().equals("")){
hitungFahrenheit();
}
else if(!txtReamur.getString().equals("")){
hitungReamur();
}
else if(!txtKelvin.getString().equals("")){
hitungKelvin();
}
}else if(c==cmdKeluar){
destroyApp(true);
notifyDestroyed();
}
}else{
if(c == cmdKembali){
disp.setCurrent(frmInput);
}
}
}//Proses penghitungan
public void hitungCelcius(){frmHasil = new Form("Hasil");int celc = Integer.parseInt(txtCelcius.getString());int hasilReam = (4*celc/5);int hasilKelv = (celc+273);int hasilFahr = (9*celc/5+32);strHasil = new StringItem("", "",StringItem.PLAIN);strHasil.setText("Celcius ke Fahrenheit = "+hasilFahr);strHasil2 = new StringItem("", "",StringItem.PLAIN);strHasil2.setText("Celcius ke Reamur = "+hasilReam);strHasil3 = new StringItem("", "",StringItem.PLAIN);strHasil3.setText("Celcius ke Kelvin = "+hasilKelv);cmdKembali = new Command("Kembali", Command.BACK, 1);frmHasil.append(strHasil);frmHasil.append(strHasil2);frmHasil.append(strHasil3);frmHasil.addCommand(cmdKembali);frmHasil.setCommandListener(this);disp.setCurrent(frmHasil);}private void hitungFahrenheit() {frmHasil = new Form("Hasil");int fahr = Integer.parseInt(txtFahrenheit.getString());int hasilCelc = (5*(fahr-32)/9);int hasilReam = (4*(fahr-32)/9);int hasilKelv = (5*(fahr-32)/9)+273;strHasil = new StringItem("", "",StringItem.PLAIN);strHasil.setText("Fahrenheit ke Celcius = "+hasilCelc);strHasil2 = new StringItem("", "",StringItem.PLAIN);strHasil2.setText("Fahrenheit ke Reamur = "+hasilReam);strHasil3 = new StringItem("", "",StringItem.PLAIN);strHasil3.setText("Fahrenheit ke Kelvin = "+hasilKelv);cmdKembali = new Command("Kembali", Command.BACK, 1);frmHasil.append(strHasil);frmHasil.append(strHasil2);frmHasil.append(strHasil3);frmHasil.addCommand(cmdKembali);frmHasil.setCommandListener(this);disp.setCurrent(frmHasil);}private void hitungReamur() {frmHasil = new Form("Hasil");int ream = Integer.parseInt(txtReamur.getString());int hasilCelc = (5*ream/4);int hasilFahr = (9*ream/4)+32;int hasilKelv = (5*ream/4)+273;strHasil = new StringItem("", "",StringItem.PLAIN);strHasil.setText("Reamur ke Celcius = "+hasilCelc);strHasil2 = new StringItem("", "",StringItem.PLAIN);strHasil2.setText("Reamur ke Fahrenheit = "+hasilFahr);strHasil3 = new StringItem("", "",StringItem.PLAIN);strHasil3.setText("Reamur ke Kelvin = "+hasilKelv);cmdKembali = new Command("Kembali", Command.BACK, 1);frmHasil.append(strHasil);frmHasil.append(strHasil2);frmHasil.append(strHasil3);frmHasil.addCommand(cmdKembali);frmHasil.setCommandListener(this);disp.setCurrent(frmHasil);}private void hitungKelvin() {frmHasil = new Form("Hasil");int kelv = Integer.parseInt(txtKelvin.getString());int hasilCelc = (kelv-273);int hasilReam = (4*hasilCelc/5);int hasilFahr = (9*hasilCelc/5+32);strHasil3 = new StringItem("", "",StringItem.PLAIN);strHasil3.setText("Kelvin ke Celcius = "+hasilCelc);strHasil = new StringItem("", "",StringItem.PLAIN);strHasil.setText("Kelvin ke Fahrenheit = "+hasilFahr);strHasil2 = new StringItem("", "",StringItem.PLAIN);strHasil2.setText("Kelvin ke Reamur = "+hasilReam);cmdKembali = new Command("Kembali", Command.BACK, 1);frmHasil.append(strHasil);frmHasil.append(strHasil2);frmHasil.append(strHasil3);frmHasil.addCommand(cmdKembali);frmHasil.setCommandListener(this);disp.setCurrent(frmHasil);}}
Semoga berguna :)
RMRPS
0 comments:
Post a Comment