Program Konversi Suhu Menggunakan JAVA ( j2me )
/* Menu Conveter Suhu */
/* Memasukan nilai suhu 40 derajat Celcius */
/* Proses mneghitung dengan menekan tombol hitung */
/* Hasil konversi derajat celcius ke fahrenheit dan reamur */
Code Program :
/*
* 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 derangga
*/
public class converter extends MIDlet implements CommandListener {
Display disp;
Form frminput , frmhasil;
StringItem strinput, strhasil , strfahrenheit , strreamur;
TextField txtcelcius , txtfahrenheit, txtreamur ;
Command cmdexit,cmdhtg,cmdkembali;
int hasilinputius,hasilfahrenheit,hasilreamur;
public converter()
{
frminput = new Form("input nilai suhu");
frmhasil = new Form("hasil");
strinput = new StringItem("","",StringItem.PLAIN);
txtcelcius= new TextField("celcius", "",30, TextField.NUMERIC);
strfahrenheit = new StringItem("","",StringItem.PLAIN);
txtfahrenheit= new TextField("fahrenheit", "",30, TextField.NUMERIC);
strreamur= new StringItem("","",StringItem.PLAIN);
txtreamur= new TextField("reamur", "",30, TextField.NUMERIC);
cmdexit = new Command("keluar",Command.EXIT, 0);
cmdhtg = new Command("Hitung",Command.OK , 1);
frminput.append(strinput);
frminput.append(txtcelcius);
frminput.append(strfahrenheit);
frminput.append(txtfahrenheit);
frminput.append(strreamur);
frminput.append(txtreamur);
frminput.addCommand(cmdexit);
frminput.addCommand(cmdhtg);
frminput.setCommandListener(this);
Command cmd = new Command("",Command.OK,1);
}
public void startApp() {
if(disp==null)
{
disp = Display.getDisplay(this);
disp.setCurrent(frminput);
}
}
public void hitungsuhu()
{
frmhasil = new Form("hasil");
//inputan inputius
if(txtfahrenheit.getString().length()==0 && txtreamur.getString().length()==0)
{
int celc = Integer.parseInt(txtcelcius.getString());
hasilfahrenheit = ((celc*9)/5)+32;
hasilreamur = (celc*4/5);
strhasil = new StringItem("","",StringItem.PLAIN);
strhasil.setText("celcius ke fahrenheit = "+hasilfahrenheit+"\n"+"celcius ke reamur = "+hasilreamur);
}else
//inputan fahrenheit
if(txtcelcius.getString().length()==0 && txtreamur.getString().length()==0)
{
int fahr = Integer.parseInt(txtfahrenheit.getString());
hasilinputius = ((fahr-32)*5)/9;
hasilreamur = ((fahr-32)*4)/9;
strhasil = new StringItem("","",StringItem.PLAIN);
strhasil.setText("fahrenheit ke celcius = "+hasilinputius+"\n"+"fahrenheit ke reamur = "+hasilreamur);
}
else
//inputan reamur
if(txtcelcius.getString().length()==0 && txtfahrenheit.getString().length()==0)
{
int rmr = Integer.parseInt(txtreamur.getString());
hasilinputius = (rmr*5)/4;
hasilfahrenheit = ((rmr*9)/4)+32;
strhasil = new StringItem("","",StringItem.PLAIN);
strhasil.setText("reamur ke celcius = "+hasilinputius+"\n"+"reamur ke fahrenheit = "+hasilfahrenheit);
}
else
{
strhasil = new StringItem("","",StringItem.PLAIN);
strhasil.setText("silahkan ulangi lagi");
}
cmdkembali = new Command("Kembali", Command.BACK, 1);
frmhasil.append(strhasil);
frmhasil.addCommand(cmdkembali);
frmhasil.setCommandListener(this);
disp.setCurrent(frmhasil);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable d) {
if(d == frminput)
{
if(c == cmdhtg)
{
hitungsuhu();
}
else if(c == cmdexit)
{
destroyApp(true);
notifyDestroyed();
}
}else{
if(c == cmdkembali)
{
disp.setCurrent(frminput);
}
}
}
}