/* * Calculator.java * * Created on October 7, 2003, 10:57 AM */ /** * * @author hansen */ public class Calculator extends javax.swing.JFrame { private int accumulator = 0; private DisplayState dState = new FirstOverwrite(); private OperatorState oState = new NullOperator(); /** 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. */ private void initComponents() {//GEN-BEGIN:initComponents display = new javax.swing.JTextField(); addButton = new javax.swing.JButton(); subButton = new javax.swing.JButton(); zeroButton = new javax.swing.JButton(); oneButton = new javax.swing.JButton(); clearButton = new javax.swing.JButton(); getContentPane().setLayout(null); addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent evt) { exitForm(evt); } }); display.setEditable(false); display.setText("0"); getContentPane().add(display); display.setBounds(0, 0, 400, 40); addButton.setText("+"); addButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { addButtonActionPerformed(evt); } }); getContentPane().add(addButton); addButton.setBounds(20, 80, 88, 25); subButton.setText("-"); subButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { subButtonActionPerformed(evt); } }); getContentPane().add(subButton); subButton.setBounds(20, 140, 88, 25); zeroButton.setText("0"); zeroButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { zeroButtonActionPerformed(evt); } }); getContentPane().add(zeroButton); zeroButton.setBounds(190, 80, 42, 25); oneButton.setText("1"); oneButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { oneButtonActionPerformed(evt); } }); getContentPane().add(oneButton); oneButton.setBounds(190, 150, 42, 25); clearButton.setText("Clear"); clearButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { clearButtonActionPerformed(evt); } }); getContentPane().add(clearButton); clearButton.setBounds(30, 210, 66, 25); java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize(); setBounds((screenSize.width-400)/2, (screenSize.height-300)/2, 400, 300); }//GEN-END:initComponents private void clearButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearButtonActionPerformed dState = new FirstOverwrite(); oState = new NullOperator(); accumulator = 0; display.setText("0"); // Add your handling code here: }//GEN-LAST:event_clearButtonActionPerformed private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed dState.operatorChosen(); oState = new AddOperator(); // Add your handling code here: }//GEN-LAST:event_addButtonActionPerformed private void zeroButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_zeroButtonActionPerformed dState.digitChosen('0'); // Add your handling code here: }//GEN-LAST:event_zeroButtonActionPerformed private void oneButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_oneButtonActionPerformed dState.digitChosen('1'); // Add your handling code here: }//GEN-LAST:event_oneButtonActionPerformed private void subButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_subButtonActionPerformed dState.operatorChosen(); oState = new SubOperator(); // Add your handling code here: }//GEN-LAST:event_subButtonActionPerformed /** Exit the Application */ private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm System.exit(0); }//GEN-LAST:event_exitForm /** * @param args the command line arguments */ public static void main(String args[]) { new Calculator().show(); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton addButton; private javax.swing.JButton clearButton; private javax.swing.JTextField display; private javax.swing.JButton oneButton; private javax.swing.JButton subButton; private javax.swing.JButton zeroButton; // End of variables declaration//GEN-END:variables // This interface is one of two control interfaces // for the calculator interface DisplayState { public void digitChosen(char digit); public void operatorChosen(); } // When we turn on or clear the calculator we enter the // FirstOverWrite state. class FirstOverwrite implements DisplayState { public void digitChosen(char digit) { display.setText(""+digit); dState = new FirstConcatenate(); } public void operatorChosen() { accumulator = Integer.parseInt(display.getText(), 2); dState = new SecondOverwrite(); } } // After a character is pressed, we concatenate to the display // rather than overwrite it. class FirstConcatenate implements DisplayState { public void digitChosen(char digit) { display.setText(display.getText()+digit); } public void operatorChosen() { accumulator = Integer.parseInt(display.getText(), 2); dState = new SecondOverwrite(); } } // After an operator is chosen, we look for a second // number, starting with SecondOverwrite class SecondOverwrite implements DisplayState { public void digitChosen(char digit) { display.setText(""+digit); dState = new SecondConcatenate(); } public void operatorChosen() { } } // Process the second, third, etc bits of the number class SecondConcatenate implements DisplayState { public void digitChosen(char digit) { display.setText(display.getText()+digit); } public void operatorChosen() { oState.doOp(); dState = new SecondOverwrite(); } } // The second state machine keeps track of what the previous // operation chosen was. interface OperatorState { public int doOp(); } // There is no previous operation class NullOperator implements OperatorState { public int doOp() { return accumulator; } } // The previous operation was an add class AddOperator implements OperatorState { public int doOp() { int temp = Integer.parseInt(display.getText(), 2); accumulator += temp; display.setText(Integer.toBinaryString(accumulator)); return accumulator; } } // The previous operation was a subtract class SubOperator implements OperatorState { public int doOp() { int temp = Integer.parseInt(display.getText(), 2); accumulator -= temp; display.setText(Integer.toBinaryString(accumulator)); return accumulator; } } }