Advertisement
casparcedro

C-latest

Mar 20th, 2013
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.41 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package GUI;
  6.  
  7. /**
  8.  *
  9.  * @author
  10.  */
  11. public class CalculatorGUI extends javax.swing.JFrame {
  12.  
  13.     public double firstDouble; // the first value is stored here
  14.     public double secondDouble; // the second value is stored here
  15.     public double answer = 0; // self-explanatory, spiteful comment
  16.     boolean operatorIsSelected = false; // used to know if a value is firstDouble or secondDouble
  17.     String operators = ""; // used to denote operator to be used in calculation
  18.     String repeat = "";
  19.     int i = 0;
  20.  
  21.     /**
  22.      * Creates new form CalculatorGUI
  23.      */
  24.     public CalculatorGUI() {
  25.         initComponents();
  26.         result.setText(""); // ensures calculator screen is clear
  27.  
  28.     }
  29.    
  30.     /* The following code allows values to be set on the calculator screen and
  31.      manipulated. */
  32.     private void operation(String value){
  33.         String text = result.getText();
  34.         if (text.length() > 0 && operatorIsSelected == false) {
  35.             result.setText(result.getText() + value);
  36.         } else if (operatorIsSelected == true) {
  37.             //for (int i = 0; i < 1;i++) {
  38.                 result.setText(null);
  39.                 operatorIsSelected = false;
  40.                 result.setText(result.getText() + value);
  41.             //}
  42.         } else {
  43.             result.setText(value);
  44.         }
  45.     }
  46.  
  47. -- SAVED SPACE --                    
  48.  
  49.     private void oneActionPerformed(java.awt.event.ActionEvent evt) {                                    
  50.  
  51. operation("1");
  52.     }                                  
  53.  
  54.     private void twoActionPerformed(java.awt.event.ActionEvent evt) {                                    
  55.  
  56. operation("2");
  57.     }                                  
  58.  
  59.     private void threeActionPerformed(java.awt.event.ActionEvent evt) {                                      
  60.  
  61. operation("3");
  62.     }                                    
  63.  
  64.     private void fourActionPerformed(java.awt.event.ActionEvent evt) {                                    
  65.  
  66. operation("4");
  67.     }                                    
  68.  
  69.     private void fiveActionPerformed(java.awt.event.ActionEvent evt) {                                    
  70.  
  71. operation("5");
  72.     }                                    
  73.  
  74.     private void sixActionPerformed(java.awt.event.ActionEvent evt) {                                    
  75.  
  76. operation("6");
  77.     }                                  
  78.  
  79.     private void sevenActionPerformed(java.awt.event.ActionEvent evt) {                                      
  80.  
  81. operation("7");
  82.     }                                    
  83.  
  84.     private void eightActionPerformed(java.awt.event.ActionEvent evt) {                                      
  85.  
  86. operation("8");
  87.     }                                    
  88.  
  89.     private void nineActionPerformed(java.awt.event.ActionEvent evt) {                                    
  90.  
  91. operation("9");
  92.     }                                    
  93.  
  94.     private void zeroActionPerformed(java.awt.event.ActionEvent evt) {                                    
  95.  
  96. operation("0");
  97.     }                                    
  98.  
  99.     private void clearAllActionPerformed(java.awt.event.ActionEvent evt) {                                        
  100.  
  101.         result.setText("");
  102.         firstDouble = 0;
  103.         secondDouble = 0;
  104.         i = 0;
  105.     }                                        
  106.  
  107.     private void resultActionPerformed(java.awt.event.ActionEvent evt) {                                      
  108. // This is the calculator screen, do not edit as all the other buttons manipulate this.
  109.     }                                      
  110.  
  111.     private void divisionActionPerformed(java.awt.event.ActionEvent evt) {                                        
  112.  
  113.         String numberwords = result.getText();
  114.         firstDouble = Double.parseDouble(numberwords);
  115.         result.setText("/");
  116.         operatorIsSelected = true;
  117.         operators = "/";
  118.         repeat = "/";
  119.     }                                        
  120.  
  121.     private void multiplicationActionPerformed(java.awt.event.ActionEvent evt) {                                              
  122.  
  123.         String numberwords = result.getText();
  124.         firstDouble = Double.parseDouble(numberwords);
  125.         result.setText("X");
  126.         operatorIsSelected = true;
  127.         operators = "*";
  128.         repeat = "*";
  129.     }                                              
  130.  
  131.     private void additionActionPerformed(java.awt.event.ActionEvent evt) {                                        
  132.  
  133.         String numberwords = result.getText();
  134.         firstDouble = Double.parseDouble(numberwords);
  135.         result.setText("+");
  136.         operatorIsSelected = true;
  137.         operators = "+";
  138.         repeat = "+";
  139.     }                                        
  140.  
  141.     private void subtractionActionPerformed(java.awt.event.ActionEvent evt) {                                            
  142.  
  143.         String numberwords = result.getText();
  144.         firstDouble = Double.parseDouble(numberwords);
  145.         result.setText("-");
  146.         operatorIsSelected = true;
  147.         operators = "-";
  148.         repeat = "-";
  149.     }                                          
  150.  
  151.     private void equalsActionPerformed(java.awt.event.ActionEvent evt) {                                      
  152.  
  153.         secondDouble = Double.parseDouble(result.getText());
  154.  
  155.         switch (operators) {
  156.             case "/":
  157.                 answer = firstDouble / secondDouble;
  158.                 operators = "";
  159.                 break;
  160.             case "*":
  161.                 answer = firstDouble * secondDouble;
  162.                 operators = "";
  163.                 break;
  164.             case "+":
  165.                 answer = firstDouble + secondDouble;
  166.                 operators = "";
  167.                 break;
  168.             case "-":
  169.                 answer = firstDouble - secondDouble;
  170.                 operators = "";
  171.                 break;
  172.             default:
  173.                 if(i == 0){
  174.        double savedValue = secondDouble;
  175.                 switch(repeat){  
  176.             case "/":
  177.                 answer = answer / savedValue;
  178.                 operators = "";
  179.                 break;
  180.             case "*":
  181.                 answer = answer * savedValue;
  182.                 operators = "";
  183.                 break;
  184.             case "+":
  185.                 answer = answer + savedValue;
  186.                 operators = "";
  187.                 break;
  188.             case "-":
  189.                 answer = answer - savedValue;
  190.                 operators = "";
  191.                 break;
  192.                 }
  193.                 };
  194.         }
  195.  
  196.         result.setText("" + answer + "");
  197.  
  198.     }                                      
  199.  
  200.     /**
  201.      * @param args the command line arguments
  202.      */
  203.     public static void main(String args[]) {
  204.         /* Set the Nimbus look and feel */
  205.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  206.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  207.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  208.          */
  209.         try {
  210.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  211.                 if ("Nimbus".equals(info.getName())) {
  212.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  213.                     break;
  214.  
  215.  
  216.  
  217.  
  218.                 }
  219.             }
  220.         } catch (ClassNotFoundException ex) {
  221.             java.util.logging.Logger.getLogger(CalculatorGUI.class
  222.                     .getName()).log(java.util.logging.Level.SEVERE, null, ex);
  223.         } catch (InstantiationException ex) {
  224.             java.util.logging.Logger.getLogger(CalculatorGUI.class
  225.                     .getName()).log(java.util.logging.Level.SEVERE, null, ex);
  226.         } catch (IllegalAccessException ex) {
  227.             java.util.logging.Logger.getLogger(CalculatorGUI.class
  228.                     .getName()).log(java.util.logging.Level.SEVERE, null, ex);
  229.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  230.             java.util.logging.Logger.getLogger(CalculatorGUI.class
  231.                     .getName()).log(java.util.logging.Level.SEVERE, null, ex);
  232.         }
  233.         //</editor-fold>
  234.  
  235.         /* Create and display the form */
  236.         java.awt.EventQueue.invokeLater(new Runnable() {
  237.             public void run() {
  238.                 new CalculatorGUI().setVisible(true);
  239.             }
  240.         });
  241.     }
  242.     // Variables declaration - do not modify                    
  243.     private javax.swing.JButton addition;
  244.     private javax.swing.JButton clear;
  245.     private javax.swing.JButton clearAll;
  246.     private javax.swing.JButton decimal;
  247.     private javax.swing.JButton division;
  248.     private javax.swing.JButton eight;
  249.     private javax.swing.JButton equals;
  250.     private javax.swing.JButton five;
  251.     private javax.swing.JButton four;
  252.     private javax.swing.JButton multiplication;
  253.     private javax.swing.JButton nine;
  254.     private javax.swing.JButton one;
  255.     private javax.swing.JFormattedTextField result;
  256.     private javax.swing.JButton seven;
  257.     private javax.swing.JButton six;
  258.     private javax.swing.JButton subtraction;
  259.     private javax.swing.JButton three;
  260.     private javax.swing.JButton two;
  261.     private javax.swing.JButton zero;
  262.     // End of variables declaration                  
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement