Rabu, 11 Februari 2009

Program Kalkulator Java

LISTING PROGRAM Kalkulator.java


import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;

public final class Kalkulator extends MIDlet implements CommandListener {
private final Ticker ticker = new Ticker("Tugas Java Simple Kalkulkator");

//jumlah karakter numeric yang diperbolehkan
private static final int NUM_SIZE = 20;

//Tombol untuk keluar aplikasi
private final Command exitCmd = new Command("Keluar", Command.EXIT, 2);

//Menu menghitung hasil
private final Command calcCmd = new Command("=", Command.SCREEN, 1);

//Textfield pertama
private final TextField t1 = new TextField(null, "", NUM_SIZE, TextField.DECIMAL);

//textfield kedua
private final TextField t2 = new TextField(null, "", NUM_SIZE, TextField.DECIMAL);

//Textfield untuk menampilkan hasil
private final TextField tr = new TextField("Hasil", "", NUM_SIZE, TextField.ANY);

//Pemilihan tanda
private final ChoiceGroup cg =
new ChoiceGroup("", ChoiceGroup.EXCLUSIVE,
new String[] { "+ Tambah", "- Kurang", "* Kali", "/ Bagi" }, null);

//peringatan jika terjadi kesalahan
private final Alert alert = new Alert("Error", "", null, AlertType.ERROR);

private boolean isInitialized = false;

//Menampilkan program pada layar dan menjalankan program
protected void startApp( ) {
if (isInitialized) {
return;
}

Form f = new Form("Kalkulator");
f.setTicker(ticker);
f.append(t1);
f.append(cg);
f.append(t2);
f.append(tr);
f.append(new Gauge("Aplikasi Kalkulator sederhana", false, Gauge.INDEFINITE,
Gauge.CONTINUOUS_RUNNING));
f.append("Denny RS,Esty P,Indah K,Nanik S, Yuni P");
f.addCommand(exitCmd);
f.addCommand(calcCmd);
f.setCommandListener(this);
Display.getDisplay(this).setCurrent(f);
isInitialized = true;
}
protected void destroyApp(boolean unconditional) {
}

protected void pauseApp() {
}


public void commandAction(Command c, Displayable d) {
if (c == exitCmd) {
destroyApp(false);
notifyDestroyed();

return;
}

double res = 0.0;

try {
double n1 = getNumber(t1, "Pertama");
double n2 = getNumber(t2, "Kedua");

switch (cg.getSelectedIndex()) {
case 0:
res = n1 + n2;

break;

case 1:
res = n1 - n2;

break;

case 2:
res = n1 * n2;

break;

case 3:
res = n1 / n2;

break;

default:
}
} catch (NumberFormatException e) {
return;
} catch (ArithmeticException e) {
alert.setString("Divide by zero.");
Display.getDisplay(this).setCurrent(alert);

return;
}

/*
* The resulted string may exceed the text max size.
* We need to correct last one then.
*/
String res_str = Double.toString(res);

if (res_str.length() > tr.getMaxSize()) {
tr.setMaxSize(res_str.length());
}

tr.setString(res_str);
}

//Mengambil data dari textfield
private double getNumber(TextField t, String type)
throws NumberFormatException {
String s = t.getString();

if (s.length() == 0) {
alert.setString("Kolom " + type + " belum di isi !");
Display.getDisplay(this).setCurrent(alert);
throw new NumberFormatException();
}

double n;

try {
n = Double.parseDouble(s);
} catch (NumberFormatException e) {
alert.setString(type + " argument is out of range.");
Display.getDisplay(this).setCurrent(alert);
throw e;
}

return n;
}
} // Akhir dari class Kalkulator ...

Tidak ada komentar:

Posting Komentar