Advertisement
Guest User

Untitled

a guest
Jun 7th, 2020
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.73 KB | None | 0 0
  1. package coeffQuartics;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.util.Random;
  7.  
  8. import javax.swing.JButton;
  9. import javax.swing.JPanel;
  10. import javax.swing.JRadioButton;
  11. import javax.swing.JScrollPane;
  12. import javax.swing.JTable;
  13.  
  14. import javax.swing.WindowConstants;
  15. import javax.swing.table.DefaultTableModel;
  16. import javax.swing.table.TableModel;
  17. import javax.swing.SwingUtilities;
  18.  
  19.  
  20.  
  21. public class NewJFrame extends javax.swing.JFrame {
  22.  
  23.     {
  24.         //Set Look & Feel
  25.         try {
  26.             javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
  27.         } catch(Exception e) {
  28.             e.printStackTrace();
  29.         }
  30.     }
  31.  
  32.     private JRadioButton jRadioButton1;
  33.     private JPanel jPanel1;
  34.     private JScrollPane jScrollPane1;
  35.     private JButton jButton1;
  36.     private JTable jTable1;
  37.     private JRadioButton jRadioButton3;
  38.     private JRadioButton jRadioButton2;
  39.  
  40.     /**
  41.     * Auto−generated main method to display this JFrame
  42.     */
  43.     public static void main(String[] args) {
  44.         SwingUtilities.invokeLater(new Runnable() {
  45.             public void run() {
  46.                 NewJFrame inst = new NewJFrame();
  47.                 inst.setLocationRelativeTo(null);
  48.                 inst.setVisible(true);
  49.             }
  50.         });
  51.     }
  52.    
  53.     public NewJFrame() {
  54.         super();
  55.         initGUI();
  56.     }
  57.    
  58.     private void initGUI() {
  59.         try {
  60.             setTitle("Współczynniki wielomianu czwartego stopnia");
  61.             setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
  62.             {
  63.                 jPanel1 = new JPanel();
  64.                 getContentPane().add(jPanel1, BorderLayout.CENTER);
  65.                 jPanel1.setPreferredSize(new java.awt.Dimension(392, 266));
  66.                 jPanel1.setLayout(null);
  67.                 {
  68.                     jRadioButton1 = new JRadioButton();
  69.                     jPanel1.add(jRadioButton1);
  70.                     jRadioButton1.setText("Pierwiastki rzeczywiste");
  71.                     jRadioButton1.setBounds(12, 4, 196, 18);
  72.                     jRadioButton1.addActionListener(new ActionListener() {
  73.                         public void actionPerformed(ActionEvent evt) {
  74.                             jRadioButton1ActionPerformed(evt);
  75.                         }
  76.                     });
  77.                 }
  78.                 {
  79.                     jRadioButton2 = new JRadioButton();
  80.                     jPanel1.add(jRadioButton2);
  81.                     jRadioButton2.setText("Iloczyn dwóch trójmianów kwadratowych");
  82.                     jRadioButton2.setBounds(12, 29, 272, 18);
  83.                     jRadioButton2.addActionListener(new ActionListener() {
  84.                         public void actionPerformed(ActionEvent evt) {
  85.                             jRadioButton2ActionPerformed(evt);
  86.                         }
  87.                     });
  88.                 }
  89.                 {
  90.                     jRadioButton3 = new JRadioButton();
  91.                     jPanel1.add(jRadioButton3);
  92.                     jRadioButton3.setText("Losowe wspó\u0142czynniki");
  93.                     jRadioButton3.setBounds(12, 54, 210, 21);
  94.                     jRadioButton3.addActionListener(new ActionListener() {
  95.                         public void actionPerformed(ActionEvent evt) {
  96.                             jRadioButton3ActionPerformed(evt);
  97.                         }
  98.                     });
  99.                 }
  100.                 {
  101.                     jScrollPane1 = new JScrollPane();
  102.                     jPanel1.add(jScrollPane1);
  103.                     jScrollPane1.setBounds(7, 95, 375, 40);
  104.                     {
  105.                         TableModel jTable1Model =
  106.                                 new DefaultTableModel(
  107.                                         new String[][] { { "", "" } },
  108.                                         new String[] { "a4", "a3","a2","a1","a0" });
  109.                         ((DefaultTableModel) jTable1Model).setColumnCount(5);
  110.                         ((DefaultTableModel) jTable1Model).setRowCount(1);
  111.                         jTable1 = new JTable();
  112.                         jScrollPane1.setViewportView(jTable1);
  113.                         jTable1.setBounds(27, 132, 365, 16);
  114.                         jTable1.setModel(jTable1Model);
  115.                         jTable1.setCellSelectionEnabled(true);
  116.                         jTable1.setColumnSelectionAllowed(true);
  117.                         jTable1.getTableHeader().setOpaque(true);
  118.                         jTable1.getTableHeader().setBounds(0, 0, 375, 30);
  119.                     }
  120.                 }
  121.                 {
  122.                     jButton1 = new JButton();
  123.                     jPanel1.add(jButton1);
  124.                     jButton1.setText("Losuj");
  125.                     jButton1.setBounds(155, 177, 73, 28);
  126.                     jButton1.addActionListener(new ActionListener() {
  127.                         public void actionPerformed(ActionEvent evt) {
  128.                             jButton1ActionPerformed(evt);
  129.                         }
  130.                     });
  131.                 }
  132.             }
  133.             pack();
  134.             setSize(400, 300);
  135.         } catch (Exception e) {
  136.             e.printStackTrace();
  137.         }
  138.     }
  139.    
  140.     private void jRadioButton1ActionPerformed(ActionEvent evt) {
  141.         System.out.println("jRadioButton1.actionPerformed, event="+evt);
  142.         //TODO add your code for jRadioButton1.actionPerformed
  143.         jRadioButton1.setSelected(true);
  144.         jRadioButton2.setSelected(false);
  145.         jRadioButton3.setSelected(false);
  146.     }
  147.    
  148.     private void jRadioButton2ActionPerformed(ActionEvent evt) {
  149.         System.out.println("jRadioButton2.actionPerformed, event="+evt);
  150.         //TODO add your code for jRadioButton2.actionPerformed
  151.         jRadioButton1.setSelected(false);
  152.         jRadioButton2.setSelected(true);
  153.         jRadioButton3.setSelected(false);
  154.     }
  155.    
  156.     private void jRadioButton3ActionPerformed(ActionEvent evt) {
  157.         System.out.println("jRadioButton3.actionPerformed, event="+evt);
  158.         //TODO add your code for jRadioButton3.actionPerformed
  159.         jRadioButton1.setSelected(false);
  160.         jRadioButton2.setSelected(false);
  161.         jRadioButton3.setSelected(true);
  162.     }
  163.    
  164.     private void jButton1ActionPerformed(ActionEvent evt) {
  165.         System.out.println("jButton1.actionPerformed, event="+evt);
  166.         //TODO add your code for jButton1.actionPerformed
  167.          int p,q,k;
  168.             Random r=new Random();
  169.             double [] a=new double[3];
  170.             double [] b=new double[3];
  171.             double [] c=new double[5];
  172.             double [] x=new double[5];
  173.            
  174.             if(jRadioButton1.isSelected()){
  175.                 x[0]=1.0;
  176.               for(int i=1;i<=4;i++){
  177.                 p=(1-2*r.nextInt(2))*r.nextInt(10);
  178.                 q=1+r.nextInt(10);
  179.                 x[i]=(double)(p)/q;
  180.                 x[0]*=(double)q;
  181.              }
  182.              for(int i=0;i<=4;i++)
  183.                 if(i%2==0) jTable1.setValueAt(x[0]*viete(4,i,x),0 ,i);
  184.                 else jTable1.setValueAt(-x[0]*viete(4,i,x),0 ,i);
  185.             }
  186.             if(jRadioButton2.isSelected()){
  187.                 a[2]=(1+r.nextInt(9))*(1-2*r.nextInt(2));
  188.                 a[1]=r.nextInt(10)*(1-2*r.nextInt(2));
  189.                 a[0]=r.nextInt(10)*(1-2*r.nextInt(2));
  190.                 b[2]=(1+r.nextInt(9))*(1-2*r.nextInt(2));
  191.                 b[1]=r.nextInt(10)*(1-2*r.nextInt(2));
  192.                 b[0]=r.nextInt(10)*(1-2*r.nextInt(2));
  193.                 c[4]=a[2]*b[2];
  194.                 c[3]=a[2]*b[1]+a[1]*b[2];
  195.                 c[2]=a[2]*b[0]+a[1]*b[1]+a[0]*b[2];
  196.                 c[1]=a[1]*b[0]+a[0]*b[1];
  197.                 c[0]=a[0]*b[0];
  198.                 for(k=0;k<=4;k++)
  199.                     jTable1.setValueAt(c[k],0,4-k);
  200.                
  201.             }
  202.             if(jRadioButton3.isSelected()){
  203.                 c[4]=(1+r.nextInt(9))*(1-2*r.nextInt(2));
  204.                 for(k=3;k>=0;k--)
  205.                     c[k]=r.nextInt(10)*(1-2*r.nextInt(2));
  206.                 for(k=0;k<=4;k++)
  207.                     jTable1.setValueAt(c[k],0,4-k);
  208.             }
  209.     }
  210.  
  211.      private double viete(int n,int k,double[] x){
  212.             if(k==0) return 1.0;
  213.             else if (n==0) return 0.0;
  214.             else return viete(n-1,k,x)+viete(n-1,k-1,x)*x[n];
  215.         }
  216.  
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement