thieumao

Phương trình bậc 2

Aug 30th, 2013
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.08 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package ptbac2;
  6.  
  7. import javax.swing.JOptionPane;
  8.  
  9. /**
  10.  *
  11.  * @author leo
  12.  */
  13. public class Bac2 extends javax.swing.JFrame {
  14.  
  15.     /**
  16.      * Creates new form Bac2
  17.      */
  18.     public Bac2() {
  19.         initComponents();
  20.     }
  21.  
  22.     /**
  23.      * This method is called from within the constructor to initialize the form.
  24.      * WARNING: Do NOT modify this code. The content of this method is always
  25.      * regenerated by the Form Editor.
  26.      */
  27.     @SuppressWarnings("unchecked")
  28.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  29.     private void initComponents() {
  30.  
  31.         jLabel1 = new javax.swing.JLabel();
  32.         jLabel2 = new javax.swing.JLabel();
  33.         jLabel3 = new javax.swing.JLabel();
  34.         jLabel4 = new javax.swing.JLabel();
  35.         tfA = new javax.swing.JTextField();
  36.         tfB = new javax.swing.JTextField();
  37.         tfC = new javax.swing.JTextField();
  38.         lbResult = new javax.swing.JLabel();
  39.         btCalc = new javax.swing.JButton();
  40.         btClear = new javax.swing.JButton();
  41.  
  42.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  43.  
  44.         jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
  45.         jLabel1.setText("ax^2 + bx + c = 0");
  46.  
  47.         jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
  48.         jLabel2.setText("a=");
  49.  
  50.         jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
  51.         jLabel3.setText("b=");
  52.  
  53.         jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
  54.         jLabel4.setText("c=");
  55.  
  56.         lbResult.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
  57.         lbResult.setText("Result");
  58.  
  59.         btCalc.setText("Calc");
  60.         btCalc.addActionListener(new java.awt.event.ActionListener() {
  61.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  62.                 btCalcActionPerformed(evt);
  63.             }
  64.         });
  65.  
  66.         btClear.setText("Clear");
  67.         btClear.addActionListener(new java.awt.event.ActionListener() {
  68.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  69.                 btClearActionPerformed(evt);
  70.             }
  71.         });
  72.  
  73.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  74.         getContentPane().setLayout(layout);
  75.         layout.setHorizontalGroup(
  76.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  77.             .addGroup(layout.createSequentialGroup()
  78.                 .addGap(47, 47, 47)
  79.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  80.                     .addComponent(jLabel2)
  81.                     .addComponent(jLabel3)
  82.                     .addComponent(jLabel4))
  83.                 .addGap(34, 34, 34)
  84.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  85.                     .addComponent(lbResult)
  86.                     .addComponent(jLabel1)
  87.                     .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
  88.                         .addGroup(layout.createSequentialGroup()
  89.                             .addComponent(btCalc)
  90.                             .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 42, Short.MAX_VALUE)
  91.                             .addComponent(btClear))
  92.                         .addComponent(tfA, javax.swing.GroupLayout.Alignment.LEADING)
  93.                         .addComponent(tfB, javax.swing.GroupLayout.Alignment.LEADING)
  94.                         .addComponent(tfC, javax.swing.GroupLayout.Alignment.LEADING)))
  95.                 .addContainerGap(65, Short.MAX_VALUE))
  96.         );
  97.         layout.setVerticalGroup(
  98.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  99.             .addGroup(layout.createSequentialGroup()
  100.                 .addGap(27, 27, 27)
  101.                 .addComponent(jLabel1)
  102.                 .addGap(26, 26, 26)
  103.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  104.                     .addComponent(jLabel2)
  105.                     .addComponent(tfA, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  106.                 .addGap(22, 22, 22)
  107.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  108.                     .addComponent(jLabel3)
  109.                     .addComponent(tfB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  110.                 .addGap(21, 21, 21)
  111.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  112.                     .addComponent(jLabel4)
  113.                     .addComponent(tfC, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  114.                 .addGap(34, 34, 34)
  115.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  116.                     .addComponent(btCalc)
  117.                     .addComponent(btClear))
  118.                 .addGap(36, 36, 36)
  119.                 .addComponent(lbResult)
  120.                 .addContainerGap(43, Short.MAX_VALUE))
  121.         );
  122.  
  123.         pack();
  124.     }// </editor-fold>                        
  125.  
  126.     private void btCalcActionPerformed(java.awt.event.ActionEvent evt) {                                      
  127.         // TODO add your handling code here:
  128.         try {
  129.             float a = Float.valueOf(tfA.getText());
  130.             float b = Float.valueOf(tfB.getText());
  131.             float c = Float.valueOf(tfC.getText());
  132.             float delta = b * b - 4 * a * c;
  133.             if (delta > 0) {
  134.                 float x1 = (float) ((-b + Math.sqrt(delta)) / (2 * a));
  135.                 float x2 = (float) ((-b - Math.sqrt(delta)) / (2 * a));
  136.                 String strResult = "x1 = " + String.valueOf(x1) + " ; x2 = " + String.valueOf(x2);
  137.                 lbResult.setText(strResult);
  138.             }
  139.             if (delta == 0) {
  140.                 float x = -b / (2 * a);
  141.                 lbResult.setText(String.valueOf(x));
  142.             }
  143.             if (delta < 0) {
  144.                 lbResult.setText("Vo nghiem");
  145.             }
  146.         } catch (Exception e) {
  147.             JOptionPane.showMessageDialog(null, "Error!");
  148.             lbResult.setText("Error!");
  149.         }
  150.     }                                      
  151.  
  152.     private void btClearActionPerformed(java.awt.event.ActionEvent evt) {                                        
  153.         // TODO add your handling code here:
  154.         tfA.setText("");
  155.         tfB.setText("");
  156.         tfC.setText("");
  157.         lbResult.setText("");
  158.     }                                      
  159.  
  160.     /**
  161.      * @param args the command line arguments
  162.      */
  163.     public static void main(String args[]) {
  164.         /* Set the Nimbus look and feel */
  165.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  166.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  167.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  168.          */
  169.         try {
  170.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  171.                 if ("Nimbus".equals(info.getName())) {
  172.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  173.                     break;
  174.                 }
  175.             }
  176.         } catch (ClassNotFoundException ex) {
  177.             java.util.logging.Logger.getLogger(Bac2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  178.         } catch (InstantiationException ex) {
  179.             java.util.logging.Logger.getLogger(Bac2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  180.         } catch (IllegalAccessException ex) {
  181.             java.util.logging.Logger.getLogger(Bac2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  182.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  183.             java.util.logging.Logger.getLogger(Bac2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  184.         }
  185.         //</editor-fold>
  186.  
  187.         /* Create and display the form */
  188.         java.awt.EventQueue.invokeLater(new Runnable() {
  189.             public void run() {
  190.                 new Bac2().setVisible(true);
  191.             }
  192.         });
  193.     }
  194.     // Variables declaration - do not modify                    
  195.     private javax.swing.JButton btCalc;
  196.     private javax.swing.JButton btClear;
  197.     private javax.swing.JLabel jLabel1;
  198.     private javax.swing.JLabel jLabel2;
  199.     private javax.swing.JLabel jLabel3;
  200.     private javax.swing.JLabel jLabel4;
  201.     private javax.swing.JLabel lbResult;
  202.     private javax.swing.JTextField tfA;
  203.     private javax.swing.JTextField tfB;
  204.     private javax.swing.JTextField tfC;
  205.     // End of variables declaration                  
  206. }
Advertisement
Add Comment
Please, Sign In to add comment