Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package coeffQuartics;
- import java.awt.BorderLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.util.Random;
- import javax.swing.JButton;
- import javax.swing.JPanel;
- import javax.swing.JRadioButton;
- import javax.swing.JScrollPane;
- import javax.swing.JTable;
- import javax.swing.WindowConstants;
- import javax.swing.table.DefaultTableModel;
- import javax.swing.table.TableModel;
- import javax.swing.SwingUtilities;
- public class NewJFrame extends javax.swing.JFrame {
- {
- //Set Look & Feel
- try {
- javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
- } catch(Exception e) {
- e.printStackTrace();
- }
- }
- private JRadioButton jRadioButton1;
- private JPanel jPanel1;
- private JScrollPane jScrollPane1;
- private JButton jButton1;
- private JTable jTable1;
- private JRadioButton jRadioButton3;
- private JRadioButton jRadioButton2;
- /**
- * Auto−generated main method to display this JFrame
- */
- public static void main(String[] args) {
- SwingUtilities.invokeLater(new Runnable() {
- public void run() {
- NewJFrame inst = new NewJFrame();
- inst.setLocationRelativeTo(null);
- inst.setVisible(true);
- }
- });
- }
- public NewJFrame() {
- super();
- initGUI();
- }
- private void initGUI() {
- try {
- setTitle("Współczynniki wielomianu czwartego stopnia");
- setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
- {
- jPanel1 = new JPanel();
- getContentPane().add(jPanel1, BorderLayout.CENTER);
- jPanel1.setPreferredSize(new java.awt.Dimension(392, 266));
- jPanel1.setLayout(null);
- {
- jRadioButton1 = new JRadioButton();
- jPanel1.add(jRadioButton1);
- jRadioButton1.setText("Pierwiastki rzeczywiste");
- jRadioButton1.setBounds(12, 4, 196, 18);
- jRadioButton1.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent evt) {
- jRadioButton1ActionPerformed(evt);
- }
- });
- }
- {
- jRadioButton2 = new JRadioButton();
- jPanel1.add(jRadioButton2);
- jRadioButton2.setText("Iloczyn dwóch trójmianów kwadratowych");
- jRadioButton2.setBounds(12, 29, 272, 18);
- jRadioButton2.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent evt) {
- jRadioButton2ActionPerformed(evt);
- }
- });
- }
- {
- jRadioButton3 = new JRadioButton();
- jPanel1.add(jRadioButton3);
- jRadioButton3.setText("Losowe wspó\u0142czynniki");
- jRadioButton3.setBounds(12, 54, 210, 21);
- jRadioButton3.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent evt) {
- jRadioButton3ActionPerformed(evt);
- }
- });
- }
- {
- jScrollPane1 = new JScrollPane();
- jPanel1.add(jScrollPane1);
- jScrollPane1.setBounds(7, 95, 375, 40);
- {
- TableModel jTable1Model =
- new DefaultTableModel(
- new String[][] { { "", "" } },
- new String[] { "a4", "a3","a2","a1","a0" });
- ((DefaultTableModel) jTable1Model).setColumnCount(5);
- ((DefaultTableModel) jTable1Model).setRowCount(1);
- jTable1 = new JTable();
- jScrollPane1.setViewportView(jTable1);
- jTable1.setBounds(27, 132, 365, 16);
- jTable1.setModel(jTable1Model);
- jTable1.setCellSelectionEnabled(true);
- jTable1.setColumnSelectionAllowed(true);
- jTable1.getTableHeader().setOpaque(true);
- jTable1.getTableHeader().setBounds(0, 0, 375, 30);
- }
- }
- {
- jButton1 = new JButton();
- jPanel1.add(jButton1);
- jButton1.setText("Losuj");
- jButton1.setBounds(155, 177, 73, 28);
- jButton1.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent evt) {
- jButton1ActionPerformed(evt);
- }
- });
- }
- }
- pack();
- setSize(400, 300);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- private void jRadioButton1ActionPerformed(ActionEvent evt) {
- System.out.println("jRadioButton1.actionPerformed, event="+evt);
- //TODO add your code for jRadioButton1.actionPerformed
- jRadioButton1.setSelected(true);
- jRadioButton2.setSelected(false);
- jRadioButton3.setSelected(false);
- }
- private void jRadioButton2ActionPerformed(ActionEvent evt) {
- System.out.println("jRadioButton2.actionPerformed, event="+evt);
- //TODO add your code for jRadioButton2.actionPerformed
- jRadioButton1.setSelected(false);
- jRadioButton2.setSelected(true);
- jRadioButton3.setSelected(false);
- }
- private void jRadioButton3ActionPerformed(ActionEvent evt) {
- System.out.println("jRadioButton3.actionPerformed, event="+evt);
- //TODO add your code for jRadioButton3.actionPerformed
- jRadioButton1.setSelected(false);
- jRadioButton2.setSelected(false);
- jRadioButton3.setSelected(true);
- }
- private void jButton1ActionPerformed(ActionEvent evt) {
- System.out.println("jButton1.actionPerformed, event="+evt);
- //TODO add your code for jButton1.actionPerformed
- int p,q,k;
- Random r=new Random();
- double [] a=new double[3];
- double [] b=new double[3];
- double [] c=new double[5];
- double [] x=new double[5];
- if(jRadioButton1.isSelected()){
- x[0]=1.0;
- for(int i=1;i<=4;i++){
- p=(1-2*r.nextInt(2))*r.nextInt(10);
- q=1+r.nextInt(10);
- x[i]=(double)(p)/q;
- x[0]*=(double)q;
- }
- for(int i=0;i<=4;i++)
- if(i%2==0) jTable1.setValueAt(x[0]*viete(4,i,x),0 ,i);
- else jTable1.setValueAt(-x[0]*viete(4,i,x),0 ,i);
- }
- if(jRadioButton2.isSelected()){
- a[2]=(1+r.nextInt(9))*(1-2*r.nextInt(2));
- a[1]=r.nextInt(10)*(1-2*r.nextInt(2));
- a[0]=r.nextInt(10)*(1-2*r.nextInt(2));
- b[2]=(1+r.nextInt(9))*(1-2*r.nextInt(2));
- b[1]=r.nextInt(10)*(1-2*r.nextInt(2));
- b[0]=r.nextInt(10)*(1-2*r.nextInt(2));
- c[4]=a[2]*b[2];
- c[3]=a[2]*b[1]+a[1]*b[2];
- c[2]=a[2]*b[0]+a[1]*b[1]+a[0]*b[2];
- c[1]=a[1]*b[0]+a[0]*b[1];
- c[0]=a[0]*b[0];
- for(k=0;k<=4;k++)
- jTable1.setValueAt(c[k],0,4-k);
- }
- if(jRadioButton3.isSelected()){
- c[4]=(1+r.nextInt(9))*(1-2*r.nextInt(2));
- for(k=3;k>=0;k--)
- c[k]=r.nextInt(10)*(1-2*r.nextInt(2));
- for(k=0;k<=4;k++)
- jTable1.setValueAt(c[k],0,4-k);
- }
- }
- private double viete(int n,int k,double[] x){
- if(k==0) return 1.0;
- else if (n==0) return 0.0;
- else return viete(n-1,k,x)+viete(n-1,k-1,x)*x[n];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement