Advertisement
Guest User

Untitled

a guest
Aug 29th, 2016
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 27.16 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.math.BigDecimal;
  3. import java.math.RoundingMode;
  4.  
  5. public class Calc {
  6.     public static void main(String[] args) {
  7.         try {
  8.             String systemLookAndFeelClassName = UIManager.getSystemLookAndFeelClassName();
  9.             UIManager.setLookAndFeel(systemLookAndFeelClassName);
  10.         } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
  11.         }
  12.          java.awt.EventQueue.invokeLater(() -> {
  13.              Gui gui = new Gui();
  14.              gui.setLocationRelativeTo(null);
  15.              gui.setVisible(true);
  16.         });      
  17.     }    
  18. }
  19.  
  20. public class Gui extends javax.swing.JFrame {
  21.     double firstNum = 0;
  22.     String operation = "";  
  23.  
  24.     public Gui() {
  25.         initComponents();
  26.     }
  27.  
  28.     //@SuppressWarnings("unchecked")
  29.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  30.     private void initComponents() {
  31.  
  32.         jTextField1 = new javax.swing.JTextField();
  33.         jButton1 = new javax.swing.JButton();
  34.         jButton5 = new javax.swing.JButton();
  35.         jButton6 = new javax.swing.JButton();
  36.         jButton7 = new javax.swing.JButton();
  37.         jButton8 = new javax.swing.JButton();
  38.         jButton9 = new javax.swing.JButton();
  39.         jButton10 = new javax.swing.JButton();
  40.         jButton11 = new javax.swing.JButton();
  41.         jButton12 = new javax.swing.JButton();
  42.         jButton13 = new javax.swing.JButton();
  43.         jButton14 = new javax.swing.JButton();
  44.         jButton15 = new javax.swing.JButton();
  45.         jButton16 = new javax.swing.JButton();
  46.         jButton17 = new javax.swing.JButton();
  47.         jButton18 = new javax.swing.JButton();
  48.         jButton19 = new javax.swing.JButton();
  49.  
  50.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  51.         setTitle("Calculator v1.0");
  52.         setBackground(new java.awt.Color(204, 204, 204));
  53.         setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
  54.         setForeground(new java.awt.Color(255, 255, 255));
  55.         setResizable(false);
  56.  
  57.         jTextField1.setFont(new java.awt.Font("Arial", 0, 24)); // NOI18N
  58.         jTextField1.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
  59.         jTextField1.setToolTipText("");
  60.         jTextField1.setEnabled(false);
  61.         jTextField1.addMouseListener(new java.awt.event.MouseAdapter() {
  62.             public void mouseClicked(java.awt.event.MouseEvent evt) {
  63.                 jTextField1MouseClicked(evt);
  64.             }
  65.         });
  66.  
  67.         jButton1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
  68.         jButton1.setText("7");
  69.         jButton1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
  70.         jButton1.setFocusPainted(false);
  71.         jButton1.setFocusable(false);
  72.         jButton1.addActionListener(new java.awt.event.ActionListener() {
  73.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  74.                 jButton1ActionPerformed(evt);
  75.             }
  76.         });
  77.  
  78.         jButton5.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
  79.         jButton5.setText("4");
  80.         jButton5.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
  81.         jButton5.setFocusPainted(false);
  82.         jButton5.setFocusable(false);
  83.         jButton5.addActionListener(new java.awt.event.ActionListener() {
  84.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  85.                 jButton5ActionPerformed(evt);
  86.             }
  87.         });
  88.  
  89.         jButton6.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
  90.         jButton6.setText("1");
  91.         jButton6.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
  92.         jButton6.setFocusPainted(false);
  93.         jButton6.setFocusable(false);
  94.         jButton6.addActionListener(new java.awt.event.ActionListener() {
  95.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  96.                 jButton6ActionPerformed(evt);
  97.             }
  98.         });
  99.  
  100.         jButton7.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
  101.         jButton7.setText("2");
  102.         jButton7.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
  103.         jButton7.setFocusPainted(false);
  104.         jButton7.setFocusable(false);
  105.         jButton7.addActionListener(new java.awt.event.ActionListener() {
  106.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  107.                 jButton7ActionPerformed(evt);
  108.             }
  109.         });
  110.  
  111.         jButton8.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
  112.         jButton8.setText("8");
  113.         jButton8.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
  114.         jButton8.setFocusPainted(false);
  115.         jButton8.setFocusable(false);
  116.         jButton8.addActionListener(new java.awt.event.ActionListener() {
  117.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  118.                 jButton8ActionPerformed(evt);
  119.             }
  120.         });
  121.  
  122.         jButton9.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
  123.         jButton9.setText("9");
  124.         jButton9.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
  125.         jButton9.setFocusPainted(false);
  126.         jButton9.setFocusable(false);
  127.         jButton9.addActionListener(new java.awt.event.ActionListener() {
  128.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  129.                 jButton9ActionPerformed(evt);
  130.             }
  131.         });
  132.  
  133.         jButton10.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
  134.         jButton10.setText("/");
  135.         jButton10.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
  136.         jButton10.setFocusPainted(false);
  137.         jButton10.setFocusable(false);
  138.         jButton10.addActionListener(new java.awt.event.ActionListener() {
  139.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  140.                 jButton10ActionPerformed(evt);
  141.             }
  142.         });
  143.  
  144.         jButton11.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
  145.         jButton11.setText("5");
  146.         jButton11.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
  147.         jButton11.setFocusPainted(false);
  148.         jButton11.setFocusable(false);
  149.         jButton11.addActionListener(new java.awt.event.ActionListener() {
  150.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  151.                 jButton11ActionPerformed(evt);
  152.             }
  153.         });
  154.  
  155.         jButton12.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
  156.         jButton12.setText("6");
  157.         jButton12.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
  158.         jButton12.setFocusPainted(false);
  159.         jButton12.setFocusable(false);
  160.         jButton12.addActionListener(new java.awt.event.ActionListener() {
  161.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  162.                 jButton12ActionPerformed(evt);
  163.             }
  164.         });
  165.  
  166.         jButton13.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
  167.         jButton13.setText("*");
  168.         jButton13.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
  169.         jButton13.setFocusPainted(false);
  170.         jButton13.setFocusable(false);
  171.         jButton13.addActionListener(new java.awt.event.ActionListener() {
  172.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  173.                 jButton13ActionPerformed(evt);
  174.             }
  175.         });
  176.  
  177.         jButton14.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
  178.         jButton14.setText("3");
  179.         jButton14.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
  180.         jButton14.setFocusPainted(false);
  181.         jButton14.setFocusable(false);
  182.         jButton14.addActionListener(new java.awt.event.ActionListener() {
  183.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  184.                 jButton14ActionPerformed(evt);
  185.             }
  186.         });
  187.  
  188.         jButton15.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
  189.         jButton15.setText("+");
  190.         jButton15.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
  191.         jButton15.setFocusPainted(false);
  192.         jButton15.setFocusable(false);
  193.         jButton15.addActionListener(new java.awt.event.ActionListener() {
  194.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  195.                 jButton15ActionPerformed(evt);
  196.             }
  197.         });
  198.  
  199.         jButton16.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
  200.         jButton16.setText("0");
  201.         jButton16.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
  202.         jButton16.setFocusPainted(false);
  203.         jButton16.setFocusable(false);
  204.         jButton16.addActionListener(new java.awt.event.ActionListener() {
  205.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  206.                 jButton16ActionPerformed(evt);
  207.             }
  208.         });
  209.  
  210.         jButton17.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
  211.         jButton17.setText(".");
  212.         jButton17.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
  213.         jButton17.setFocusPainted(false);
  214.         jButton17.setFocusable(false);
  215.         jButton17.addActionListener(new java.awt.event.ActionListener() {
  216.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  217.                 jButton17ActionPerformed(evt);
  218.             }
  219.         });
  220.  
  221.         jButton18.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
  222.         jButton18.setText("=");
  223.         jButton18.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
  224.         jButton18.setFocusPainted(false);
  225.         jButton18.setFocusable(false);
  226.         jButton18.addActionListener(new java.awt.event.ActionListener() {
  227.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  228.                 jButton18ActionPerformed(evt);
  229.             }
  230.         });
  231.  
  232.         jButton19.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
  233.         jButton19.setText("-");
  234.         jButton19.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
  235.         jButton19.setFocusPainted(false);
  236.         jButton19.setFocusable(false);
  237.         jButton19.addActionListener(new java.awt.event.ActionListener() {
  238.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  239.                 jButton19ActionPerformed(evt);
  240.             }
  241.         });
  242.  
  243.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  244.         getContentPane().setLayout(layout);
  245.         layout.setHorizontalGroup(
  246.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  247.             .addGroup(layout.createSequentialGroup()
  248.                 .addContainerGap()
  249.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  250.                     .addComponent(jTextField1)
  251.                     .addGroup(layout.createSequentialGroup()
  252.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  253.                             .addGroup(layout.createSequentialGroup()
  254.                                 .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
  255.                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  256.                                 .addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
  257.                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  258.                                 .addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
  259.                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  260.                                 .addComponent(jButton10, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE))
  261.                             .addGroup(layout.createSequentialGroup()
  262.                                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
  263.                                     .addGroup(layout.createSequentialGroup()
  264.                                         .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
  265.                                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  266.                                         .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE))
  267.                                     .addGroup(layout.createSequentialGroup()
  268.                                         .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
  269.                                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  270.                                         .addComponent(jButton11, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)))
  271.                                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  272.                                     .addGroup(layout.createSequentialGroup()
  273.                                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  274.                                         .addComponent(jButton12, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
  275.                                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  276.                                         .addComponent(jButton13, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE))
  277.                                     .addGroup(layout.createSequentialGroup()
  278.                                         .addGap(6, 6, 6)
  279.                                         .addComponent(jButton14, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
  280.                                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  281.                                         .addComponent(jButton15, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE))))
  282.                             .addGroup(layout.createSequentialGroup()
  283.                                 .addComponent(jButton16, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
  284.                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  285.                                 .addComponent(jButton17, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
  286.                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  287.                                 .addComponent(jButton18, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
  288.                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  289.                                 .addComponent(jButton19, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)))
  290.                         .addGap(0, 0, Short.MAX_VALUE)))
  291.                 .addContainerGap())
  292.         );
  293.         layout.setVerticalGroup(
  294.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  295.             .addGroup(layout.createSequentialGroup()
  296.                 .addContainerGap()
  297.                 .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
  298.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  299.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  300.                     .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
  301.                     .addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
  302.                     .addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
  303.                     .addComponent(jButton10, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
  304.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  305.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  306.                     .addComponent(jButton13, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
  307.                     .addComponent(jButton12, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
  308.                     .addComponent(jButton11, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
  309.                     .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
  310.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  311.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  312.                     .addComponent(jButton15, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
  313.                     .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
  314.                     .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
  315.                     .addComponent(jButton14, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
  316.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  317.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  318.                     .addComponent(jButton16, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
  319.                     .addComponent(jButton17, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
  320.                     .addComponent(jButton18, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
  321.                     .addComponent(jButton19, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
  322.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  323.         );
  324.  
  325.         pack();
  326.     }// </editor-fold>                        
  327.  
  328.     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  329.         jTextField1.setText(jTextField1.getText() + "7");
  330.     }                                        
  331.  
  332.     private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  333.         jTextField1.setText(jTextField1.getText() + "8");
  334.     }                                        
  335.  
  336.     private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  337.         jTextField1.setText(jTextField1.getText() + "9");
  338.     }                                        
  339.  
  340.     private void jButton16ActionPerformed(java.awt.event.ActionEvent evt) {                                          
  341.         jTextField1.setText(jTextField1.getText() + "0");
  342.     }                                        
  343.  
  344.     private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  345.         jTextField1.setText(jTextField1.getText() + "1");
  346.     }                                        
  347.  
  348.     private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  349.        
  350.         jTextField1.setText(jTextField1.getText() + "2");
  351.     }                                        
  352.  
  353.     private void jButton14ActionPerformed(java.awt.event.ActionEvent evt) {                                          
  354.         jTextField1.setText(jTextField1.getText() + "3");
  355.     }                                        
  356.  
  357.     private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  358.         jTextField1.setText(jTextField1.getText() + "4");
  359.     }                                        
  360.  
  361.     private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {                                          
  362.         jTextField1.setText(jTextField1.getText() + "5");
  363.     }                                        
  364.  
  365.     private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) {                                          
  366.         jTextField1.setText(jTextField1.getText() + "6");
  367.     }                                        
  368.  
  369.     private void jTextField1MouseClicked(java.awt.event.MouseEvent evt) {                                        
  370.         String temp = jTextField1.getText();
  371.         int len = 0;
  372.         if(len != temp.length()) {
  373.             jTextField1.setText(temp.substring(0, temp.length() - 1).trim());  
  374.         }
  375.     }                                        
  376.  
  377.     private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {                                          
  378.         firstNum = Double.valueOf(jTextField1.getText());
  379.         operation = " / ";
  380.         String temp = String.valueOf(firstNum);
  381.         if(temp.endsWith(".0")) {            
  382.             temp = temp.substring(0, temp.length() - 2);
  383.             jTextField1.setText(temp + operation);
  384.         } else {
  385.             jTextField1.setText(temp + operation);
  386.         }
  387.     }                                        
  388.  
  389.     private void jButton13ActionPerformed(java.awt.event.ActionEvent evt) {                                          
  390.         firstNum = Double.valueOf(jTextField1.getText());
  391.         operation = " * ";
  392.         String temp = String.valueOf(firstNum);
  393.         if(temp.endsWith(".0")) {            
  394.             temp = temp.substring(0, temp.length() - 2);
  395.             jTextField1.setText(temp + operation);
  396.         } else {
  397.             jTextField1.setText(temp + operation);
  398.         }
  399.     }                                        
  400.  
  401.     private void jButton15ActionPerformed(java.awt.event.ActionEvent evt) {                                          
  402.         firstNum = Double.valueOf(jTextField1.getText());
  403.         operation = " + ";
  404.         String temp = String.valueOf(firstNum);
  405.         if(temp.endsWith(".0")) {            
  406.             temp = temp.substring(0, temp.length() - 2);
  407.             jTextField1.setText(temp + operation);
  408.         } else {
  409.             jTextField1.setText(temp + operation);
  410.         }
  411.     }                                        
  412.  
  413.     private void jButton19ActionPerformed(java.awt.event.ActionEvent evt) {                                          
  414.         firstNum = Double.valueOf(jTextField1.getText());
  415.         operation = " - ";
  416.         String temp = String.valueOf(firstNum);
  417.         if(temp.endsWith(".0")) {            
  418.             temp = temp.substring(0, temp.length() - 2);
  419.             jTextField1.setText(temp + operation);
  420.         } else {
  421.             jTextField1.setText(temp + operation);
  422.         }
  423.     }                                        
  424.  
  425.     private void jButton18ActionPerformed(java.awt.event.ActionEvent evt) {                                          
  426.         String temp = jTextField1.getText();
  427.         boolean flag = true;
  428.         int a = temp.indexOf("*");
  429.         int b = temp.indexOf("/");
  430.         int c = temp.indexOf("-");
  431.         int d = temp.indexOf("+");
  432.         double secondNum;
  433.        
  434.         if(operation.trim().equals("*")) {
  435.            secondNum = Double.valueOf(temp.substring(a + 2));
  436.            double p1 = firstNum * secondNum;
  437.            p1 = new BigDecimal(p1).setScale(14, RoundingMode.HALF_UP).doubleValue();
  438.            temp = String.valueOf(p1);
  439.            if (temp.endsWith(".0")) {
  440.                temp = String.valueOf(p1).substring(0, String.valueOf(p1).length() - 2);
  441.            } else {
  442.                temp = String.valueOf(p1).substring(0, String.valueOf(p1).length());
  443.            }
  444.            jTextField1.setText(temp);
  445.         } else if (operation.trim().equals("/")) {
  446.            secondNum = Double.valueOf(temp.substring(b + 2));
  447.            double p2 = firstNum / secondNum;          
  448.            temp = String.valueOf(p2);
  449.            
  450.           /* if(!temp.equals("Infinite") | !temp.equals("NaN")) {
  451.                p2 = new BigDecimal(p2).setScale(10, RoundingMode.HALF_UP).doubleValue();
  452.            }*/
  453.            
  454.            if (temp.endsWith(".0")) {
  455.                temp = String.valueOf(p2).substring(0, String.valueOf(p2).length() - 2);
  456.            } else {
  457.                temp = String.valueOf(p2).substring(0, String.valueOf(p2).length());
  458.            }
  459.            jTextField1.setText(temp);          
  460.         } else if (operation.trim().equals("-")) {
  461.            secondNum = Double.valueOf(temp.substring(c + 2));
  462.            double p3 = firstNum - secondNum;
  463.            p3 = new BigDecimal(p3).setScale(14, RoundingMode.HALF_UP).doubleValue();
  464.            temp = String.valueOf(p3);
  465.            if (temp.endsWith(".0")) {
  466.                temp = String.valueOf(p3).substring(0, String.valueOf(p3).length() - 2);
  467.            } else {
  468.                temp = String.valueOf(p3).substring(0, String.valueOf(p3).length());
  469.            }
  470.            jTextField1.setText(temp);
  471.         } else if (operation.trim().equals("+")) {
  472.            secondNum = Double.valueOf(temp.substring(d + 2));
  473.            double p4 = firstNum + secondNum;
  474.            p4 = new BigDecimal(p4).setScale(14, RoundingMode.HALF_UP).doubleValue();
  475.            temp = String.valueOf(p4);
  476.            if (temp.endsWith(".0")) {
  477.                temp = String.valueOf(p4).substring(0, String.valueOf(p4).length() - 2);
  478.            } else {
  479.                temp = String.valueOf(p4).substring(0, String.valueOf(p4).length());
  480.            }
  481.            jTextField1.setText(temp);
  482.         }
  483.     }                                        
  484.  
  485.     private void jButton17ActionPerformed(java.awt.event.ActionEvent evt) {                                          
  486.         jTextField1.setText(jTextField1.getText() + ".");
  487.     }                                        
  488.  
  489.    
  490.     // Variables declaration - do not modify                    
  491.     private javax.swing.JButton jButton1;
  492.     private javax.swing.JButton jButton10;
  493.     private javax.swing.JButton jButton11;
  494.     private javax.swing.JButton jButton12;
  495.     private javax.swing.JButton jButton13;
  496.     private javax.swing.JButton jButton14;
  497.     private javax.swing.JButton jButton15;
  498.     private javax.swing.JButton jButton16;
  499.     private javax.swing.JButton jButton17;
  500.     private javax.swing.JButton jButton18;
  501.     private javax.swing.JButton jButton19;
  502.     private javax.swing.JButton jButton5;
  503.     private javax.swing.JButton jButton6;
  504.     private javax.swing.JButton jButton7;
  505.     private javax.swing.JButton jButton8;
  506.     private javax.swing.JButton jButton9;
  507.     private javax.swing.JTextField jTextField1;
  508.     // End of variables declaration                  
  509. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement