Java adalah nama untuk sekumpulan teknologi untuk membuat dan menjalankan perangkat lunak pada komputer standalone ataupun pada lingkungan jaringan. Kita lebih menyukai menyebut Java sebagai sebuah teknologi dibanding hanya sebuah bahasa pemrograman, karena Java lebih lengkap dibanding sebuah bahasa pemrograman konvensional. Berikut ini saya akan memberikan tutorial membuat kalkulator dengan java yang dibantu oleh software NetBeans 6.5
1. Buka NetBeans 6.5 ataupun versi lainnya
2. Buatlah project dengan cara klik file-new project
3. Pilihlah java application lalu klik next
4. Lalu beri nama project tersebut sesuai dengan keinginan anda
5. Buatlah file java dengan cara klik file-new file lalu pilihlah Swing GUI Froms sebagai katagorinya dan JFrame From sebagai type dari filenya.
6. Buatlah tampilan seperti ini pada form yang anda buat tadi
Radiobutton untuk tombol degrees dan radian
Checkbox untuk Inv dan Hyp
Button untuk angka-angka dan perintah pada kalkulator
Textfield untuk tampilan hasil kalkulator
7. Ketikkan script berikut ini pada bagian source
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* Calculator.java
*
* Created on Nov 17, 2012, 1:44:18 PM
*/
package keren;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
/**
*
* @author Puspa
*/
public class Calculator extends javax.swing.JFrame {
String angka="";
Double total,angka1,tampung;
int kurung;
boolean TKpress,btotal;
private Double comma;
/** Creates new form Calculator */
public Calculator(){
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (ClassNotFoundException ex) {
Logger.getLogger(Calculator.class.getName()).log(Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
Logger.getLogger(Calculator.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(Calculator.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnsupportedLookAndFeelException ex) {
Logger.getLogger(Calculator.class.getName()).log(Level.SEVERE, null, ex);
}
initComponents();
angka="";
}
Prioritas priority = new Prioritas();
String infixExp = "", postfixExp = "";
/** 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() {
buttonGroup1 = new javax.swing.ButtonGroup();
tampil = new javax.swing.JTextField();
cmd1 = new javax.swing.JButton();
cmd4 = new javax.swing.JButton();
cmd7 = new javax.swing.JButton();
cmdplus_minus = new javax.swing.JButton();
cmd2 = new javax.swing.JButton();
cmd5 = new javax.swing.JButton();
cmd8 = new javax.swing.JButton();
cmd0 = new javax.swing.JButton();
cmd3 = new javax.swing.JButton();
cmd6 = new javax.swing.JButton();
cmd9 = new javax.swing.JButton();
cmdcomma = new javax.swing.JButton();
cmdphi = new javax.swing.JButton();
cmdbackspace = new javax.swing.JButton();
cmdc = new javax.swing.JButton();
cmddeg = new javax.swing.JRadioButton();
cmdrad = new javax.swing.JRadioButton();
cmdsin = new javax.swing.JButton();
cmdcos = new javax.swing.JButton();
cmdtan = new javax.swing.JButton();
cmdlog = new javax.swing.JButton();
cmdxpowy = new javax.swing.JButton();
cmdxpow3 = new javax.swing.JButton();
cmdxpow2 = new javax.swing.JButton();
cmd1devidebyx = new javax.swing.JButton();
cmdakar = new javax.swing.JButton();
cmdlen = new javax.swing.JButton();
cmdsamadengan = new javax.swing.JButton();
cmdkali = new javax.swing.JButton();
cmdbagi = new javax.swing.JButton();
cmdtambah = new javax.swing.JButton();
cmdkurang = new javax.swing.JButton();
cmdinv = new javax.swing.JCheckBox();
cmdhyp = new javax.swing.JCheckBox();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Kalkulator Tugas Besar Puspa");
tampil.setEditable(false);
tampil.setFont(new java.awt.Font("Arial", 1, 14));
tampil.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
cmd1.setFont(new java.awt.Font("Arial", 1, 14));
cmd1.setForeground(new java.awt.Color(204, 0, 0));
cmd1.setText("1");
cmd1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmd1ActionPerformed(evt);
}
});
cmd4.setFont(new java.awt.Font("Arial", 1, 14));
cmd4.setForeground(new java.awt.Color(204, 0, 0));
cmd4.setText("4");
cmd4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmd4ActionPerformed(evt);
}
});
cmd7.setFont(new java.awt.Font("Arial", 1, 14));
cmd7.setForeground(new java.awt.Color(204, 0, 0));
cmd7.setText("7");
cmd7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmd7ActionPerformed(evt);
}
});
cmdplus_minus.setFont(new java.awt.Font("Arial", 1, 14));
cmdplus_minus.setForeground(new java.awt.Color(153, 0, 153));
cmdplus_minus.setText("+/-");
cmdplus_minus.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmdplus_minusActionPerformed(evt);
}
});
cmd2.setFont(new java.awt.Font("Arial", 1, 14));
cmd2.setForeground(new java.awt.Color(204, 0, 0));
cmd2.setText("2");
cmd2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmd2ActionPerformed(evt);
}
});
cmd5.setFont(new java.awt.Font("Arial", 1, 14));
cmd5.setForeground(new java.awt.Color(204, 0, 0));
cmd5.setText("5");
cmd5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmd5ActionPerformed(evt);
}
});
cmd8.setFont(new java.awt.Font("Arial", 1, 14));
cmd8.setForeground(new java.awt.Color(204, 0, 0));
cmd8.setText("8");
cmd8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmd8ActionPerformed(evt);
}
});
cmd0.setFont(new java.awt.Font("Arial", 1, 14));
cmd0.setForeground(new java.awt.Color(204, 0, 0));
cmd0.setText("0");
cmd0.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmd0ActionPerformed(evt);
}
});
cmd3.setFont(new java.awt.Font("Arial", 1, 14));
cmd3.setForeground(new java.awt.Color(204, 0, 0));
cmd3.setText("3");
cmd3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmd3ActionPerformed(evt);
}
});
cmd6.setFont(new java.awt.Font("Arial", 1, 14));
cmd6.setForeground(new java.awt.Color(204, 0, 0));
cmd6.setText("6");
cmd6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmd6ActionPerformed(evt);
}
});
cmd9.setFont(new java.awt.Font("Arial", 1, 14));
cmd9.setForeground(new java.awt.Color(204, 0, 0));
cmd9.setText("9");
cmd9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmd9ActionPerformed(evt);
}
});
cmdcomma.setFont(new java.awt.Font("Arial", 1, 18));
cmdcomma.setForeground(new java.awt.Color(153, 0, 153));
cmdcomma.setText(".");
cmdcomma.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmdcommaActionPerformed(evt);
}
});
cmdphi.setFont(new java.awt.Font("Times New Roman", 1, 18));
cmdphi.setForeground(new java.awt.Color(0, 0, 204));
cmdphi.setText("<html>&pi</html>");
cmdphi.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmdphiActionPerformed(evt);
}
});
cmdbackspace.setFont(new java.awt.Font("Arial", 1, 18));
cmdbackspace.setForeground(new java.awt.Color(153, 0, 153));
cmdbackspace.setText("<html>&larr</html>");
cmdbackspace.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmdbackspaceActionPerformed(evt);
}
});
cmdc.setFont(new java.awt.Font("Arial", 1, 14));
cmdc.setForeground(new java.awt.Color(153, 0, 153));
cmdc.setText("C");
cmdc.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmdcActionPerformed(evt);
}
});
cmddeg.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
cmddeg.setForeground(new java.awt.Color(0, 153, 0));
cmddeg.setText("Degrees");
cmdrad.setFont(new java.awt.Font("Arial", 0, 12));
cmdrad.setForeground(new java.awt.Color(0, 153, 0));
cmdrad.setText("Radians");
cmdrad.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmdradActionPerformed(evt);
}
});
cmdsin.setFont(new java.awt.Font("Arial", 1, 14));
cmdsin.setForeground(new java.awt.Color(0, 0, 204));
cmdsin.setText("Sin");
cmdsin.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmdsinActionPerformed(evt);
}
});
cmdcos.setFont(new java.awt.Font("Arial", 1, 14));
cmdcos.setForeground(new java.awt.Color(0, 0, 204));
cmdcos.setText("Cos");
cmdcos.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmdcosActionPerformed(evt);
}
});
cmdtan.setFont(new java.awt.Font("Arial", 1, 14));
cmdtan.setForeground(new java.awt.Color(0, 0, 204));
cmdtan.setText("Tan");
cmdtan.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmdtanActionPerformed(evt);
}
});
cmdlog.setFont(new java.awt.Font("Arial", 1, 14));
cmdlog.setForeground(new java.awt.Color(0, 0, 204));
cmdlog.setText("Log");
cmdlog.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmdlogActionPerformed(evt);
}
});
cmdxpowy.setFont(new java.awt.Font("Arial", 1, 14));
cmdxpowy.setForeground(new java.awt.Color(0, 0, 204));
cmdxpowy.setText("<html>x<SUP>y</SUP><html>");
cmdxpowy.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmdxpowyActionPerformed(evt);
}
});
cmdxpow3.setFont(new java.awt.Font("Arial", 1, 14));
cmdxpow3.setForeground(new java.awt.Color(0, 0, 204));
cmdxpow3.setText("<html>x<SUP>3</SUP><html>");
cmdxpow3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmdxpow3ActionPerformed(evt);
}
});
cmdxpow2.setFont(new java.awt.Font("Arial", 1, 14));
cmdxpow2.setForeground(new java.awt.Color(0, 0, 204));
cmdxpow2.setText("<html>x<SUP>2</SUP><html>");
cmdxpow2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmdxpow2ActionPerformed(evt);
}
});
cmd1devidebyx.setFont(new java.awt.Font("Arial", 1, 14));
cmd1devidebyx.setForeground(new java.awt.Color(0, 0, 204));
cmd1devidebyx.setText("1/x");
cmd1devidebyx.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmd1devidebyxActionPerformed(evt);
}
});
cmdakar.setFont(new java.awt.Font("Arial", 1, 14));
cmdakar.setForeground(new java.awt.Color(0, 0, 204));
cmdakar.setText("<html>√x</html>");
cmdakar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmdakarActionPerformed(evt);
}
});
cmdlen.setFont(new java.awt.Font("Arial", 1, 14));
cmdlen.setForeground(new java.awt.Color(0, 0, 204));
cmdlen.setText("ln");
cmdlen.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmdlenActionPerformed(evt);
}
});
cmdsamadengan.setFont(new java.awt.Font("Arial", 1, 14));
cmdsamadengan.setForeground(new java.awt.Color(153, 0, 153));
cmdsamadengan.setText("=");
cmdsamadengan.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmdsamadenganActionPerformed(evt);
}
});
cmdkali.setFont(new java.awt.Font("Arial", 1, 18));
cmdkali.setForeground(new java.awt.Color(0, 0, 204));
cmdkali.setText("*");
cmdkali.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmdkaliActionPerformed(evt);
}
});
cmdbagi.setFont(new java.awt.Font("Arial", 1, 18));
cmdbagi.setForeground(new java.awt.Color(0, 0, 204));
cmdbagi.setText("/");
cmdbagi.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmdbagiActionPerformed(evt);
}
});
cmdtambah.setFont(new java.awt.Font("Arial", 1, 18));
cmdtambah.setForeground(new java.awt.Color(0, 0, 204));
cmdtambah.setText("+");
cmdtambah.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmdtambahActionPerformed(evt);
}
});
cmdkurang.setFont(new java.awt.Font("Arial", 1, 18));
cmdkurang.setForeground(new java.awt.Color(0, 0, 204));
cmdkurang.setText("-");
cmdkurang.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmdkurangActionPerformed(evt);
}
});
cmdinv.setFont(new java.awt.Font("Arial", 0, 12));
cmdinv.setForeground(new java.awt.Color(102, 102, 0));
cmdinv.setText("Inv");
cmdhyp.setFont(new java.awt.Font("Arial", 0, 12));
cmdhyp.setForeground(new java.awt.Color(102, 102, 0));
cmdhyp.setText("Hyp");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(40, 40, 40)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(tampil, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 495, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(cmdinv)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(cmdhyp))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(cmdplus_minus, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(cmd7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(cmd4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(cmd1, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(cmd0, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(cmd8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(cmd5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(cmd2, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(cmdcomma, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(cmd9, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(cmd6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(cmd3, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addGap(31, 31, 31)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(cmdakar, javax.swing.GroupLayout.DEFAULT_SIZE, 67, Short.MAX_VALUE)
.addComponent(cmdxpowy, javax.swing.GroupLayout.DEFAULT_SIZE, 67, Short.MAX_VALUE)
.addComponent(cmdsin, javax.swing.GroupLayout.DEFAULT_SIZE, 67, Short.MAX_VALUE)
.addComponent(cmdkali, javax.swing.GroupLayout.DEFAULT_SIZE, 67, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(cmdlen, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(cmdxpow2)
.addComponent(cmdcos, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(cmdbagi, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addComponent(cmdbackspace, javax.swing.GroupLayout.DEFAULT_SIZE, 140, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(cmdxpow3)
.addComponent(cmdtan, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(cmdtambah, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(cmdc, javax.swing.GroupLayout.DEFAULT_SIZE, 64, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(cmdkurang, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(cmdlog, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(cmd1devidebyx, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(cmdphi, javax.swing.GroupLayout.DEFAULT_SIZE, 66, Short.MAX_VALUE)))
.addComponent(cmdsamadengan, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(cmddeg)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(cmdrad)))
.addGap(43, 43, 43))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(36, 36, 36)
.addComponent(tampil, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(cmdc, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(cmdphi, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(cmdbackspace, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(cmdkurang, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(cmdbagi, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(cmdtambah, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(cmdkali, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(cmdcos, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(cmdsin, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(cmdtan, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(cmdlog, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(cmdxpowy, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(cmdxpow3, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(cmdxpow2, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(cmd1devidebyx, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(cmdakar, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(cmdlen, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(cmdsamadengan, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(cmddeg)
.addComponent(cmdrad))
.addGap(3, 3, 3)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(cmdinv)
.addComponent(cmdhyp))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addComponent(cmd1, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(cmd4, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(cmd7, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(cmdplus_minus, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addComponent(cmd2, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(cmd5, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(cmd8, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(cmd0, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(cmd3, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(cmd6, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(cmd9, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(cmdcomma, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))))))
.addContainerGap(51, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void cmdxpow3ActionPerformed(java.awt.event.ActionEvent evt) {
String pow_3 = tampil.getText();
if(pow_3.isEmpty()){
pow_3+="0";
tampil.setText(pow_3);
pow_3="";
}else{
double pw = Double.parseDouble(pow_3);
double hasil = Math.pow(pw,3);
tampil.setText(Double.toString(hasil));
if(cmdinv.isSelected()==true){
hasil = Math.pow(pw,1/3.0);
tampil.setText(Double.toString(hasil));
cmdinv.setSelected(false);
}
}
}
private void cmdxpow2ActionPerformed(java.awt.event.ActionEvent evt) {
String pow_2 = tampil.getText();
if(pow_2.isEmpty()){
pow_2+="0";
tampil.setText(pow_2);
pow_2="";
}else{
double pw = Double.parseDouble(pow_2);
double hasil = Math.pow(pw,2);
tampil.setText(Double.toString(hasil));
if(cmdinv.isSelected()==true){
hasil = Math.pow(pw, 0.5);
tampil.setText(Double.toString(hasil));
cmdinv.setSelected(false);
}
}
}
private void cmdcActionPerformed(java.awt.event.ActionEvent evt) {
angka1 = 0.0;
total = 0.0;
angka = "";
tampil.setText("");
}
private void cmdphiActionPerformed(java.awt.event.ActionEvent evt) {
double hasil = Math.PI;
tampil.setText(Double.toString(hasil));
}
private void cmd1ActionPerformed(java.awt.event.ActionEvent evt) {
angka+="1";
tampil.setText(angka);
}
private void cmd2ActionPerformed(java.awt.event.ActionEvent evt) {
angka+="2";
tampil.setText(angka);
}
private void cmd3ActionPerformed(java.awt.event.ActionEvent evt) {
angka+="3";
tampil.setText(angka);
}
private void cmd4ActionPerformed(java.awt.event.ActionEvent evt) {
angka+="4";
tampil.setText(angka);
}
private void cmd5ActionPerformed(java.awt.event.ActionEvent evt) {
angka+="5";
tampil.setText(angka);
}
private void cmd6ActionPerformed(java.awt.event.ActionEvent evt) {
angka+="6";
tampil.setText(angka);
}
private void cmd7ActionPerformed(java.awt.event.ActionEvent evt) {
angka+="7";
tampil.setText(angka);
}
private void cmd8ActionPerformed(java.awt.event.ActionEvent evt) {
angka+="8";
tampil.setText(angka);
}
private void cmd9ActionPerformed(java.awt.event.ActionEvent evt) {
angka+="9";
tampil.setText(angka);
}
private void cmd0ActionPerformed(java.awt.event.ActionEvent evt) {
angka+="0";
tampil.setText(angka);
}
private void cmdtambahActionPerformed(java.awt.event.ActionEvent evt) {
if(tampil.getText().isEmpty()){
tampil.setText("0");
}else{
if(TKpress==true){
infixExp+=" + ";
}
else{
angka1=Double.parseDouble(tampil.getText());
infixExp+=angka1+" + ";
}
angka = "";
}
}
private void cmdkurangActionPerformed(java.awt.event.ActionEvent evt) {
if(tampil.getText().isEmpty()){
tampil.setText("0");
}else{
if(TKpress==true){
infixExp+=" – ";
}
else{
angka1=Double.parseDouble(tampil.getText());
infixExp+=angka1+" – ";
}
angka = "";
}
}
private void cmdkaliActionPerformed(java.awt.event.ActionEvent evt) {
if(tampil.getText().isEmpty()){
tampil.setText("0");
}else{
if(TKpress==true){
infixExp+=" * ";
}
else{
angka1=Double.parseDouble(tampil.getText());
infixExp+=angka1+" * ";
}
angka = "";
}
}
private void cmdbagiActionPerformed(java.awt.event.ActionEvent evt) {
if(tampil.getText().isEmpty()){
tampil.setText("0");
}else{
if(TKpress==true){
infixExp+=" / ";
}else{
angka1=Double.parseDouble(tampil.getText());
infixExp+=angka1+" / ";
}
angka = "";
}
}
private void cmdsamadenganActionPerformed(java.awt.event.ActionEvent evt) {
if((kurung==0)&&(!tampil.getText().equals(")"))){
angka1=Double.parseDouble(tampil.getText());
infixExp+=angka1;
}
try{
postfixExp = priority.convertToPostfix(infixExp);
total=priority.evaluate(postfixExp);
}catch(Exception e){
}tampil.setText(""+(total));
btotal=true;
infixExp="";
System.out.println(""+infixExp);
TKpress=false;
angka="";
}
private void cmdsinActionPerformed(java.awt.event.ActionEvent evt) {
String sin = tampil.getText();
if(sin.isEmpty()){
tampil.setText("0");
}else{
double derajat = Double.parseDouble(sin);
double pi = Math.PI;
//dalam bentuk degrees//
double hasil = Math.sin(derajat * pi/180);
tampil.setText(Double.toString(hasil));
if(derajat%180==0){
tampil.setText("0.0");
}
if(cmdhyp.isSelected()==true&&cmdinv.isSelected()==false){
hasil = Math.sinh(derajat);
tampil.setText(Double.toString(hasil));
cmdhyp.setSelected(false);
}
if(cmdinv.isSelected()==true&&cmdhyp.isSelected()==false){
hasil = Math.asin(derajat)*180/pi;
tampil.setText(Double.toString(hasil));
if(derajat==1){
tampil.setText("90");
}
cmdinv.setSelected(false);
}
if(cmdinv.isSelected()==true && cmdhyp.isSelected()==true){
double proseshasil =Math.pow(derajat, 2)+1.0;
double proseshasil2 = Math.sqrt(proseshasil);
hasil = Math.log(derajat+proseshasil2);
tampil.setText(Double.toString(hasil));
cmdhyp.setSelected(false);
cmdinv.setSelected(false);
}
//dalam bentuk radian//
if(cmdrad.isSelected()==true){
hasil = Math.sin(derajat);
tampil.setText(Double.toString(hasil));
}
if(cmdrad.isSelected()==true && cmdhyp.isSelected()==true && cmdinv.isSelected()==false){
hasil = Math.sinh(derajat);
tampil.setText(Double.toString(hasil));
cmdhyp.setSelected(false);
}
if(cmdrad.isSelected()==true && cmdinv.isSelected()==true && cmdhyp.isSelected()==false){
hasil = Math.asin(derajat);
tampil.setText(Double.toString(hasil));
cmdinv.setSelected(false);
}
if(cmdrad.isSelected()==true && cmdinv.isSelected()==true && cmdhyp.isSelected()==true){
double proseshasil =Math.pow(derajat, 2)+1.0;
double proseshasil2 = Math.sqrt(proseshasil);
hasil = Math.log(derajat+proseshasil2);
tampil.setText(Double.toString(hasil));
cmdinv.setSelected(false);
cmdhyp.setSelected(false);
}
}
}
private void cmdbackspaceActionPerformed(java.awt.event.ActionEvent evt) {
angka = angka.substring(0, angka.length()-1);
tampil.setText(angka);
}
private void cmdplus_minusActionPerformed(java.awt.event.ActionEvent evt) {
if(!angka.contains("-"))
angka = "-"+angka;
else
angka = angka.substring(1, angka.length());
tampil.setText(angka);
}
private void cmdcosActionPerformed(java.awt.event.ActionEvent evt) {
String cos = tampil.getText();
double derajat = Double.parseDouble(cos);
double pi = Math.PI;
//dalam bentuk degrees//
double hasil = Math.cos(derajat * pi/180);
tampil.setText(Double.toString(hasil));
if(derajat ==90 ){
tampil.setText("0");
}
if(derajat%270==0){
tampil.setText("0");
}
if(cmdhyp.isSelected()==true && cmdinv.isSelected()==false){
hasil = Math.cosh(derajat);
tampil.setText(Double.toString(hasil));
cmdhyp.setSelected(false);
}
if(cmdinv.isSelected()==true && cmdhyp.isSelected()==false){
hasil = Math.acos(derajat)*180/pi;
tampil.setText(Double.toString(hasil));
cmdinv.setSelected(false);
}
if(cmdinv.isSelected()==true && cmdhyp.isSelected()==true){
double proseshasil =Math.pow(derajat, 2)-1.0;
double proseshasil2 = Math.sqrt(proseshasil);
hasil = Math.log(derajat+proseshasil2);
tampil.setText(Double.toString(hasil));
cmdhyp.setSelected(false);
cmdinv.setSelected(false);
}
//dalam bentuk radian//
if(cmdrad.isSelected()==true){
hasil = Math.cos(derajat);
tampil.setText(Double.toString(hasil));
}
if(cmdrad.isSelected()==true && cmdhyp.isSelected()==true && cmdinv.isSelected()==false){
hasil = Math.cosh(derajat);
tampil.setText(Double.toString(hasil));
cmdhyp.setSelected(false);
}
if(cmdrad.isSelected()==true && cmdinv.isSelected()==true && cmdhyp.isSelected()==false){
hasil = Math.acos(derajat);
tampil.setText(Double.toString(hasil));
cmdinv.setSelected(false);
}
if(cmdrad.isSelected()==true && cmdinv.isSelected()==true && cmdhyp.isSelected()==true){
double proseshasil =Math.pow(derajat, 2)-1.0;
double proseshasil2 = Math.sqrt(proseshasil);
hasil = Math.log(derajat+proseshasil2);
tampil.setText(Double.toString(hasil));
cmdinv.setSelected(false);
cmdhyp.setSelected(false);
}
}
private void cmdcommaActionPerformed(java.awt.event.ActionEvent evt) {
angka +=".";
setComma(Double.parseDouble(angka));
}
private void cmdtanActionPerformed(java.awt.event.ActionEvent evt) {
String tan = tampil.getText();
if(tan.isEmpty()){
tan+="0";
tampil.setText(tan);
tan="";
}else{
double derajat = Double.parseDouble(tan);
double pi = Math.PI;
//dalam bentuk degrees//
double hasil = Math.tan(derajat * pi/180);
tampil.setText(Double.toString(hasil));
if(derajat%90==0 || derajat%270==0){
tampil.setText("Math ERROR");
}
if(derajat%180==0|| derajat%360==0){
tampil.setText("0");
}
if(derajat ==45 || derajat % 225==0){
tampil.setText("1.0");
}
if(derajat%135==0||derajat%315==0){
tampil.setText("-1.0");
}
if(cmdhyp.isSelected()==true && cmdinv.isSelected()==false){
hasil = Math.tanh(derajat);
tampil.setText(Double.toString(hasil));
cmdhyp.setSelected(false);
}
if(cmdinv.isSelected()==true && cmdhyp.isSelected()==false){
hasil = Math.atan(derajat)*180/pi;
tampil.setText(Double.toString(hasil));
cmdinv.setSelected(false);
}
//dalam bentuk radian//
if(cmdrad.isSelected()==true){
hasil = Math.tan(derajat);
tampil.setText(Double.toString(hasil));
}
if(cmdrad.isSelected()==true && cmdhyp.isSelected()==true && cmdinv.isSelected()==false){
hasil = Math.tanh(derajat);
tampil.setText(Double.toString(hasil));
cmdhyp.setSelected(false);
}
if(cmdrad.isSelected()==true && cmdinv.isSelected()==true && cmdhyp.isSelected()==false){
hasil = Math.atan(derajat);
tampil.setText(Double.toString(hasil));
cmdinv.setSelected(false);
}
}
}
private void cmd1devidebyxActionPerformed(java.awt.event.ActionEvent evt) {
String per = tampil.getText();
if(per.isEmpty()){
per+="0";
tampil.setText(per);
per="";
}else{
double perperan = Double.parseDouble(per);
double hasil = 1/perperan;
tampil.setText(Double.toString(hasil));
}
}
private void cmdlogActionPerformed(java.awt.event.ActionEvent evt) {
String logaritma = tampil.getText();
if(logaritma.isEmpty()){
logaritma+="0";
tampil.setText(logaritma);
logaritma="";
}else{
double a = Double.parseDouble(logaritma);
double hasil = Math.log10(a);
tampil.setText(Double.toString(hasil));
if(a<0){
tampil.setText("Math ERROR");
}
if(cmdinv.isSelected()==true){
hasil = Math.pow(10.0, a);
double pembulatan = Math.round(hasil);
tampil.setText(Double.toString(pembulatan));
cmdinv.setSelected(false);
}
}
}
private void cmdlenActionPerformed(java.awt.event.ActionEvent evt) {
String ln = tampil.getText();
if(ln.isEmpty()){
ln+="0";
tampil.setText(ln);
ln="";
}else{
double b = Double.parseDouble(ln);
double hasil = Math.log(b);
tampil.setText(Double.toString(hasil));
if(b<0){
tampil.setText("Math ERROR");
}
if(cmdinv.isSelected()==true){
hasil = Math.pow((Math.E),b);
double pembulatan = Math.round(hasil);
tampil.setText(Double.toString(pembulatan));
cmdinv.setSelected(false);
}
}
}
private void cmdxpowyActionPerformed(java.awt.event.ActionEvent evt) {
if(TKpress==true){
infixExp+=" ^ ";
if(cmdinv.isSelected()==true){
infixExp+=" ^ 1 / ";
}
}
else{
angka1=Double.parseDouble(tampil.getText());
infixExp+=angka1+" ^ ";
if(cmdinv.isSelected()==true){
angka1=Double.parseDouble(tampil.getText());
infixExp+=angka1+" ^ 1 / ";
}
}angka = "";
}
private void cmdakarActionPerformed(java.awt.event.ActionEvent evt) {
String akar = tampil.getText();
if(akar.isEmpty()){
akar+="0";
tampil.setText(akar);
akar="";
}else{
double root = Double.parseDouble(akar);
double hasil = Math.sqrt(root);
tampil.setText(Double.toString(hasil));
if(cmdinv.isSelected()==true){
hasil = Math.sqrt(root);
tampil.setText(Double.toString(hasil));
cmdinv.setSelected(false);
}
}
}
private void cmdradActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new Calculator().setVisible(true);
}
});
}
public void setComma(Double comma) {
this.comma = comma;
}
public Double getComma() {
return comma;
}
// Variables declaration - do not modify
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.JButton cmd0;
private javax.swing.JButton cmd1;
private javax.swing.JButton cmd1devidebyx;
private javax.swing.JButton cmd2;
private javax.swing.JButton cmd3;
private javax.swing.JButton cmd4;
private javax.swing.JButton cmd5;
private javax.swing.JButton cmd6;
private javax.swing.JButton cmd7;
private javax.swing.JButton cmd8;
private javax.swing.JButton cmd9;
private javax.swing.JButton cmdakar;
private javax.swing.JButton cmdbackspace;
private javax.swing.JButton cmdbagi;
private javax.swing.JButton cmdc;
private javax.swing.JButton cmdcomma;
private javax.swing.JButton cmdcos;
private javax.swing.JRadioButton cmddeg;
private javax.swing.JCheckBox cmdhyp;
private javax.swing.JCheckBox cmdinv;
private javax.swing.JButton cmdkali;
private javax.swing.JButton cmdkurang;
private javax.swing.JButton cmdlen;
private javax.swing.JButton cmdlog;
private javax.swing.JButton cmdphi;
private javax.swing.JButton cmdplus_minus;
private javax.swing.JRadioButton cmdrad;
private javax.swing.JButton cmdsamadengan;
private javax.swing.JButton cmdsin;
private javax.swing.JButton cmdtambah;
private javax.swing.JButton cmdtan;
private javax.swing.JButton cmdxpow2;
private javax.swing.JButton cmdxpow3;
private javax.swing.JButton cmdxpowy;
private javax.swing.JTextField tampil;
// End of variables declaration
}
8. Buatlah class java dengan cara klik file-new file dan beri nama interface kalkulator
9. Ketikkan script berikut
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package keren;
/**
*
* @author Puspa
*/
public interface InterfaceCalculator {
public boolean isOperator(String token);
public boolean isOperand(String token);
}
10. Buatlah class java dengan cara klik file-new file dan beri nama prioritas
11. Ketikkan script berikut
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package keren;
/**
*
* @author Puspa
*/
import java.util.Stack;
import java.util.StringTokenizer;
public class Prioritas implements InterfaceCalculator{
private Stack<Double> stackOfOperand;
private Stack<Character> stackOfOperator;
public Prioritas() {
stackOfOperator = new Stack<Character>();
stackOfOperand = new Stack<Double>();
}
@Override
public boolean isOperator(String token) {
return (token.equalsIgnoreCase("^") || token.equalsIgnoreCase("*") || token.equalsIgnoreCase("/") ||
token.equalsIgnoreCase("+") || token.equalsIgnoreCase("-"));
}
@Override
public boolean isOperand(String token) {
//sudah menangani kasus bilangan negatif
return (!isOperator(token) && ((Character.isDigit(token.charAt(0))) || (token.charAt(0) == '-')));
}
//mengembalikan prioritas operator saat evaluasi
public int skala_prioritas(char tanda) {
int skala;
switch (tanda) {
case '^':{
skala = 3;
break;
}
case '*':{
skala = 2;
break;
}
case '/':{
skala = 2;
break;
}
case '+':{
skala = 1;
break;
}
case '-':{
skala = 1;
break;
}
default:{
skala = 0;
break;
}
}
return skala;
}
public String convertToPostfix(String infixExp) throws Exception, PrioritasException {
StringTokenizer st = new StringTokenizer(infixExp);
String curToken = "", postfixExp = "";
int nKurungBuka = 0, nKurungTutup = 0;
while(st.hasMoreTokens()) {
//mengambil token
curToken = st.nextToken();
if(isOperand(curToken)) {
//jika currentToken adalah operand, maka kembalikan sebagai ekspresi postfix
postfixExp = postfixExp + " " + (Double.parseDouble(curToken));
} else if(curToken.equals("(")) {
//jika currentToken adalah kurung buka, maka push tanda kurung buka ke stack operator
Character tanda = new Character('(');
stackOfOperator.push(tanda);
nKurungBuka++;
} else if(curToken.equals(")")) {
//jika currentToken adalah kurung tutup, maka pop stack operator sampai ketemu kurung buka
while(((Character)stackOfOperator.peek()).charValue() != '(') {
postfixExp = postfixExp + "" + stackOfOperator.pop();
}
if (((Character)stackOfOperator.peek()).charValue() == '(')
stackOfOperator.pop();
nKurungTutup++;
} else if(isOperator(curToken)) {
//jika currentToken adalah operator
if(stackOfOperator.isEmpty()) {
//stack operator masih kosong, maka push currentToken ke stack operator
Character tanda = new Character(curToken.charAt(0));
stackOfOperator.push(tanda);
} else {
/*
stack operator sudah ada isinya
ambil puncak stack, lalu bandingkan presedensinya dengan currentToken
jika precendence(puncak) > precedence(currentToken) maka pop stack
*/
Character tanda = new Character(curToken.charAt(0));
if (skala_prioritas(((Character)stackOfOperator.peek()).charValue()) > skala_prioritas(tanda)) {
postfixExp = postfixExp + " " + stackOfOperator.pop();
}
//push currentToken
stackOfOperator.push(tanda);
}
} else {
//ekspresi tidak valid
throw new PrioritasException("Syntax Error");
}
//System.out.println(bla + " " +postfixExp );
//bla++;
}
//ekspresi tidak valid
if(nKurungBuka != nKurungTutup)
throw new PrioritasException("Syntax Error");
//pop terus stack operator sampai kosong
while (!stackOfOperator.isEmpty()) {
postfixExp = postfixExp + " " + stackOfOperator.pop();
}
return postfixExp;
}
public double evaluate(String postfixExp) throws Exception {
StringTokenizer st = new StringTokenizer(postfixExp);
double skala;
String curToken = "";
while (st.hasMoreTokens()) {
//mengambil token
curToken = st.nextToken();
if(isOperand(curToken)) {
//jika currentToken adalah operand, maka push ke stack operand
Double opn = new Double(Double.parseDouble(curToken));
stackOfOperand.push(opn);
} else {
//jika currentToken adalah operator, maka evaluasi dua operan sebelumnya
double opn2 = ((Double)stackOfOperand.pop()).doubleValue();
double opn1 = ((Double)stackOfOperand.pop()).doubleValue();
double result = 0;
switch(curToken.charAt(0)) {
case '^':{
result = Math.pow(opn1, opn2);
break;
}
case '*':{
result = opn1 * opn2;
break;
}
case '%':{
result = opn1 % opn2;
break;
}
case '/':{
result = opn1 / opn2;
break;
}
case '-':{
result = opn1 - opn2;
break;
}
case '+':{
result = opn1 + opn2;
break;
}
}
Double opn = new Double(result);
stackOfOperand.push(opn);
}
}
skala = ((Double)stackOfOperand.pop()).doubleValue();
return skala;
}
class PrioritasException extends Exception {
private String message;
public PrioritasException(String _message) {
super(_message);
message = _message;
}
@Override
public String getMessage() {
return message;
}
@Override
public String toString() {
return "PrioritasException: " + getMessage();
}
@Override
public void printStackTrace() {
System.out.println(this);
super.fillInStackTrace();
}
}
}
12. Buatlah class java dengan cara klik file-new file dan beri nama kalkulator console
13. Ketikkan script berikut
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package keren;
/**
*
* @author Puspa
*/
public class kalkulatorconsole {
Prioritas priority = new Prioritas();
String infixExp = "", postfixExp = "";
}
Hasil Akhir
Langganan:
Posting Komentar (Atom)
infonya sangat bermanfat dan menambah wawasan,, thanks gan..
BalasHapus