Advertisement
ljukk

kalkulackaa

May 19th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.26 KB | None | 0 0
  1. public class JFrame extends javax.swing.JFrame {
  2.    
  3.     int sum;
  4.     int difference;
  5.     int multiplication;
  6.     int quotient;
  7.    
  8.     int sucet[] = sucet();
  9.     int rozdiel[] = rozdiel();
  10.     int sucin[] = sucin();
  11.     int podiel[] = podiel();
  12.    
  13.     String sum_s;
  14.     String difference_s;
  15.     String multiplication_s;
  16.     String quotient_s;
  17.    
  18.     int accepted = 0;
  19.    
  20.    
  21.     public JFrame() {
  22.         initComponents();
  23.     }
  24.    
  25.     protected int[] sucet() {
  26.         int x = (int) (Math.random()* 20);
  27.         int y = (int) (Math.random()* 20);
  28.         return new int[] {x, y};
  29.     }
  30.    
  31.     protected int[] rozdiel() {
  32.         int x = (int) (Math.random()* 20);
  33.         int y = (int) (Math.random()* 20);
  34.         return new int[] {x, y};
  35.     }
  36.    
  37.     protected int[] sucin() {
  38.         int x = (int) (Math.random()* 20);
  39.         int y = (int) (Math.random()* 20);
  40.         return new int[] {x, y};
  41.     }
  42.    
  43.     protected int[] podiel() {
  44.         int x = (int) (Math.random()* 20);
  45.         int y = (int) (Math.random()* 20);
  46.         return new int[] {x, y};
  47.     }
  48.    
  49.     @SuppressWarnings("unchecked")
  50.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  51.     private void initComponents() {
  52.  
  53.         jButton1 = new javax.swing.JButton();
  54.         jButton2 = new javax.swing.JButton();
  55.         jLabel1 = new javax.swing.JLabel();
  56.         jLabel2 = new javax.swing.JLabel();
  57.         jLabel3 = new javax.swing.JLabel();
  58.         jLabel4 = new javax.swing.JLabel();
  59.         jTextField1 = new javax.swing.JTextField();
  60.         jTextField2 = new javax.swing.JTextField();
  61.         jTextField3 = new javax.swing.JTextField();
  62.         jTextField4 = new javax.swing.JTextField();
  63.         jScrollPane1 = new javax.swing.JScrollPane();
  64.         jTextArea1 = new javax.swing.JTextArea();
  65.         jLabel5 = new javax.swing.JLabel();
  66.  
  67.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  68.  
  69.         jButton1.setText("Daj príklady");
  70.         jButton1.addActionListener(new java.awt.event.ActionListener() {
  71.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  72.                 jButton1ActionPerformed(evt);
  73.             }
  74.         });
  75.  
  76.         jButton2.setText("Skontroluj");
  77.         jButton2.addActionListener(new java.awt.event.ActionListener() {
  78.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  79.                 jButton2ActionPerformed(evt);
  80.             }
  81.         });
  82.  
  83.         jLabel1.setText("Príklad 1");
  84.  
  85.         jLabel2.setText("Príklad 2");
  86.  
  87.         jLabel3.setText("Príklad 3");
  88.  
  89.         jLabel4.setText("Priklad 4");
  90.  
  91.         jTextArea1.setColumns(20);
  92.         jTextArea1.setRows(5);
  93.         jScrollPane1.setViewportView(jTextArea1);
  94.  
  95.         jLabel5.setText("Hodnotenie");
  96.  
  97.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  98.         getContentPane().setLayout(layout);
  99.         layout.setHorizontalGroup(
  100.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  101.             .addGroup(layout.createSequentialGroup()
  102.                 .addContainerGap()
  103.                 .addComponent(jButton1)
  104.                 .addGap(41, 41, 41)
  105.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  106.                     .addComponent(jLabel1)
  107.                     .addComponent(jLabel4)
  108.                     .addComponent(jLabel3)
  109.                     .addComponent(jLabel2))
  110.                 .addGap(77, 77, 77)
  111.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  112.                     .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
  113.                     .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
  114.                     .addGroup(layout.createSequentialGroup()
  115.                         .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
  116.                         .addGap(18, 18, 18)
  117.                         .addComponent(jButton2))
  118.                     .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
  119.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  120.             .addComponent(jScrollPane1)
  121.             .addGroup(layout.createSequentialGroup()
  122.                 .addComponent(jLabel5)
  123.                 .addGap(0, 0, Short.MAX_VALUE))
  124.         );
  125.         layout.setVerticalGroup(
  126.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  127.             .addGroup(layout.createSequentialGroup()
  128.                 .addContainerGap()
  129.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  130.                     .addComponent(jButton1)
  131.                     .addComponent(jButton2)
  132.                     .addComponent(jLabel1)
  133.                     .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  134.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  135.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  136.                     .addComponent(jLabel2)
  137.                     .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  138.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  139.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  140.                     .addComponent(jLabel3)
  141.                     .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  142.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  143.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  144.                     .addComponent(jLabel4)
  145.                     .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  146.                 .addGap(18, 18, 18)
  147.                 .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)
  148.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 32, Short.MAX_VALUE)
  149.                 .addComponent(jLabel5))
  150.         );
  151.  
  152.         pack();
  153.     }// </editor-fold>                        
  154.  
  155.     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  156.         sum = sucet[0] + sucet[1];
  157.         difference = rozdiel[0] - rozdiel[1];
  158.         multiplication = sucin[0] * sucin[1];
  159.         quotient = podiel[0] / podiel[1];
  160.        
  161.         jLabel1.setText("" + Integer.toString(sucet[0]) + " + " + Integer.toString(sucet[1]) + " =");
  162.         jLabel2.setText("" + Integer.toString(rozdiel[0]) + " - " + Integer.toString(rozdiel[1]) + " =");
  163.         jLabel3.setText("" + Integer.toString(sucin[0]) + " * " + Integer.toString(sucin[1]) + " =");
  164.         jLabel4.setText("" + Integer.toString(podiel[0]) + " / " + Integer.toString(podiel[1]) + " =");
  165.     }                                        
  166.  
  167.     private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  168.         int sucet_i = Integer.parseInt(jTextField1.getText());
  169.         int rozdiel_i = Integer.parseInt(jTextField2.getText());
  170.         int sucin_i = Integer.parseInt(jTextField3.getText());
  171.         int podiel_i = Integer.parseInt(jTextField4.getText());
  172.        
  173.         if (sum == sucet_i) {
  174.             sum_s = "" + Integer.toString(sucet[0]) + " + " + Integer.toString(sucet[1]) + " = " + Integer.toString(sum) + " správne";  
  175.             accepted++;
  176.         } else {
  177.             sum_s = "" + Integer.toString(sucet[0]) + " + " + Integer.toString(sucet[1]) + " = " + Integer.toString(sucet_i) + " nesprávne (výsledok je " + Integer.toString(sum) + ").";    
  178.         }
  179.        
  180.         if (difference == rozdiel_i) {
  181.             difference_s = "" + Integer.toString(rozdiel[0]) + " - " + Integer.toString(rozdiel[1]) + " = " + Integer.toString(difference) + " správne";
  182.             accepted++;
  183.         } else {
  184.             difference_s = "" + Integer.toString(rozdiel[0]) + " - " + Integer.toString(rozdiel[1]) + " = " + Integer.toString(rozdiel_i) + " nesprávne (výsledok je " + Integer.toString(difference) + ").";      
  185.         }
  186.        
  187.         if (multiplication == sucin_i) {
  188.             multiplication_s = "" + Integer.toString(sucin[0]) + " * " + Integer.toString(sucin[1]) + " = " + Integer.toString(difference) + " správne";
  189.             accepted++;
  190.         } else {
  191.             multiplication_s = "" + Integer.toString(sucin[0]) + " * " + Integer.toString(sucin[1]) + " = " + Integer.toString(sucin_i) + " nesprávne (výsledok je " + Integer.toString(multiplication) + ").";    
  192.         }
  193.        
  194.         if (quotient == podiel_i) {
  195.             quotient_s = "" + Integer.toString(podiel[0]) + " / " + Integer.toString(podiel[1]) + " = " + Integer.toString(quotient) + " správne";  
  196.             accepted++;
  197.         } else {
  198.             quotient_s = "" + Integer.toString(podiel[0]) + " / " + Integer.toString(podiel[1]) + " = " + Integer.toString(podiel_i) + " nesprávne (výsledok je " + Integer.toString(quotient) + ").";      
  199.         }
  200.        
  201.         jTextArea1.setText(sum_s + "\n" + difference_s + "\n" + multiplication_s + "\n" + quotient_s);
  202.         jLabel5.setText("Počet správnych odpovedí: " + accepted);
  203.     }                                        
  204.  
  205.     /**
  206.      * @param args the command line arguments
  207.      */
  208.     public static void main(String args[]) {
  209.         /* Set the Nimbus look and feel */
  210.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  211.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  212.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  213.          */
  214.         try {
  215.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  216.                 if ("Nimbus".equals(info.getName())) {
  217.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  218.                     break;
  219.                 }
  220.             }
  221.         } catch (ClassNotFoundException ex) {
  222.             java.util.logging.Logger.getLogger(JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  223.         } catch (InstantiationException ex) {
  224.             java.util.logging.Logger.getLogger(JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  225.         } catch (IllegalAccessException ex) {
  226.             java.util.logging.Logger.getLogger(JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  227.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  228.             java.util.logging.Logger.getLogger(JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  229.         }
  230.         //</editor-fold>
  231.  
  232.         /* Create and display the form */
  233.         java.awt.EventQueue.invokeLater(new Runnable() {
  234.             public void run() {
  235.                 new JFrame().setVisible(true);
  236.             }
  237.         });
  238.     }
  239.  
  240.     // Variables declaration - do not modify                    
  241.     private javax.swing.JButton jButton1;
  242.     private javax.swing.JButton jButton2;
  243.     private javax.swing.JLabel jLabel1;
  244.     private javax.swing.JLabel jLabel2;
  245.     private javax.swing.JLabel jLabel3;
  246.     private javax.swing.JLabel jLabel4;
  247.     private javax.swing.JLabel jLabel5;
  248.     private javax.swing.JScrollPane jScrollPane1;
  249.     private javax.swing.JTextArea jTextArea1;
  250.     private javax.swing.JTextField jTextField1;
  251.     private javax.swing.JTextField jTextField2;
  252.     private javax.swing.JTextField jTextField3;
  253.     private javax.swing.JTextField jTextField4;
  254.     // End of variables declaration                  
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement