Hai kawan, dipertemuan kali ini saya membuat sebuah aplikasi untuk mempermudah kita mengetahui perbandingan suhu/temperatur seperti Celcius, Fahrenheit, Reamur dan Kelvin. Adapun nama aplikas tersebut ialah "Aplikasi Konversi Suhu berbasis J2ME (Java 2 Mobile Edition)" dengan menggunakan netbean 6.8 .
Sebelum melihat tampilannya, ada baiknya kita melihat dan mempelajari terlebih dahulu source code dari Aplikasi Konversi Suhu itu sendiri. Ini dia source codenya :
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package hello;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.*;
/**
* @author Kardiyoga
*/
public class Konversi_Suhu extends MIDlet implements CommandListener {
Display disp;
Form frmInput, frmHasil;
StringItem strCelcius, strFahrenheit, strReamur, strKelvin, strHasil;
TextField txtCelcius, txtFahrenheit, txtReamur, txtKelvin;
Command cmdExit, cmdHitung, cmdKembali;
public Konversi_Suhu()
{
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);
cmdExit = 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(cmdExit);
frmInput.addCommand(cmdHitung);
frmInput.setCommandListener((CommandListener) this);
}
public void startApp() {
if(disp == null){
disp = Display.getDisplay(this);
disp.setCurrent(frmInput);
}
}
public void hitungSuhu(){
frmHasil = new Form("Hasil Konversi Suhu");
if(!txtCelcius.getString().equals("")){
int celcius = Integer.parseInt(txtCelcius.getString());
int fahrenheit = ((celcius)*9/5+32);
int reamur = (celcius*4/5);
int kelvin = (celcius+273);
strHasil = new StringItem("","",StringItem.PLAIN);
strHasil.setText("C -> F = "+fahrenheit+"\n\nC -> R = "+reamur+"\n\nC -> K = "+kelvin);
}
else if(!txtFahrenheit.getString().equals("")){
int fahrenheit = Integer.parseInt(txtFahrenheit.getString());
int celcius = ((fahrenheit-32)*5/9);
int reamur = ((fahrenheit-32)*4/9);
int kelvin = (5*(fahrenheit-32)/9)+273;
strHasil = new StringItem("","",StringItem.PLAIN);
strHasil.setText("F -> C = "+celcius+"\n\nF -> R = "+reamur+"\n\nF -> K = "+kelvin);
}
else if(!txtReamur.getString().equals("")){
int reamur = Integer.parseInt(txtReamur.getString());
int celcius = ((reamur)*5/4);
int fahrenheit = (((reamur)*9/4)+32);
int kelvin = (5*reamur/4)+273;
strHasil = new StringItem("","",StringItem.PLAIN);
strHasil.setText("R -> C = "+celcius+"\n\nR -> F = "+fahrenheit+"\n\nR -> K = "+kelvin);
}
else if(!txtKelvin.getString().equals("")){
int kelvin = Integer.parseInt(txtKelvin.getString());
int celcius = (kelvin-273);
int reamur = (4*celcius/5);
int fahrenheit = (9*celcius/5+32);
strHasil = new StringItem("","",StringItem.PLAIN);
strHasil.setText("K -> C = "+celcius+"\n\nK -> F = "+fahrenheit+"\n\nK -> R = "+reamur);
}
cmdKembali = new Command("Kembali", Command.BACK, 1);
frmHasil.append(strHasil);
frmHasil.addCommand(cmdKembali);
frmHasil.setCommandListener((CommandListener) this);
disp.setCurrent(frmHasil);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable d) {
if(d == frmInput){
if(c == cmdHitung){
hitungSuhu();
}else if(c == cmdExit){
destroyApp(true);
notifyDestroyed();
}
}else{
if(c == cmdKembali){
disp.setCurrent(frmInput);
}
}
}
}
dan inilah hasil tampilan dari source code diatas:
/* Menu Awal */
-Pilih Midlet
/* Tampilan Menu Input Satuan Suhu */
/* Masukan nilai suhu sesuai kehendak anda (misalkan 10 derajat Celsius) */
- Masukan nilai suhu disalah satu text field yang tersedia.
- Lalu selanjutnya pilih tulisan Menu di bagian bawah samping kanan emulator lalu pilih Hitung.
/* Maka akan tampil hasil konversi dari Celsius ke Fahrenheit, Reamur dan Kelvin */
Sekian posting saya kali ini, semoga dapat bermanfaat bagi rekan – rekan pembaca sekalian. Mohon maaf apabila terdapat kesalahan penulisan yang diakibatkan kekeliruan saya dalam menulis postingan ini.
Terima Kasih,
Wassalamu'alaikum wr, wb.