Ошибка - Java

Узнай цену своей работы

Формулировка задачи:

Здравствуйте, имеется программа моделирования автономной матричной линейной последовательности машины (АМЛПМ) над полем GF(2). При вводе степени полиномов выдает ошибку, помогите разобраться: Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 3 at lasthope.matr.matrA(matr.java:13) at lasthope.amlpm.actionPerformed(amlpm.java:188) at java.awt.Button.processActionEvent(Unknown Source) at java.awt.Button.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$500(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
 
import javax.swing.*;

public class amlpm extends Frame implements ActionListener, MouseListener {
    int sost=1;
    int m=0,n=0, pla=0, plb=0;
    int [][][]s=null; int [][]a=null; int [][]b=null;
    int prS=0, prA=0, prB=0, row=-1, column=-1;
    ScrollPane pnA,pnB,pnS;
    TextField rang;
    Frame w; MenuBar mb; 
    Menu m0;
    List t1,t3; List t2, t4; Label l10;
    
    TextField tma,tmb, tms, psp, hm;
    Button b1, bcn,bcp;
    JTable mA, mB, mS=new JTable();
    public static void main(String[] args) {
        amlpm a = new amlpm();
         }
    public amlpm() throws HeadlessException {
    super();
    w=new Frame("АМЛПМ");
    System.out.println("Hello!");
    
    w.setSize(700,550);
    w.setBackground(Color.orange);
    w.setLayout(null);
    rang=new TextField();
    w.add(rang);
    rang.setBounds(535,75,50,20);
    w.add(new Label("Ранг начальной матрицы")).setBounds(485,50,150,20);
    b1=new Button("Вычислить");
    w.add(b1);
    b1.setEnabled(false);
    b1.setBounds(520,110,80,25);
    b1.setBackground(Color.white);
    b1.setFont(new Font(("Times New Roman"),1,12));
    b1.setForeground(Color.black);
    Label l1=new Label("Степень полинома А");
    w.add(l1);
    l1.setBounds(20,50,150,20);
    t1=new List();
    t1.add("2"); t1.add("3"); t1.add("4"); t1.add("5"); t1.add("6"); t1.add("7"); t1.add("8"); t1.add("9"); t1.add("10");
    t1.add("11"); t1.add("12");t1.add("13");t1.add("14");t1.add("15");t1.add("16");
    
    w.add(t1);
    t1.setBounds(30,75,70,20);
    
    Label l2=new Label("Коэффициенты полинома А");
    w.add(l2);
    l2.setBounds(10,110,160,20);
    t2=new List();
    w.add(t2);
    t2.setBounds(25,135,70,20);
        
    Label l3=new Label("Степень полинома В");
    w.add(l3);
    l3.setBounds(280,50,150,20);
    t3=new List();
        t3.add("2"); t3.add("3"); t3.add("4"); t3.add("5"); t3.add("6"); t3.add("7"); t3.add("8"); t3.add("9"); t3.add("10");
    t3.add("11"); t3.add("12");t3.add("13");t3.add("14");t3.add("15");t3.add("16");
    
    w.add(t3);
    t3.setBounds(295,75,70,20);
    Label l4=new Label("Коэффициенты полинома В");
    w.add(l4);
    l4.setBounds(250,110,160,20);
    t4=new List();
    w.add(t4);
    t4.setBounds(290,135,70,20);
    
    Label l5=new Label("Период матрицы А");
    w.add(l5);
    l5.setBounds(10,170,150,20);
    tma=new TextField();
    w.add(tma);
    tma.setBounds(25,190,70,20);
    Label l6=new Label("Период матрицы В");
    w.add(l6);
    l6.setBounds(255,170,150,20);
    tmb=new TextField();
    w.add(tmb);
    tmb.setBounds(290,190,70,20);
    
    Label l7=new Label("Период матрицы S");
    w.add(l7);
    l7.setBounds(490,170,150,20);
    tms=new TextField();
    w.add(tms);
    tms.setBounds(530,190,70,20);
    
    Label l8=new Label("матрица А");
    w.add(l8);
    l8.setBounds(60,240,90,20);
    pnA=new ScrollPane(); 
    w.add(pnA);
    pnA.setBounds(20,265,150,150);
    pnA.doLayout();
    
    Label l9=new Label("матрица В");
    w.add(l9);
    l9.setBounds(300,240,90,20);
    pnB=new ScrollPane(); 
    w.add(pnB);
    pnB.setBounds(250,265,150,150);
    pnB.doLayout();
    
    l10=new Label("матрица S[0]");
    l10.setForeground(Color.red);
    w.add(l10);
    l10.setBounds(540,240,90,20);
    pnS=new ScrollPane(); 
    w.add(pnS);
    pnS.setBounds(500,265,150,150);
    pnS.doLayout();
    
    Label ll=new Label("Состояния");
    ll.setFont(new Font("Times New Roman",1,13));
    w.add(ll);
    ll.setBounds(540,420,120,20);
    bcn=new Button(">>");
    w.add(bcn);
    bcn.setEnabled(false);
    bcn.setBounds(580,440,25,25);
    bcn.setBackground(Color.white);
    bcn.setForeground(Color.black);
    bcn.setFont(new Font("Times New Roman",0,13));
    
    bcp=new Button("<<");
    w.add(bcp);
    bcp.setEnabled(false);
    bcp.setBounds(540,440,25,25);
    bcp.setBackground(Color.white);
    bcp.setForeground(Color.black);
    bcp.setFont(new Font("Times New Roman",0,13));
    
    Label hem=new Label("Вес Хэмминга");
    w.add(hem);
    hem.setBounds(25,450,85,20);
    hm=new TextField();
    w.add(hm);
    hm.setBounds(120,450,70,20);
    Label psl=new Label("ПСП");
    w.add(psl);
    psl.setBounds(15,495,30,20);
    psp=new TextField();
    psp.setForeground(Color.blue);
    w.add(psp);
    psp.setFont(new Font("Times New Roman",0,13));
    psp.setBounds(50,495,470,23);
    w.setVisible(true);
        w.addWindowListener(new WindowAdapter() {   
        
           public void windowClosing(WindowEvent g) 
           {    
               w.dispose(); 
               System.exit(0); 
               }      
                  } );
    
    t1.addMouseListener(this);
    t2.addMouseListener(this);
    t4.addMouseListener(this);
    t3.addMouseListener(this);
    b1.addActionListener(this);
    bcn.addActionListener(this);
    bcp.addActionListener(this);
    }
    public void actionPerformed(ActionEvent e){
        
        if (e.getActionCommand().equals("Вычислить")){
            sost=0;
                        
        if (n!=0) {
            if (t2.getSelectedItem()!=null){
                pla=Integer.parseInt(t2.getSelectedItem().substring(0,t2.getSelectedItem().length()-1));
            a=matr.matrA(n,pla);
            mA=new JTable(n,n);
            mA.setGridColor(Color.blue);
            mA.setIgnoreRepaint(true);
            mA.doLayout();
            mA.setEnabled(false);
            mA.setIntercellSpacing(new Dimension(3,3));
                    
            mA.setRowHeight(20);
            
            mA.sizeColumnsToFit(37);
                        
            for (int i=0;i<n;i++)
            for (int j=0;j<n;j++){
                String str=Integer.toString(a[i][j]);   
                mA.setValueAt(str,i,j);}
            
            pnA.removeAll();
            pnA.add(mA);
            pnA.doLayout();
                            
            prA=matr.period(a);
            tma.setText(Integer.toString(prA));
            }
            else msg("Не выбраны коэффициенты полинома A");
            }
            
            if (m!=0)
                if (t4.getSelectedItem()!=null){                
                plb=Integer.parseInt(t4.getSelectedItem().substring(0,t4.getSelectedItem().length()-1));
                b=matr.matrB(m,plb);
                mB=new JTable(m,m);
                mB.setGridColor(Color.blue);
                mB.setIntercellSpacing(new Dimension(3,3));
                mB.setIgnoreRepaint(true);
                mB.doLayout();
                mB.setEnabled(false);
                mB.setRowHeight(20);
                mB.sizeColumnsToFit(7);
                String str="";
                for (int i=0;i<m;i++)
                for (int j=0;j<m;j++){
                    str=Integer.toString(b[i][j]);
                    mB.setValueAt(str,i,j);
                }
                pnB.removeAll();
                pnB.add(mB);
                pnB.doLayout();
                prB=matr.period(b);
                tmb.setText(Integer.toString(prB));
            }
                else msg("Не выбраны коэффициенты полинома B");
            int rg=0;
            boolean rr=false;
            if (!rang.getText().isEmpty()){
            rg=Integer.parseInt(rang.getText());
            if ((rg>n)&(rg>m))
                msg("Ранг превышает размер матрицы");
            else rr=true;
                }
            else msg("Введите ранг!");
            
                if ((n!=0)&(m!=0)&(rr==true)){
                    l10.setText("матрица S[0]");
                    String str;
                    int kf=0;
                    if (m*n>100) kf=750;
                    if ((m*n>60)&(m*n<100)) kf=1400;
                    if (m*n<60) kf=1700;
                    s=new int[kf*m*n][n][m];
                    s[0]=matr.matrS0(n,m,rg);
                                                    
                    mS=new JTable(n,m);
                    
                    mS.setFont(new Font("Times New Roman",1,14));
                    mS.setSelectionBackground(Color.yellow);
                    mS.setCellSelectionEnabled(true);
                    mS.addMouseListener(this);
                    mB.setIgnoreRepaint(true);
                    mS.doLayout();
                    
                    mS.setGridColor(Color.red);
                    mS.setIgnoreRepaint(true);
                    mS.setRowHeight(20);
                    mS.sizeColumnsToFit(307);
                    mS.setIntercellSpacing(new Dimension(5,5));

                    for (int i=0;i<n;i++)
                        for (int j=0;j<m;j++){
                            str=Integer.toString(s[0][i][j]);
                            mS.setValueAt(str,i,j); }
                    
                    pnS.removeAll();
                    pnS.add(mS);
                    pnS.doLayout();
                                    
                                }
    if ((a!=null)&(b!=null)&(s!=null)){
    for (int k=1;;k++){
    s[k]=matr.product(a,b,s[k-1]);
    int pr=0;
    for (int i=0;i<a.length;i++)
        for (int j=0;j<b.length;j++)
            if (s[k][i][j]==s[0][i][j]) pr++;
    if (pr==a.length*b.length) {prS=k;  break;}
    }
    bcn.setEnabled(true);
    if (prS!=0) tms.setText(Integer.toString(prS));
    }
    
    }
            
        if (e.getActionCommand().equals(">>")){
        if ((s[0]!=null)&(a!=null)&(b!=null)&(sost<prS)){
            sost++;
            String str;
            mS.removeAll();
            mS.setForeground(new Color((17+sost*30)%256 ,(6+sost*20)%256,(3+sost*35)%256));
            System.out.println("sost="+sost);
            for (int i=0;i<n;i++)
                for (int j=0;j<m;j++){
                    str=Integer.toString(s[sost][i][j]);
                    mS.setValueAt(str,i,j);     
            }
            l10.setText("матрица S["+sost+"]");
            l10.setForeground(Color.red);

            }
        
    }

    if (e.getActionCommand().equals("<<")){
            
        if ((s[1]!=null)&(sost>0)){
            String str;
            sost--;
            mS.removeAll();
            for (int i=0;i<n;i++)
                for (int j=0;j<m;j++){
                    str=Integer.toString(s[sost][i][j]);
                    mS.setValueAt(str,i,j); 
            }
            l10.setText("матрица S["+sost+"]");
            
        }
        
    }
    if (sost>0) bcp.setEnabled(true);
    else bcp.setEnabled(false);
    if (sost>=prS-1) bcn.setEnabled(false);
    else bcn.setEnabled(true);
        }

    public static void koeff (int n, List t){
        if (n==2) t.add("7H");
        if (n==3) t.add("13F");
        if (n==4){
            t.add("23F");   
            t.add("37D"); }
        if (n==5){
            t.add("45E");
            t.add("75G");
            t.add("67H");
        }
        if (n==6){
            t.add("103F");
            t.add("127B");
            t.add("147H");
            t.add("111A");
        }
        if (n==7){
            t.add("211E");
            t.add("217E");
            t.add("235E");
        }
        if (n==8){
            t.add("435E");
            t.add("567B");
            t.add("763D");
        }
        
        if (n==9){
            t.add("1021E");
            t.add("1131E");
            t.add("1461G");
        }
        if (n==10){
            t.add("2011E");
            t.add("2017B");
            t.add("2415E");
        }
        if (n==11){
            t.add("4005E");
            t.add("4445E");
            t.add("4215E");
            t.add("4757B");
        }
        if (n==12){
            t.add("10123F");
            t.add("12133B");
            t.add("10115A");
        }

    }
    public void mouseClicked (MouseEvent g){
        
        psp.setText("");    
        hm.setText("");
        if (g.getComponent()==t1){
            if (mA!=null) pnA.removeAll(); 
            if (mS!=null) pnS.removeAll();
            t2.removeAll();
            n=Integer.parseInt(t1.getSelectedItem());
            koeff(n,t2);        }
        if (g.getComponent()==t3){
            if (mB!=null) pnB.removeAll();
            if (mS!=null) pnS.removeAll();
            t4.removeAll();
            m=Integer.parseInt(t3.getSelectedItem());
            koeff(m,t4); }
        
        if ((n!=0)&(m!=0)) b1.setEnabled(true);

        if (g.getComponent()==mS){
            psp.setText(""); hm.setText("");
            int psl[]=null;
            if (mS!=null){
            row=mS.getSelectedRow();
            column=mS.getSelectedColumn();
            if ((row!=-1)&(column!=-1))
            psl=posl.posled(s,prS,row,column);
            String str="";
            if (psl!=null){
                for (int i=0;i<psl.length;i++)
                    str=str+psl[i];
                
                psp.setText(str);
                int hem=posl.hemming(psl);
                hm.setText(Integer.toString(hem));}}
            
        }           
        
        }
    boolean ind;
    public void msg(String text){
        final Dialog d=new Dialog(w);
        d.setBackground(Color.red);
        d.setSize(260,80);
        d.setLocation(100,100);
        d.setLayout(null);
        Label l=new Label(text);
        d.add(l);
        
        l.setBounds(10,28,240,20);
        final Button b=new Button("OK");
        d.add(b);
        d.setBackground(Color.yellow);
        b.setBounds(90,53,50,20);
        b.setBackground(Color.white);
        if (ind==false) d.setVisible(true);
        ind=true;
        
        b.addActionListener(new ActionListener(){   
            public void actionPerformed (ActionEvent r){
                if (r.getSource()==b) {d.dispose(); ind=false;}}});
    }
    @Override
    public void mouseEntered(MouseEvent arg0) {
        // TODO Auto-generated method stub
        
    }
    @Override
    public void mouseExited(MouseEvent arg0) {
        // TODO Auto-generated method stub
        
    }
    @Override
    public void mousePressed(MouseEvent arg0) {
        // TODO Auto-generated method stub
        
    }
    @Override
    public void mouseReleased(MouseEvent arg0) {
        // TODO Auto-generated method stub
        
    } }

Решение задачи: «Ошибка»

textual
Листинг программы
public class posl {
    public posl() { super();}
 
    public static int[] posled (int s[][][], int size, int row, int column){ // генерация ПСП
        int psl[]=new int[size];
        for (int k=0;k<size;k++)
            for (int i=0;i<s[k].length;i++)
                for (int j=0;j<s[k][i].length;j++)
                    if ((i==row)&(j==column)) 
                        psl[k]=s[k][i][j];
                
    return psl; 
    }
 
    public static int hemming (int[]psl){ // вычисление веса Хэмминга
        int k=0;
        for (int i=0;i<psl.length;i++)
            if (psl[i]==1) k++;
            return k;
    }
    }

Оцени полезность:

10   голосов , оценка 4.1 из 5