-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kalkulator.java
95 lines (78 loc) · 2.48 KB
/
Kalkulator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/**
*
* @author Catur Andi Pamungkas
* @version 1.0.0
*
*/
public class Kalkulator extends MIDlet implements CommandListener{
Display display;
Form calcForm;
TextField txtOperand1,txtOperand2, txtResult;
ChoiceGroup choiceOperator;
Command commandHitung, commandExit,commandReset;
double hasil;
public Kalkulator(){
commandExit = new Command("Exit",Command.EXIT,0);
commandHitung = new Command("Hitung",Command.OK,1);
commandReset = new Command("Reset",Command.OK,1);
calcForm = new Form("Kalkulator ");
calcForm.setCommandListener(this);
calcForm.addCommand(commandExit);
calcForm.addCommand(commandHitung);
calcForm.addCommand(commandReset);
txtOperand1 = new TextField("Nilai Pertama","",25,TextField.DECIMAL);
txtOperand2 = new TextField("Nilai Kedua","",25,TextField.DECIMAL);
txtResult = new TextField("Hasil Perhitungan","",25,TextField.UNEDITABLE);
choiceOperator = new ChoiceGroup("Operator",Choice.POPUP);
choiceOperator.append("+",null);
choiceOperator.append("-",null);
choiceOperator.append("*",null);
choiceOperator.append("/",null);
choiceOperator.append("%",null);
calcForm.append(txtOperand1);
calcForm.append(choiceOperator);
calcForm.append(txtOperand2);
calcForm.append(txtResult);
}
public void startApp(){
if(display == null){
display = Display.getDisplay(this);
}
display.setCurrent(calcForm);
}
public void pauseApp(){
}
public void destroyApp(boolean unconditional){
}
public void commandAction(Command c, Displayable d){
if(c == commandExit){
destroyApp(true);
notifyDestroyed();
}
if(c == commandHitung){
double nilaiPertama = Double.parseDouble(txtOperand1.getString());
double nilaiKedua = Double.parseDouble(txtOperand2.getString());
hasil = 0;
int index = choiceOperator.getSelectedIndex();
String strOperator = choiceOperator.getString(index);
System.out.println(strOperator);
char operator = strOperator.charAt(0);
switch(operator){
case '+' : hasil = nilaiPertama + nilaiKedua; break;
case '-' : hasil = nilaiPertama - nilaiKedua; break;
case '*' : hasil = nilaiPertama * nilaiKedua; break;
case '/' : hasil = nilaiPertama / nilaiKedua; break;
case '%' : hasil = nilaiPertama % nilaiKedua; break;
}
txtResult.setString(String.valueOf(hasil));
}
if(c == commandReset){
hasil = 0;
txtOperand1.setString("");
txtOperand2.setString("");
txtResult.setString("");
}
}
}