package Calculations;
/**
*
* @author Brooke Linder
* 1.25.20
*/
public class Calculator extends javax.swing.JFrame {
double firstNumber;
double secondNumber;
double result;
String operator;
/**
* Creates new form Calculator
*/
public Calculator() {
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")
// //GEN-BEGIN:initComponents
private void initComponents() {
jTxtBox = new javax.swing.JTextField();
jButtonC = new javax.swing.JButton();
jButtonSign = new javax.swing.JButton();
jButtonAdd = new javax.swing.JButton();
jButton7 = new javax.swing.JButton();
jButton8 = new javax.swing.JButton();
jButton9 = new javax.swing.JButton();
jButtonSub = new javax.swing.JButton();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButtonDiv = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton0 = new javax.swing.JButton();
jButtonDec = new javax.swing.JButton();
jButtonEq = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();
jButtonMul = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setBackground(new java.awt.Color(247, 247, 247));
jTxtBox.setFont(new java.awt.Font("Arial", 1, 24)); // NOI18N
jTxtBox.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
jButtonC.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
jButtonC.setText("C");
jButtonC.setToolTipText("");
jButtonC.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonCActionPerformed(evt);
}
});
jButtonSign.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
jButtonSign.setText("+/-");
jButtonSign.setToolTipText("");
jButtonSign.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonSignActionPerformed(evt);
}
});
jButtonAdd.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
jButtonAdd.setText("+");
jButtonAdd.setToolTipText("");
jButtonAdd.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonAddActionPerformed(evt);
}
});
jButton7.setBackground(new java.awt.Color(252, 252, 252));
jButton7.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
jButton7.setText("7");
jButton7.setToolTipText("");
jButton7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton7ActionPerformed(evt);
}
});
jButton8.setBackground(new java.awt.Color(252, 252, 252));
jButton8.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
jButton8.setText("8");
jButton8.setToolTipText("");
jButton8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton8ActionPerformed(evt);
}
});
jButton9.setBackground(new java.awt.Color(252, 252, 252));
jButton9.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
jButton9.setText("9");
jButton9.setToolTipText("");
jButton9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton9ActionPerformed(evt);
}
});
jButtonSub.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
jButtonSub.setText("-");
jButtonSub.setToolTipText("");
jButtonSub.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonSubActionPerformed(evt);
}
});
jButton1.setBackground(new java.awt.Color(252, 252, 252));
jButton1.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
jButton1.setText("1");
jButton1.setToolTipText("");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setBackground(new java.awt.Color(252, 252, 252));
jButton2.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
jButton2.setText("2");
jButton2.setToolTipText("");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButtonDiv.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
jButtonDiv.setText("/");
jButtonDiv.setToolTipText("");
jButtonDiv.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonDivActionPerformed(evt);
}
});
jButton3.setBackground(new java.awt.Color(252, 252, 252));
jButton3.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
jButton3.setText("3");
jButton3.setToolTipText("");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jButton0.setBackground(new java.awt.Color(252, 252, 252));
jButton0.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
jButton0.setText("0");
jButton0.setToolTipText("");
jButtonDec.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
jButtonDec.setText(".");
jButtonDec.setToolTipText("");
jButtonDec.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonDecActionPerformed(evt);
}
});
jButtonEq.setBackground(new java.awt.Color(255, 153, 51));
jButtonEq.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
jButtonEq.setText("=");
jButtonEq.setToolTipText("");
jButtonEq.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonEqActionPerformed(evt);
}
});
jButton4.setBackground(new java.awt.Color(252, 252, 252));
jButton4.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
jButton4.setText("4");
jButton4.setToolTipText("");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
jButton5.setBackground(new java.awt.Color(252, 252, 252));
jButton5.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
jButton5.setText("5");
jButton5.setToolTipText("");
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton5ActionPerformed(evt);
}
});
jButton6.setBackground(new java.awt.Color(252, 252, 252));
jButton6.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
jButton6.setText("6");
jButton6.setToolTipText("");
jButton6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton6ActionPerformed(evt);
}
});
jButtonMul.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
jButtonMul.setText("*");
jButtonMul.setToolTipText("");
jButtonMul.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonMulActionPerformed(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()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTxtBox)
.addGroup(layout.createSequentialGroup()
.addComponent(jButtonC, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButtonSign, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButtonAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButtonSub, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButtonDiv, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(jButtonDec, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton0, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButtonEq, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButtonMul, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jTxtBox, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButtonC, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButtonSign, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButtonAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButtonSub, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButtonMul, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButtonDiv, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButtonDec, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton0, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButtonEq, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// //GEN-END:initComponents
private void jButtonMulActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonMulActionPerformed
firstNumber = Double.parseDouble(jTxtBox.getText());
jTxtBox.setText("");
operator = "*";
}//GEN-LAST:event_jButtonMulActionPerformed
private void jButtonDivActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDivActionPerformed
firstNumber = Double.parseDouble(jTxtBox.getText());
jTxtBox.setText("");
operator = "/";
}//GEN-LAST:event_jButtonDivActionPerformed
private void jButtonDecActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDecActionPerformed
String enterNum = jTxtBox.getText() + jButtonDec.getText();
jTxtBox.setText(enterNum);
}//GEN-LAST:event_jButtonDecActionPerformed
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
String enterNum = jTxtBox.getText() + jButton1.getText();
jTxtBox.setText(enterNum);
}//GEN-LAST:event_jButton1ActionPerformed
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
String enterNum = jTxtBox.getText() + jButton2.getText();
jTxtBox.setText(enterNum);
}//GEN-LAST:event_jButton2ActionPerformed
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
String enterNum = jTxtBox.getText() + jButton3.getText();
jTxtBox.setText(enterNum); }//GEN-LAST:event_jButton3ActionPerformed
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
String enterNum = jTxtBox.getText() + jButton4.getText();
jTxtBox.setText(enterNum);
}//GEN-LAST:event_jButton4ActionPerformed
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
String enterNum = jTxtBox.getText() + jButton5.getText();
jTxtBox.setText(enterNum);
}//GEN-LAST:event_jButton5ActionPerformed
private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
String enterNum = jTxtBox.getText() + jButton6.getText();
jTxtBox.setText(enterNum);
}//GEN-LAST:event_jButton6ActionPerformed
private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
String enterNum = jTxtBox.getText() + jButton7.getText();
jTxtBox.setText(enterNum);
}//GEN-LAST:event_jButton7ActionPerformed
private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed
String enterNum = jTxtBox.getText() + jButton8.getText();
jTxtBox.setText(enterNum);
}//GEN-LAST:event_jButton8ActionPerformed
private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton9ActionPerformed
String enterNum = jTxtBox.getText() + jButton9.getText();
jTxtBox.setText(enterNum);
}//GEN-LAST:event_jButton9ActionPerformed
private void jButtonCActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCActionPerformed
jTxtBox.setText("");
}//GEN-LAST:event_jButtonCActionPerformed
private void jButtonAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAddActionPerformed
firstNumber = Double.parseDouble(jTxtBox.getText());
jTxtBox.setText("");
operator = "+";
}//GEN-LAST:event_jButtonAddActionPerformed
private void jButtonSubActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSubActionPerformed
firstNumber = Double.parseDouble(jTxtBox.getText());
jTxtBox.setText("");
operator = "-";
}//GEN-LAST:event_jButtonSubActionPerformed
private void jButtonSignActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSignActionPerformed
double sign = Double.parseDouble(String.valueOf(jTxtBox.getText()));
sign = sign * (-1);
jTxtBox.setText(String.valueOf(sign));
}//GEN-LAST:event_jButtonSignActionPerformed
private void jButtonEqActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEqActionPerformed
String answer;
secondNumber = Double.parseDouble(jTxtBox.getText());
if(operator == "+"){
result = firstNumber + secondNumber;
answer = String.format("%.0f", result);
jTxtBox.setText(answer);
}
else if(operator == "-"){
result = firstNumber - secondNumber;
answer = String.format("%.0f", result);
jTxtBox.setText(answer);
}
else if(operator == "*"){
result = firstNumber * secondNumber;
answer = String.format("%.0f", result);
jTxtBox.setText(answer);
}
else if(operator == "/"){
result = firstNumber / secondNumber;
answer = String.format("%.0f", result);
jTxtBox.setText(answer);
}
}//GEN-LAST:event_jButtonEqActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//
/* 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(Calculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Calculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Calculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Calculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Calculator().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton0;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JButton jButton7;
private javax.swing.JButton jButton8;
private javax.swing.JButton jButton9;
private javax.swing.JButton jButtonAdd;
private javax.swing.JButton jButtonC;
private javax.swing.JButton jButtonDec;
private javax.swing.JButton jButtonDiv;
private javax.swing.JButton jButtonEq;
private javax.swing.JButton jButtonMul;
private javax.swing.JButton jButtonSign;
private javax.swing.JButton jButtonSub;
private javax.swing.JTextField jTxtBox;
// End of variables declaration//GEN-END:variables
}