MEMBUAT KALKULATOR SEDERHANA
NAMA : MUHAMMAD FAHMI
NIM : 2060910131
KELAS : SI 2015 C
Untuk membuat kalkulator sederhana ini, kita menggunakan project yang sama
dengan program diatas. Maka, kita tidak perlu membuat project baru melainkan
bisa langsung membuat program didalam package.
1. Klik kanan pada package project Login, lalu pilih NEW
lalu pilih JFrame Form
2. Lalu pada New JFrame Form isikan nama class, yaitu Kalkulator. Lalu klik
Finish
3. Setelah itu akan muncul tampilan desain form. Lalu buat desain seperti
ini dengan menggunakan textfield dan button
button 1 ubah
Variable Name menjadi cmd1
button 2 ubah Variable Name menjadi cmd2
button 3 ubah Variable Name menjadi cmd3
button
4 ubah Variable Name menjadi cmd4
button 5 ubah
Variable Name menjadi cmd5
button 6 ubah Variable Name menjadi cmd6
button 7 ubah Variable Name menjadi cmd7
button 8 ubah
Variable Name menjadi cmd8
button 9 ubah Variable
Name menjadi cmd9
button 0 ubah Variable Name
menjadi cmd0
button Clear ubah Variable Name menjadi cmdclear
button = ubah Variable Name menjadi cmdsmdengan
button
+ ubah Variable Name menjadi cmdtambah
button - ubah
Variable Name menjadi cmdkurang
button * ubah
Variable Name menjadi cmdkali
button / ubah Variable Name menjadi cmdbagi
Masukan Syntak pada setiap button dengan cara klik kanan pada button kemudian pilih Events lalu pilih Action dan pilih actionPerformed
untuk button 1 :
String cmd1Text = txt1.getText() + cmd1.getText();
txt1.setText( cmd1Text );
untuk button 2 :
String cmd2Text = txt1.getText() + cmd2.getText();
txt1.setText( cmd2Text );
untuk button 3 :
String cmd3Text = txt1.getText() + cmd3.getText();
txt1.setText( cmd3Text );
untk button 4 :
String cmd4Text = txt1.getText() + cmd4.getText();
txt1.setText( cmd4Text );
untuk button 5 :
String cmd5Text = txt1.getText() + cmd5.getText();
txt1.setText( cmd5Text );
untuk button 6 :
String cmd6Text = txt1.getText() + cmd6.getText();
txt1.setText( cmd6Text );
untuk button 7 :
String cmd7Text = txt1.getText() + cmd7.getText();
txt1.setText( cmd7Text );
untuk button 8 :
String cmd8Text = txt1.getText() + cmd8.getText();
txt1.setText( cmd8Text );
untuk button 9 :
String cmd9Text = txt1.getText() + cmd9.getText();
txt1.setText( cmd9Text );
untuk button 0 :
String cmd0Text = txt1.getText() + cmd0.getText();
txt1.setText( cmd0Text );
String cmd1Text = txt1.getText() + cmd1.getText();
txt1.setText( cmd1Text );
untuk button 2 :
String cmd2Text = txt1.getText() + cmd2.getText();
txt1.setText( cmd2Text );
untuk button 3 :
String cmd3Text = txt1.getText() + cmd3.getText();
txt1.setText( cmd3Text );
untk button 4 :
String cmd4Text = txt1.getText() + cmd4.getText();
txt1.setText( cmd4Text );
untuk button 5 :
String cmd5Text = txt1.getText() + cmd5.getText();
txt1.setText( cmd5Text );
untuk button 6 :
String cmd6Text = txt1.getText() + cmd6.getText();
txt1.setText( cmd6Text );
untuk button 7 :
String cmd7Text = txt1.getText() + cmd7.getText();
txt1.setText( cmd7Text );
untuk button 8 :
String cmd8Text = txt1.getText() + cmd8.getText();
txt1.setText( cmd8Text );
untuk button 9 :
String cmd9Text = txt1.getText() + cmd9.getText();
txt1.setText( cmd9Text );
untuk button 0 :
String cmd0Text = txt1.getText() + cmd0.getText();
txt1.setText( cmd0Text );
Lalu untuk mendeklarasikan syntak
pada tombol clear kalian masukan tepat di bawa
public
class KalkulatorJava extends javax.swing.JFrame {
private
double total1 = 0.0;
private double total2 = 0.0;
private char math_operator;
private double total2 = 0.0;
private char math_operator;
}
private void
getOperator(String btnText){
math_operator = btnText.charAt(0);
total1 = total1 + Double.parseDouble(text1.getText());
text1.setText("");
total1 = total1 + Double.parseDouble(text1.getText());
text1.setText("");
}
- Ketikan syntak untuk button clear seperti ini
untuk button "Clear "
total2 = 0;
text1.setText("")
total2 = 0;
text1.setText("")
- Ketikan syntak untuk button "= "
switch
(math_operator){
case '+':
total2 = total1 + Double.parseDouble(text1.getText( ) );
break;
case '-':
total2 = total1 - Double.parseDouble(text1.getText( ) );
break;
case '*':
total2 = total1 * Double.parseDouble(text1.getText( ) );
break;
case '/':
total2 = total1 / Double.parseDouble(text1.getText( ) );
break;
}
text1.setText( Double.toString(total2) );
total1 = 0;
case '+':
total2 = total1 + Double.parseDouble(text1.getText( ) );
break;
case '-':
total2 = total1 - Double.parseDouble(text1.getText( ) );
break;
case '*':
total2 = total1 * Double.parseDouble(text1.getText( ) );
break;
case '/':
total2 = total1 / Double.parseDouble(text1.getText( ) );
break;
}
text1.setText( Double.toString(total2) );
total1 = 0;
- Lalu untuk button "+"
String
button_text = cmdtambah.getText();
getOperator(button_text);
getOperator(button_text);
- Lalu untuk button "-"
String
button_text = cmdkurang.getText();
getOperator(button_text);
getOperator(button_text);
- untuk button "*"
String
button_text = cmdkali.getText();
getOperator(button_text);
getOperator(button_text);
- dan terakhir untuk button " / "
String button_text = cmdbagi.getText();
getOperator(button_text);
demikian tugas praktikum bahasa pemrograman 2 membuat kalkulator sederhana
Terima Kasih










Komentar
Posting Komentar