Ошибка - 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)
Листинг программы
  1. import java.awt.*;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import java.awt.event.MouseEvent;
  5. import java.awt.event.MouseListener;
  6. import java.awt.event.WindowAdapter;
  7. import java.awt.event.WindowEvent;
  8. import javax.swing.*;
  9.  
  10. public class amlpm extends Frame implements ActionListener, MouseListener {
  11. int sost=1;
  12. int m=0,n=0, pla=0, plb=0;
  13. int [][][]s=null; int [][]a=null; int [][]b=null;
  14. int prS=0, prA=0, prB=0, row=-1, column=-1;
  15. ScrollPane pnA,pnB,pnS;
  16. TextField rang;
  17. Frame w; MenuBar mb;
  18. Menu m0;
  19. List t1,t3; List t2, t4; Label l10;
  20. TextField tma,tmb, tms, psp, hm;
  21. Button b1, bcn,bcp;
  22. JTable mA, mB, mS=new JTable();
  23. public static void main(String[] args) {
  24. amlpm a = new amlpm();
  25. }
  26. public amlpm() throws HeadlessException {
  27. super();
  28. w=new Frame("АМЛПМ");
  29. System.out.println("Hello!");
  30. w.setSize(700,550);
  31. w.setBackground(Color.orange);
  32. w.setLayout(null);
  33. rang=new TextField();
  34. w.add(rang);
  35. rang.setBounds(535,75,50,20);
  36. w.add(new Label("Ранг начальной матрицы")).setBounds(485,50,150,20);
  37. b1=new Button("Вычислить");
  38. w.add(b1);
  39. b1.setEnabled(false);
  40. b1.setBounds(520,110,80,25);
  41. b1.setBackground(Color.white);
  42. b1.setFont(new Font(("Times New Roman"),1,12));
  43. b1.setForeground(Color.black);
  44. Label l1=new Label("Степень полинома А");
  45. w.add(l1);
  46. l1.setBounds(20,50,150,20);
  47. t1=new List();
  48. 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");
  49. t1.add("11"); t1.add("12");t1.add("13");t1.add("14");t1.add("15");t1.add("16");
  50. w.add(t1);
  51. t1.setBounds(30,75,70,20);
  52. Label l2=new Label("Коэффициенты полинома А");
  53. w.add(l2);
  54. l2.setBounds(10,110,160,20);
  55. t2=new List();
  56. w.add(t2);
  57. t2.setBounds(25,135,70,20);
  58. Label l3=new Label("Степень полинома В");
  59. w.add(l3);
  60. l3.setBounds(280,50,150,20);
  61. t3=new List();
  62. 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");
  63. t3.add("11"); t3.add("12");t3.add("13");t3.add("14");t3.add("15");t3.add("16");
  64. w.add(t3);
  65. t3.setBounds(295,75,70,20);
  66. Label l4=new Label("Коэффициенты полинома В");
  67. w.add(l4);
  68. l4.setBounds(250,110,160,20);
  69. t4=new List();
  70. w.add(t4);
  71. t4.setBounds(290,135,70,20);
  72. Label l5=new Label("Период матрицы А");
  73. w.add(l5);
  74. l5.setBounds(10,170,150,20);
  75. tma=new TextField();
  76. w.add(tma);
  77. tma.setBounds(25,190,70,20);
  78. Label l6=new Label("Период матрицы В");
  79. w.add(l6);
  80. l6.setBounds(255,170,150,20);
  81. tmb=new TextField();
  82. w.add(tmb);
  83. tmb.setBounds(290,190,70,20);
  84. Label l7=new Label("Период матрицы S");
  85. w.add(l7);
  86. l7.setBounds(490,170,150,20);
  87. tms=new TextField();
  88. w.add(tms);
  89. tms.setBounds(530,190,70,20);
  90. Label l8=new Label("матрица А");
  91. w.add(l8);
  92. l8.setBounds(60,240,90,20);
  93. pnA=new ScrollPane();
  94. w.add(pnA);
  95. pnA.setBounds(20,265,150,150);
  96. pnA.doLayout();
  97. Label l9=new Label("матрица В");
  98. w.add(l9);
  99. l9.setBounds(300,240,90,20);
  100. pnB=new ScrollPane();
  101. w.add(pnB);
  102. pnB.setBounds(250,265,150,150);
  103. pnB.doLayout();
  104. l10=new Label("матрица S[0]");
  105. l10.setForeground(Color.red);
  106. w.add(l10);
  107. l10.setBounds(540,240,90,20);
  108. pnS=new ScrollPane();
  109. w.add(pnS);
  110. pnS.setBounds(500,265,150,150);
  111. pnS.doLayout();
  112. Label ll=new Label("Состояния");
  113. ll.setFont(new Font("Times New Roman",1,13));
  114. w.add(ll);
  115. ll.setBounds(540,420,120,20);
  116. bcn=new Button(">>");
  117. w.add(bcn);
  118. bcn.setEnabled(false);
  119. bcn.setBounds(580,440,25,25);
  120. bcn.setBackground(Color.white);
  121. bcn.setForeground(Color.black);
  122. bcn.setFont(new Font("Times New Roman",0,13));
  123. bcp=new Button("<<");
  124. w.add(bcp);
  125. bcp.setEnabled(false);
  126. bcp.setBounds(540,440,25,25);
  127. bcp.setBackground(Color.white);
  128. bcp.setForeground(Color.black);
  129. bcp.setFont(new Font("Times New Roman",0,13));
  130. Label hem=new Label("Вес Хэмминга");
  131. w.add(hem);
  132. hem.setBounds(25,450,85,20);
  133. hm=new TextField();
  134. w.add(hm);
  135. hm.setBounds(120,450,70,20);
  136. Label psl=new Label("ПСП");
  137. w.add(psl);
  138. psl.setBounds(15,495,30,20);
  139. psp=new TextField();
  140. psp.setForeground(Color.blue);
  141. w.add(psp);
  142. psp.setFont(new Font("Times New Roman",0,13));
  143. psp.setBounds(50,495,470,23);
  144. w.setVisible(true);
  145. w.addWindowListener(new WindowAdapter() {
  146. public void windowClosing(WindowEvent g)
  147. {
  148. w.dispose();
  149. System.exit(0);
  150. }
  151. } );
  152. t1.addMouseListener(this);
  153. t2.addMouseListener(this);
  154. t4.addMouseListener(this);
  155. t3.addMouseListener(this);
  156. b1.addActionListener(this);
  157. bcn.addActionListener(this);
  158. bcp.addActionListener(this);
  159. }
  160. public void actionPerformed(ActionEvent e){
  161. if (e.getActionCommand().equals("Вычислить")){
  162. sost=0;
  163. if (n!=0) {
  164. if (t2.getSelectedItem()!=null){
  165. pla=Integer.parseInt(t2.getSelectedItem().substring(0,t2.getSelectedItem().length()-1));
  166. a=matr.matrA(n,pla);
  167. mA=new JTable(n,n);
  168. mA.setGridColor(Color.blue);
  169. mA.setIgnoreRepaint(true);
  170. mA.doLayout();
  171. mA.setEnabled(false);
  172. mA.setIntercellSpacing(new Dimension(3,3));
  173. mA.setRowHeight(20);
  174. mA.sizeColumnsToFit(37);
  175. for (int i=0;i<n;i++)
  176. for (int j=0;j<n;j++){
  177. String str=Integer.toString(a[i][j]);
  178. mA.setValueAt(str,i,j);}
  179. pnA.removeAll();
  180. pnA.add(mA);
  181. pnA.doLayout();
  182. prA=matr.period(a);
  183. tma.setText(Integer.toString(prA));
  184. }
  185. else msg("Не выбраны коэффициенты полинома A");
  186. }
  187. if (m!=0)
  188. if (t4.getSelectedItem()!=null){
  189. plb=Integer.parseInt(t4.getSelectedItem().substring(0,t4.getSelectedItem().length()-1));
  190. b=matr.matrB(m,plb);
  191. mB=new JTable(m,m);
  192. mB.setGridColor(Color.blue);
  193. mB.setIntercellSpacing(new Dimension(3,3));
  194. mB.setIgnoreRepaint(true);
  195. mB.doLayout();
  196. mB.setEnabled(false);
  197. mB.setRowHeight(20);
  198. mB.sizeColumnsToFit(7);
  199. String str="";
  200. for (int i=0;i<m;i++)
  201. for (int j=0;j<m;j++){
  202. str=Integer.toString(b[i][j]);
  203. mB.setValueAt(str,i,j);
  204. }
  205. pnB.removeAll();
  206. pnB.add(mB);
  207. pnB.doLayout();
  208. prB=matr.period(b);
  209. tmb.setText(Integer.toString(prB));
  210. }
  211. else msg("Не выбраны коэффициенты полинома B");
  212. int rg=0;
  213. boolean rr=false;
  214. if (!rang.getText().isEmpty()){
  215. rg=Integer.parseInt(rang.getText());
  216. if ((rg>n)&(rg>m))
  217. msg("Ранг превышает размер матрицы");
  218. else rr=true;
  219. }
  220. else msg("Введите ранг!");
  221. if ((n!=0)&(m!=0)&(rr==true)){
  222. l10.setText("матрица S[0]");
  223. String str;
  224. int kf=0;
  225. if (m*n>100) kf=750;
  226. if ((m*n>60)&(m*n<100)) kf=1400;
  227. if (m*n<60) kf=1700;
  228. s=new int[kf*m*n][n][m];
  229. s[0]=matr.matrS0(n,m,rg);
  230. mS=new JTable(n,m);
  231. mS.setFont(new Font("Times New Roman",1,14));
  232. mS.setSelectionBackground(Color.yellow);
  233. mS.setCellSelectionEnabled(true);
  234. mS.addMouseListener(this);
  235. mB.setIgnoreRepaint(true);
  236. mS.doLayout();
  237. mS.setGridColor(Color.red);
  238. mS.setIgnoreRepaint(true);
  239. mS.setRowHeight(20);
  240. mS.sizeColumnsToFit(307);
  241. mS.setIntercellSpacing(new Dimension(5,5));
  242.  
  243. for (int i=0;i<n;i++)
  244. for (int j=0;j<m;j++){
  245. str=Integer.toString(s[0][i][j]);
  246. mS.setValueAt(str,i,j); }
  247. pnS.removeAll();
  248. pnS.add(mS);
  249. pnS.doLayout();
  250. }
  251. if ((a!=null)&(b!=null)&(s!=null)){
  252. for (int k=1;;k++){
  253. s[k]=matr.product(a,b,s[k-1]);
  254. int pr=0;
  255. for (int i=0;i<a.length;i++)
  256. for (int j=0;j<b.length;j++)
  257. if (s[k][i][j]==s[0][i][j]) pr++;
  258. if (pr==a.length*b.length) {prS=k; break;}
  259. }
  260. bcn.setEnabled(true);
  261. if (prS!=0) tms.setText(Integer.toString(prS));
  262. }
  263. }
  264. if (e.getActionCommand().equals(">>")){
  265. if ((s[0]!=null)&(a!=null)&(b!=null)&(sost<prS)){
  266. sost++;
  267. String str;
  268. mS.removeAll();
  269. mS.setForeground(new Color((17+sost*30)%256 ,(6+sost*20)%256,(3+sost*35)%256));
  270. System.out.println("sost="+sost);
  271. for (int i=0;i<n;i++)
  272. for (int j=0;j<m;j++){
  273. str=Integer.toString(s[sost][i][j]);
  274. mS.setValueAt(str,i,j);
  275. }
  276. l10.setText("матрица S["+sost+"]");
  277. l10.setForeground(Color.red);
  278.  
  279. }
  280. }
  281.  
  282. if (e.getActionCommand().equals("<<")){
  283. if ((s[1]!=null)&(sost>0)){
  284. String str;
  285. sost--;
  286. mS.removeAll();
  287. for (int i=0;i<n;i++)
  288. for (int j=0;j<m;j++){
  289. str=Integer.toString(s[sost][i][j]);
  290. mS.setValueAt(str,i,j);
  291. }
  292. l10.setText("матрица S["+sost+"]");
  293. }
  294. }
  295. if (sost>0) bcp.setEnabled(true);
  296. else bcp.setEnabled(false);
  297. if (sost>=prS-1) bcn.setEnabled(false);
  298. else bcn.setEnabled(true);
  299. }
  300.  
  301. public static void koeff (int n, List t){
  302. if (n==2) t.add("7H");
  303. if (n==3) t.add("13F");
  304. if (n==4){
  305. t.add("23F");
  306. t.add("37D"); }
  307. if (n==5){
  308. t.add("45E");
  309. t.add("75G");
  310. t.add("67H");
  311. }
  312. if (n==6){
  313. t.add("103F");
  314. t.add("127B");
  315. t.add("147H");
  316. t.add("111A");
  317. }
  318. if (n==7){
  319. t.add("211E");
  320. t.add("217E");
  321. t.add("235E");
  322. }
  323. if (n==8){
  324. t.add("435E");
  325. t.add("567B");
  326. t.add("763D");
  327. }
  328. if (n==9){
  329. t.add("1021E");
  330. t.add("1131E");
  331. t.add("1461G");
  332. }
  333. if (n==10){
  334. t.add("2011E");
  335. t.add("2017B");
  336. t.add("2415E");
  337. }
  338. if (n==11){
  339. t.add("4005E");
  340. t.add("4445E");
  341. t.add("4215E");
  342. t.add("4757B");
  343. }
  344. if (n==12){
  345. t.add("10123F");
  346. t.add("12133B");
  347. t.add("10115A");
  348. }
  349.  
  350. }
  351. public void mouseClicked (MouseEvent g){
  352. psp.setText("");
  353. hm.setText("");
  354. if (g.getComponent()==t1){
  355. if (mA!=null) pnA.removeAll();
  356. if (mS!=null) pnS.removeAll();
  357. t2.removeAll();
  358. n=Integer.parseInt(t1.getSelectedItem());
  359. koeff(n,t2); }
  360. if (g.getComponent()==t3){
  361. if (mB!=null) pnB.removeAll();
  362. if (mS!=null) pnS.removeAll();
  363. t4.removeAll();
  364. m=Integer.parseInt(t3.getSelectedItem());
  365. koeff(m,t4); }
  366. if ((n!=0)&(m!=0)) b1.setEnabled(true);
  367.  
  368. if (g.getComponent()==mS){
  369. psp.setText(""); hm.setText("");
  370. int psl[]=null;
  371. if (mS!=null){
  372. row=mS.getSelectedRow();
  373. column=mS.getSelectedColumn();
  374. if ((row!=-1)&(column!=-1))
  375. psl=posl.posled(s,prS,row,column);
  376. String str="";
  377. if (psl!=null){
  378. for (int i=0;i<psl.length;i++)
  379. str=str+psl[i];
  380. psp.setText(str);
  381. int hem=posl.hemming(psl);
  382. hm.setText(Integer.toString(hem));}}
  383. }
  384. }
  385. boolean ind;
  386. public void msg(String text){
  387. final Dialog d=new Dialog(w);
  388. d.setBackground(Color.red);
  389. d.setSize(260,80);
  390. d.setLocation(100,100);
  391. d.setLayout(null);
  392. Label l=new Label(text);
  393. d.add(l);
  394. l.setBounds(10,28,240,20);
  395. final Button b=new Button("OK");
  396. d.add(b);
  397. d.setBackground(Color.yellow);
  398. b.setBounds(90,53,50,20);
  399. b.setBackground(Color.white);
  400. if (ind==false) d.setVisible(true);
  401. ind=true;
  402. b.addActionListener(new ActionListener(){
  403. public void actionPerformed (ActionEvent r){
  404. if (r.getSource()==b) {d.dispose(); ind=false;}}});
  405. }
  406. @Override
  407. public void mouseEntered(MouseEvent arg0) {
  408. // TODO Auto-generated method stub
  409. }
  410. @Override
  411. public void mouseExited(MouseEvent arg0) {
  412. // TODO Auto-generated method stub
  413. }
  414. @Override
  415. public void mousePressed(MouseEvent arg0) {
  416. // TODO Auto-generated method stub
  417. }
  418. @Override
  419. public void mouseReleased(MouseEvent arg0) {
  420. // TODO Auto-generated method stub
  421. } }

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

textual
Листинг программы
  1. public class posl {
  2.     public posl() { super();}
  3.  
  4.     public static int[] posled (int s[][][], int size, int row, int column){ // генерация ПСП
  5.         int psl[]=new int[size];
  6.         for (int k=0;k<size;k++)
  7.             for (int i=0;i<s[k].length;i++)
  8.                 for (int j=0;j<s[k][i].length;j++)
  9.                     if ((i==row)&(j==column))
  10.                         psl[k]=s[k][i][j];
  11.                
  12.     return psl;
  13.     }
  14.  
  15.     public static int hemming (int[]psl){ // вычисление веса Хэмминга
  16.         int k=0;
  17.         for (int i=0;i<psl.length;i++)
  18.             if (psl[i]==1) k++;
  19.             return k;
  20.     }
  21.     }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

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

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

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут