Nothing Special   »   [go: up one dir, main page]

Tugas 11 (2201010014 - Ni Nyoman Aprianti)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

Tugas 11

Object Oriented Programming

Nama : Ni Nyoman Aprianti


NIM : 2201010014
Jurusan : TI-KAB

INSTIKI 2024
Soal:
1. Buatlah sebuah program kalkulator dengan menggunakan neatbean java dengan desain interface
seperti gambar dibawah ini:

✓ Sertakan penjelasan pada setiap baris sintak yang dideklarasikan dengan komentar.
✓ Dikumpulkan ke elsa dalam bentuk format PDF.

Jawaban:
1. Screenshoot Desain Kalkulator.java
2. Copy sintak Source kalkulator.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
*
* @author PC-A-30
*/
public class Swing extends javax.swing.JFrame {
private double firstNumber = 0;
private String operator = "";
private double num1 = 0;
private double num2 = 0;
private boolean startNewNumber = true;

/**
* Creates new form Swing
*/
public Swing() {
initComponents();
}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jLabel8 = new javax.swing.JLabel();


txtangka = new javax.swing.JTextField();
btn1 = new javax.swing.JButton();
btn2 = new javax.swing.JButton();
btn3 = new javax.swing.JButton();
btn4 = new javax.swing.JButton();
btn5 = new javax.swing.JButton();
btn6 = new javax.swing.JButton();
btn7 = new javax.swing.JButton();
btn8 = new javax.swing.JButton();
btn9 = new javax.swing.JButton();
btnnol = new javax.swing.JButton();
btntitik = new javax.swing.JButton();
btnbagi = new javax.swing.JButton();
btnkali = new javax.swing.JButton();
btnkurang = new javax.swing.JButton();
btntambah = new javax.swing.JButton();
btnsmdengan = new javax.swing.JButton();
btnreset = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jLabel8.setFont(new java.awt.Font("Nirmala UI", 1, 18)); // NOI18N


jLabel8.setText("KALKULATOR");

txtangka.setFont(new java.awt.Font("Nirmala UI Semilight", 1, 18)); // NOI18N


txtangka.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
txtangka.setText("0");
txtangka.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtangkaActionPerformed(evt);
}
});

btn1.setFont(new java.awt.Font("Nirmala UI Semilight", 1, 18)); // NOI18N


btn1.setText("1");
btn1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn1ActionPerformed(evt);
}
});

btn2.setFont(new java.awt.Font("Nirmala UI Semilight", 1, 18)); // NOI18N


btn2.setText("2");
btn2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn2ActionPerformed(evt);
}
});

btn3.setFont(new java.awt.Font("Nirmala UI Semilight", 1, 18)); // NOI18N


btn3.setText("3");
btn3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn3ActionPerformed(evt);
}
});

btn4.setFont(new java.awt.Font("Nirmala UI Semilight", 1, 18)); // NOI18N


btn4.setText("4");
btn4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn4ActionPerformed(evt);
}
});

btn5.setFont(new java.awt.Font("Nirmala UI Semilight", 1, 18)); // NOI18N


btn5.setText("5");
btn5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn5ActionPerformed(evt);
}
});
btn6.setFont(new java.awt.Font("Nirmala UI Semilight", 1, 18)); // NOI18N
btn6.setText("6");
btn6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn6ActionPerformed(evt);
}
});

btn7.setFont(new java.awt.Font("Nirmala UI Semilight", 1, 18)); // NOI18N


btn7.setText("7");
btn7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn7ActionPerformed(evt);
}
});

btn8.setFont(new java.awt.Font("Nirmala UI Semilight", 1, 18)); // NOI18N


btn8.setText("8");
btn8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn8ActionPerformed(evt);
}
});

btn9.setFont(new java.awt.Font("Nirmala UI Semilight", 1, 18)); // NOI18N


btn9.setText("9");
btn9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn9ActionPerformed(evt);
}
});

btnnol.setFont(new java.awt.Font("Nirmala UI Semilight", 1, 18)); // NOI18N


btnnol.setText("0");
btnnol.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnnolActionPerformed(evt);
}
});

btntitik.setFont(new java.awt.Font("Nirmala UI Semilight", 1, 18)); // NOI18N


btntitik.setText(".");
btntitik.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btntitikActionPerformed(evt);
}
});

btnbagi.setFont(new java.awt.Font("Nirmala UI Semilight", 1, 18)); // NOI18N


btnbagi.setText(":");
btnbagi.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnbagiActionPerformed(evt);
}
});

btnkali.setFont(new java.awt.Font("Nirmala UI Semilight", 1, 18)); // NOI18N


btnkali.setText("x");
btnkali.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnkaliActionPerformed(evt);
}
});

btnkurang.setFont(new java.awt.Font("Nirmala UI Semilight", 1, 18)); // NOI18N


btnkurang.setText("-");
btnkurang.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnkurangActionPerformed(evt);
}
});

btntambah.setFont(new java.awt.Font("Nirmala UI Semilight", 1, 18)); // NOI18N


btntambah.setText("+");
btntambah.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btntambahActionPerformed(evt);
}
});

btnsmdengan.setFont(new java.awt.Font("Nirmala UI Semilight", 1, 18)); // NOI18N


btnsmdengan.setText("=");
btnsmdengan.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnsmdenganActionPerformed(evt);
}
});
btnreset.setFont(new java.awt.Font("Nirmala UI Semilight", 1, 18)); // NOI18N
btnreset.setText("C");
btnreset.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnresetActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());


getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(16, 16, 16)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(btn7, javax.swing.GroupLayout.PREFERRED_SIZE, 52,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btn4, javax.swing.GroupLayout.PREFERRED_SIZE, 52,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btn1, javax.swing.GroupLayout.PREFERRED_SIZE, 52,
javax.swing.GroupLayout.PREFERRED_SIZE))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(btn5,
javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(btn6,
javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(btn2,
javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(btn3,
javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(35, 35, 35)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btnbagi,
javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnkali,
javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createSequentialGroup()
.addComponent(btn8, javax.swing.GroupLayout.PREFERRED_SIZE,
52, javax.swing.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(btn9, javax.swing.GroupLayout.PREFERRED_SIZE,
52, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(35, 35, 35)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btntambah,
javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnkurang,
javax.swing.GroupLayout.PREFERRED_SIZE, 52,
javax.swing.GroupLayout.PREFERRED_SIZE)))))
.addGroup(layout.createSequentialGroup()
.addComponent(btnnol, javax.swing.GroupLayout.PREFERRED_SIZE, 116,
javax.swing.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(btntitik, javax.swing.GroupLayout.PREFERRED_SIZE, 52,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(btnsmdengan, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addComponent(txtangka, javax.swing.GroupLayout.PREFERRED_SIZE, 204,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(btnreset, javax.swing.GroupLayout.PREFERRED_SIZE, 52,
javax.swing.GroupLayout.PREFERRED_SIZE))))
.addGroup(layout.createSequentialGroup()
.addGap(88, 88, 88)
.addComponent(jLabel8)))
.addContainerGap(16, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel8)
.addGap(18, 18, 18)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(btnreset, javax.swing.GroupLayout.DEFAULT_SIZE, 53,
Short.MAX_VALUE)
.addComponent(txtangka))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btn2, javax.swing.GroupLayout.PREFERRED_SIZE, 53,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btn1, javax.swing.GroupLayout.PREFERRED_SIZE, 53,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btn3, javax.swing.GroupLayout.PREFERRED_SIZE, 53,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnbagi, javax.swing.GroupLayout.PREFERRED_SIZE, 53,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btn4, javax.swing.GroupLayout.PREFERRED_SIZE, 53,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btn5, javax.swing.GroupLayout.PREFERRED_SIZE, 53,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btn6, javax.swing.GroupLayout.PREFERRED_SIZE, 53,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnkali, javax.swing.GroupLayout.PREFERRED_SIZE, 53,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btn7, javax.swing.GroupLayout.PREFERRED_SIZE, 53,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btn8, javax.swing.GroupLayout.PREFERRED_SIZE, 53,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btn9, javax.swing.GroupLayout.PREFERRED_SIZE, 53,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnkurang, javax.swing.GroupLayout.PREFERRED_SIZE, 53,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnnol, javax.swing.GroupLayout.PREFERRED_SIZE, 53,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btntitik, javax.swing.GroupLayout.PREFERRED_SIZE, 53,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btntambah, javax.swing.GroupLayout.PREFERRED_SIZE, 53,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(btnsmdengan, javax.swing.GroupLayout.PREFERRED_SIZE, 53,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(24, 24, 24))
);

pack();
}// </editor-fold>

private void btnsmdenganActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
double result = 0;
String secondNumberStr = txtangka.getText();

double secondNumber = Double.parseDouble(secondNumberStr);

switch (operator) {
case "+":
result = firstNumber + secondNumber;
break;
case "-":
result = firstNumber - secondNumber;
break;
case "*":
result = firstNumber * secondNumber;
break;
case "/":
if (secondNumber != 0) {
result = firstNumber / secondNumber;
} else {
txtangka.setText("Error: Division by zero");
return;
}
break;
default:
break;
}

txtangka.setText(String.valueOf(result));
}

private void btn2ActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
String input = txtangka.getText() + btn2.getText();
txtangka.setText(input);
}

private void btn1ActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
String input = txtangka.getText() + btn1.getText();
txtangka.setText(input);
}

private void btn3ActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
String input = txtangka.getText() + btn3.getText();
txtangka.setText(input);
}

private void btn4ActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
String input = txtangka.getText() + btn4.getText();
txtangka.setText(input);
}

private void btn5ActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
String input = txtangka.getText() + btn5.getText();
txtangka.setText(input);
}

private void btn6ActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
String input = txtangka.getText() + btn6.getText();
txtangka.setText(input);
}

private void btn7ActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
String input = txtangka.getText() + btn7.getText();
txtangka.setText(input);
}

private void btn8ActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
String input = txtangka.getText() + btn8.getText();
txtangka.setText(input);
}

private void btn9ActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
String input = txtangka.getText() + btn9.getText();
txtangka.setText(input);
}

private void btnnolActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
String input = txtangka.getText() + btnnol.getText();
txtangka.setText(input);
}

private void btntitikActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
String input = txtangka.getText() + btntitik.getText();
txtangka.setText(input);
}

private void btnbagiActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
firstNumber = Double.parseDouble(txtangka.getText());
operator = ":";
txtangka.setText("");
}
private void btnkaliActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
firstNumber = Double.parseDouble(txtangka.getText());
operator = "x";
txtangka.setText("");
}

private void btnkurangActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
firstNumber = Double.parseDouble(txtangka.getText());
operator = "-";
txtangka.setText("");
}

private void btntambahActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
firstNumber = Double.parseDouble(txtangka.getText());
operator = "+";
txtangka.setText("");
}

private void txtangkaActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
}

private void btnresetActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
txtangka.setText("0");
num1 = 0;
num2 = 0;
operator = "";
startNewNumber = true;
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see
http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {

java.util.logging.Logger.getLogger(Swing.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (InstantiationException ex) {

java.util.logging.Logger.getLogger(Swing.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (IllegalAccessException ex) {

java.util.logging.Logger.getLogger(Swing.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(Swing.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
}
//</editor-fold>

/* Create and display the form */


java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Swing().setVisible(true);
}
});
}

// Variables declaration - do not modify


private javax.swing.JButton btn1;
private javax.swing.JButton btn2;
private javax.swing.JButton btn3;
private javax.swing.JButton btn4;
private javax.swing.JButton btn5;
private javax.swing.JButton btn6;
private javax.swing.JButton btn7;
private javax.swing.JButton btn8;
private javax.swing.JButton btn9;
private javax.swing.JButton btnbagi;
private javax.swing.JButton btnkali;
private javax.swing.JButton btnkurang;
private javax.swing.JButton btnnol;
private javax.swing.JButton btnreset;
private javax.swing.JButton btnsmdengan;
private javax.swing.JButton btntambah;
private javax.swing.JButton btntitik;
private javax.swing.JLabel jLabel8;
private javax.swing.JTextField txtangka;
// End of variables declaration
}

3. Screenshot hasil run saat melakukan penjumlahan

You might also like