Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 37.88 KB | None | 0 0
  1. package eg.edu.alexu.csd.datastructure.linkedList.cs22_cs70;
  2. /*This lab was done by Pierre Maged (22)
  3. and Mina Ashraf(70)
  4. */
  5. import java.awt.*;
  6. import java.util.Vector;
  7. public interface IPolynomialSolver {
  8. void setPolynomial(char poly, int[][] terms);
  9. String print(char poly);
  10. void clearPolynomial(char poly);
  11. float evaluatePolynomial(char poly, float value);
  12. int[][] add(char poly1, char poly2);
  13. int[][] subtract(char poly1, char poly2);
  14. int[][] multiply(char poly1, char poly2);
  15. }
  16. class solver implements IPolynomialSolver
  17. {
  18. SingleLinkedList A=new SingleLinkedList();
  19. SingleLinkedList B=new SingleLinkedList();
  20. SingleLinkedList C=new SingleLinkedList();
  21. SingleLinkedList R=new SingleLinkedList();
  22.  
  23. @Override
  24. public void setPolynomial(char poly, int[][] terms)
  25. {
  26. int row = terms.length;
  27. if(poly=='A')
  28. A.clear();
  29. else if(poly=='B')
  30. B.clear();
  31. else if(poly=='C')
  32. C.clear();
  33. else
  34. {
  35. throw new RuntimeException ("You entered a wrong letter");
  36. }
  37. Vector<Point> vect=new Vector<Point>();
  38. for(int i=0;i<row;i++){
  39. Point couple=new Point();
  40. couple.x=terms[i][0];
  41. couple.y=terms[i][1];
  42. vect.add(couple);
  43. }
  44. for(int i=0;i<vect.size();i++){
  45. for(int j=i+1;j<vect.size();j++){
  46. Point first=new Point();
  47. Point second=new Point();
  48. first=vect.get(i);second=vect.get(j);
  49. if(first.getY()==second.getY()){
  50. Point fin=new Point((int)first.getX()+(int)second.getX(),(int)first.getY());
  51. vect.set(i,fin);
  52. vect.remove(j);
  53. j--;
  54. }
  55. }
  56. }
  57. for(int i=0;i<vect.size();i++){
  58. if(poly=='A')
  59. A.add(vect.get(i));
  60. else if(poly=='B')
  61. B.add(vect.get(i));
  62. else if(poly=='C')
  63. C.add(vect.get(i));
  64. }
  65. }
  66.  
  67. @Override
  68. public String print(char poly) {
  69. Point couple = new Point();int count=0;
  70. String sol=new String();int n=0;
  71. if(poly=='A'){
  72. n = A.size();}
  73. else if(poly=='B'){
  74. n = B.size();}
  75. else if(poly=='C'){
  76. n = C.size();}
  77. else if(poly=='R'){
  78. n = R.size();}
  79. else
  80. {
  81. throw new RuntimeException ("You entered a wrong letter");
  82. }
  83. if(n==0)
  84. {
  85. String ans=new String();
  86. ans="(0,0)";
  87. return ans;
  88. }
  89. Point[] ans=new Point[n];
  90. if (poly == 'A') {
  91. for(int i=0;i<ans.length;i++){
  92. ans[i]=(Point)A.get(i);
  93. }
  94. }
  95. else if(poly=='B'){
  96. for(int i=0;i<ans.length;i++){
  97. ans[i]=(Point)B.get(i);
  98. }
  99. }
  100. else if(poly=='C'){
  101. for(int i=0;i<ans.length;i++){
  102. ans[i]=(Point)C.get(i);
  103. }
  104. }
  105. else if(poly=='R'){
  106. for(int i=0;i<ans.length;i++){
  107. ans[i]=(Point)R.get(i);
  108. }
  109. }
  110. for(int i=0;i<ans.length;i++){
  111. for(int j=i+1;j<ans.length;j++){
  112. if(ans[i].getY()<ans[j].getY()){
  113. Point test=new Point();
  114. test=ans[i];
  115. ans[i]=ans[j];
  116. ans[j]=test;
  117. }
  118. }
  119. }
  120. for(int i=0;i<ans.length;i++) {
  121. if(ans[i].x==0)
  122. continue;
  123. if(ans.length==1){
  124. if(ans[i].y==0){
  125. sol+=ans[i].x;
  126. }else{
  127. if(ans[i].x==1){
  128. if(ans[i].y==1){
  129. sol+="x";
  130. }else{
  131. sol+="x^";
  132. sol+=ans[i].y;
  133. }
  134. }else{
  135. if (ans[i].x == -1) sol += "-";
  136. else {
  137. sol += ans[i].x;
  138. sol += "x";
  139. }
  140. }
  141. return sol;
  142. }
  143. if(count==0){
  144. if(ans[i].y==0){
  145. sol+=ans[i].x;
  146. }else{
  147. if(ans[i].x==1){
  148. if(ans[i].y==1){
  149. sol+="x";
  150. }else{
  151. sol+="x^";
  152. sol+=ans[i].y;
  153. }
  154. }else {
  155. if (ans[i].y == 1) {
  156. if (ans[i].x == -1) sol += "-";
  157. else {
  158. sol += ans[i].x;
  159. sol += "x";
  160. }
  161. } else {
  162. if (ans[i].x == -1) {
  163. sol += "-";
  164. sol += "x^";
  165. sol += ans[i].y;
  166. } else {
  167. sol += ans[i].x;
  168. sol += "x^";
  169. sol += ans[i].y;
  170. }
  171. }
  172. }
  173. }
  174. }
  175. else{
  176. if (ans[i].y == 0){
  177. if (ans[i].x > 0){
  178. sol+="+";
  179. sol+=ans[i].x;
  180. }
  181. else{
  182. sol+=ans[i].x;}
  183. }
  184. else {
  185. if (ans[i].x > 0) {
  186. sol+="+";
  187. if(ans[i].x!=1)
  188. sol+=ans[i].x;
  189. if(ans[i].y==1)sol+="x";
  190. else {sol+="x^";
  191. sol+=ans[i].y;}
  192. } else {
  193. if(ans[i].x==-1)sol+="-";
  194. else sol+=ans[i].x;
  195. if(ans[i].y==1)sol+="x";
  196. else {sol+="x^";
  197. sol+=ans[i].y;}
  198. }
  199. }
  200. }
  201. count++;
  202.  
  203. }
  204. return sol;
  205. }
  206.  
  207. @Override
  208. public void clearPolynomial(char poly)
  209. {
  210. if(poly=='A')
  211. A.clear();
  212. else if(poly=='B')
  213. B.clear();
  214. else if(poly=='C')
  215. C.clear();
  216. else
  217. {
  218. throw new RuntimeException ("You entered a wrong letter");
  219. }
  220. }
  221.  
  222. @Override
  223. public float evaluatePolynomial(char poly, float value) {
  224. float ans = 0;
  225. Point couple = new Point();
  226. if (poly == 'A') {
  227. if(A.isEmpty())
  228. throw new RuntimeException("A is empty");
  229. int n = A.size();
  230. for(int i=0;i<n;i++){
  231. couple = (Point)A.get(i);
  232. ans += (couple.x) * Math.pow(value, couple.y);
  233. }
  234. }
  235. else if(poly == 'B'){
  236. if(B.isEmpty())
  237. throw new RuntimeException("B is empty");
  238. int n = B.size();
  239. for(int i=0;i<n;i++){
  240. couple =(Point)B.get(i);
  241. ans += (couple.x) * Math.pow(value, couple.y);
  242. }
  243. }
  244. else if(poly == 'C') {
  245. if(C.isEmpty())
  246. throw new RuntimeException("C is empty");
  247. int n = C.size();
  248. for (int i = 0; i < n; i++) {
  249. couple =(Point) C.get(i);
  250. ans += (couple.x) * Math.pow(value, couple.y);
  251. }
  252. }
  253. else if(poly == 'R'){
  254. if(R.isEmpty())
  255. throw new RuntimeException("R is empty");
  256. int n = R.size();
  257. for(int i=0;i<n;i++){
  258. couple =(Point)R.get(i);
  259. ans += (couple.x) * Math.pow(value, couple.y);
  260. }
  261. }
  262. else
  263. {
  264. throw new RuntimeException ("You entered a wrong letter");
  265. }
  266. return ans;
  267. }
  268.  
  269. @Override
  270. public int[][] add(char poly1, char poly2)
  271. {
  272. Vector<Point> ans=new Vector<Point>();
  273. if((poly1=='A'&&poly2=='B'))
  274. {
  275. if(A.isEmpty())
  276. throw new ArithmeticException("A is empty");
  277. if(B.isEmpty())
  278. throw new ArithmeticException("B is empty");
  279. int size1=A.size(),size2=B.size();
  280. boolean[]visited=new boolean[size2];
  281. for(int i=0;i<size1;i++)
  282. {
  283. boolean added=false;
  284. Point first= (Point) A.get(i);
  285. for(int j=0;j<size2;j++)
  286. {
  287. Point second=(Point)B.get(j);
  288. if(first.getY()==second.getY())
  289. {
  290. Point temp=new Point((int)first.getX()+(int)second.getX(),(int)first.getY());
  291. ans.add(temp);
  292. added=true;
  293. visited[j]=true;
  294. }
  295. }
  296. if(!added)
  297. ans.add(first);
  298. }
  299. for(int j=0;j<size2;j++)
  300. {
  301. if(!visited[j])
  302. ans.add(new Point((Point)B.get(j)));
  303. }
  304. }
  305. else if((poly1=='B'&&poly2=='A'))
  306. {
  307. if(B.isEmpty())
  308. throw new ArithmeticException("A is empty");
  309. if(A.isEmpty())
  310. throw new ArithmeticException("B is empty");
  311. int size1=B.size(),size2=A.size();
  312. boolean[]visited=new boolean[size2];
  313. for(int i=0;i<size1;i++)
  314. {
  315. boolean added=false;
  316. Point first= (Point) B.get(i);
  317. for(int j=0;j<size2;j++)
  318. {
  319. Point second=(Point)A.get(j);
  320. if(first.getY()==second.getY())
  321. {
  322. Point temp=new Point((int)first.getX()+(int)second.getX(),(int)first.getY());
  323. ans.add(temp);
  324. added=true;
  325. visited[j]=true;
  326. }
  327. }
  328. if(!added)
  329. ans.add(first);
  330. }
  331. for(int j=0;j<size2;j++)
  332. {
  333. if(!visited[j])
  334. ans.add(new Point((Point)B.get(j)));
  335. }
  336. }
  337. else if(poly1=='C'&&poly2=='A')
  338. {
  339. if(C.isEmpty())
  340. throw new ArithmeticException("A is empty");
  341. if(A.isEmpty())
  342. throw new ArithmeticException("C is empty");
  343. int size1=C.size(),size2=A.size();
  344. boolean[]visited=new boolean[size2];
  345. for(int i=0;i<size1;i++)
  346. {
  347. boolean added=false;
  348. Point first= (Point) C.get(i);
  349. for(int j=0;j<size2;j++)
  350. {
  351. Point second=(Point)A.get(j);
  352. if(first.getY()==second.getY())
  353. {
  354. Point temp=new Point((int)first.getX()+(int)second.getX(),(int)first.getY());
  355. ans.add(temp);
  356. added=true;
  357. visited[j]=true;
  358. }
  359. }
  360. if(!added)
  361. ans.add(first);
  362. }
  363. for(int j=0;j<size2;j++)
  364. {
  365. if(!visited[j])
  366. ans.add(new Point((Point)C.get(j)));
  367. }
  368.  
  369. }
  370. else if(poly1=='A'&&poly2=='C')
  371. {
  372. if(A.isEmpty())
  373. throw new ArithmeticException("A is empty");
  374. if(C.isEmpty())
  375. throw new ArithmeticException("C is empty");
  376. int size1=A.size(),size2=C.size();
  377. boolean[]visited=new boolean[size2];
  378. for(int i=0;i<size1;i++)
  379. {
  380. boolean added=false;
  381. Point first= (Point) A.get(i);
  382. for(int j=0;j<size2;j++)
  383. {
  384. Point second=(Point)C.get(j);
  385. if(first.getY()==second.getY())
  386. {
  387. Point temp=new Point((int)first.getX()+(int)second.getX(),(int)first.getY());
  388. ans.add(temp);
  389. added=true;
  390. visited[j]=true;
  391. }
  392. }
  393. if(!added)
  394. ans.add(first);
  395. }
  396. for(int j=0;j<size2;j++)
  397. {
  398. if(!visited[j])
  399. ans.add(new Point((Point)C.get(j)));
  400. }
  401.  
  402. }
  403. else if(poly1=='B'&&poly2=='C')
  404. {
  405. if(B.isEmpty())
  406. throw new ArithmeticException("B is empty");
  407. if(C.isEmpty())
  408. throw new ArithmeticException("C is empty");
  409. int size1=B.size(),size2=C.size();
  410. boolean[]visited=new boolean[size2];
  411. for(int i=0;i<size1;i++)
  412. {
  413. boolean added=false;
  414. Point first= (Point) B.get(i);
  415. for(int j=0;j<size2;j++)
  416. {
  417. Point second=(Point)C.get(j);
  418. if(first.getY()==second.getY())
  419. {
  420. Point temp=new Point((int)first.getX()+(int)second.getX(),(int)first.getY());
  421. ans.add(temp);
  422. added=true;
  423. visited[j]=true;
  424. }
  425. }
  426. if(!added)
  427. ans.add(first);
  428. }
  429. for(int j=0;j<size2;j++)
  430. {
  431. if(!visited[j])
  432. ans.add(new Point((Point)C.get(j)));
  433. }
  434. }
  435. else if(poly1=='C'&&poly2=='B')
  436. {
  437. if(C.isEmpty())
  438. throw new ArithmeticException("C is empty");
  439. if(B.isEmpty())
  440. throw new ArithmeticException("B is empty");
  441. int size1=C.size(),size2=B.size();
  442. boolean[]visited=new boolean[size2];
  443. for(int i=0;i<size1;i++)
  444. {
  445. boolean added=false;
  446. Point first= (Point) C.get(i);
  447. for(int j=0;j<size2;j++)
  448. {
  449. Point second=(Point)B.get(j);
  450. if(first.getY()==second.getY())
  451. {
  452. Point temp=new Point((int)first.getX()+(int)second.getX(),(int)first.getY());
  453. ans.add(temp);
  454. added=true;
  455. visited[j]=true;
  456. }
  457. }
  458. if(!added)
  459. ans.add(first);
  460. }
  461. for(int j=0;j<size2;j++)
  462. {
  463. if(!visited[j])
  464. ans.add(new Point((Point)C.get(j)));
  465. }
  466. }
  467. else if(poly1=='C'&&poly2=='C')
  468. {
  469. if(C.isEmpty())
  470. throw new ArithmeticException("C is empty");
  471. int size1=C.size(),size2=C.size();
  472. boolean[]visited=new boolean[size2];
  473. for(int i=0;i<size1;i++)
  474. {
  475. boolean added=false;
  476. Point first= (Point) C.get(i);
  477. for(int j=0;j<size2;j++)
  478. {
  479. Point second=(Point)C.get(j);
  480. if(first.getY()==second.getY())
  481. {
  482. Point temp=new Point((int)first.getX()+(int)second.getX(),(int)first.getY());
  483. ans.add(temp);
  484. added=true;
  485. visited[j]=true;
  486. }
  487. }
  488. if(!added)
  489. ans.add(first);
  490. }
  491. for(int j=0;j<size2;j++)
  492. {
  493. if(!visited[j])
  494. ans.add(new Point((Point)C.get(j)));
  495. }
  496. }
  497. else if(poly1=='B'&&poly2=='B')
  498. {
  499. if(B.isEmpty())
  500. throw new ArithmeticException("B is empty");
  501. int size1=B.size(),size2=B.size();
  502. boolean[]visited=new boolean[size2];
  503. for(int i=0;i<size1;i++)
  504. {
  505. boolean added=false;
  506. Point first= (Point) B.get(i);
  507. for(int j=0;j<size2;j++)
  508. {
  509. Point second=(Point)B.get(j);
  510. if(first.getY()==second.getY())
  511. {
  512. Point temp=new Point((int)first.getX()+(int)second.getX(),(int)first.getY());
  513. ans.add(temp);
  514. added=true;
  515. visited[j]=true;
  516. }
  517. }
  518. if(!added)
  519. ans.add(first);
  520. }
  521. for(int j=0;j<size2;j++)
  522. {
  523. if(!visited[j])
  524. ans.add(new Point((Point)B.get(j)));
  525. }
  526. }
  527. else if(poly1=='A'&&poly2=='A')
  528. {
  529. if(A.isEmpty())
  530. throw new ArithmeticException("A is empty");
  531. int size1=A.size(),size2=A.size();
  532. boolean[]visited=new boolean[size2];
  533. for(int i=0;i<size1;i++)
  534. {
  535. boolean added=false;
  536. Point first= (Point) A.get(i);
  537. for(int j=0;j<size2;j++)
  538. {
  539. Point second=(Point)A.get(j);
  540. if(first.getY()==second.getY())
  541. {
  542. Point temp=new Point((int)first.getX()+(int)second.getX(),(int)first.getY());
  543. ans.add(temp);
  544. added=true;
  545. visited[j]=true;
  546. }
  547. }
  548. if(!added)
  549. ans.add(first);
  550. }
  551. for(int j=0;j<size2;j++)
  552. {
  553. if(!visited[j])
  554. ans.add(new Point((Point)A.get(j)));
  555. }
  556. }
  557. else
  558. {
  559. throw new RuntimeException ("You entered a wrong letter");
  560. }
  561. int[][] result=new int[ans.size()][2];
  562. for(int i=0;i<ans.size();i++)
  563. {
  564. for(int j=i+1;j<ans.size();j++)
  565. {
  566. if(ans.elementAt(i).getY()>ans.elementAt(j).getY())
  567. {
  568. Point temp=ans.elementAt(i);
  569. ans.set(i,ans.elementAt(j));
  570. ans.set(j,temp);
  571. }
  572. }
  573. }
  574. for(int i=0;i<ans.size();i++)
  575. {
  576. for(int j=i+1;j<ans.size();j++)
  577. {
  578. if(ans.elementAt(i).getY()==ans.elementAt(j).getY())
  579. {
  580. Point temp1=ans.elementAt(i),temp2=ans.elementAt(j);;
  581. Point fin=new Point((int)temp1.getX()+(int)temp2.getX(),(int)temp1.getY());
  582. ans.set(i,fin);
  583. ans.remove(j);
  584. j--;
  585. }
  586. }
  587. }
  588. R.clear();
  589. for(int i=0;i<ans.size();i++)
  590. R.add(ans.elementAt(i));
  591. for(int i=0;i<ans.size();i++)
  592. {
  593. result[i][0]=(int)ans.elementAt(i).getX();
  594. result[i][1]=(int)ans.elementAt(i).getY();
  595. }
  596. return result;
  597. }
  598.  
  599. @Override
  600. public int[][] subtract(char poly1, char poly2) {
  601. Vector<Point> ans=new Vector<Point>();
  602. if((poly1=='A'&&poly2=='B'))
  603. {
  604. if(A.isEmpty())
  605. throw new ArithmeticException("A is empty");
  606. if(B.isEmpty())
  607. throw new ArithmeticException("B is empty");
  608. int size1=A.size(),size2=B.size();
  609. boolean[]visited=new boolean[size2];
  610. for(int i=0;i<size1;i++)
  611. {
  612. boolean added=false;
  613. Point first= (Point) A.get(i);
  614. for(int j=0;j<size2;j++)
  615. {
  616. Point second=(Point)B.get(j);
  617. if(first.getY()==second.getY())
  618. {
  619. Point temp=new Point((int)first.getX()-(int)second.getX(),(int)first.getY());
  620. ans.add(temp);
  621. added=true;
  622. visited[j]=true;
  623. }
  624. }
  625. if(!added)
  626. ans.add(first);
  627. }
  628. for(int j=0;j<size2;j++)
  629. {
  630. if(!visited[j])
  631. {
  632. Point temp=new Point((Point)B.get(j));
  633. temp.setLocation(-temp.getX(),temp.getY());
  634. ans.add(temp);
  635. }
  636. }
  637. }
  638. else if((poly1=='B'&&poly2=='A'))
  639. {
  640. if(B.isEmpty())
  641. throw new ArithmeticException("A is empty");
  642. if(A.isEmpty())
  643. throw new ArithmeticException("B is empty");
  644. int size1=B.size(),size2=A.size();
  645. boolean[]visited=new boolean[size2];
  646. for(int i=0;i<size1;i++)
  647. {
  648. boolean added=false;
  649. Point first= (Point) B.get(i);
  650. for(int j=0;j<size2;j++)
  651. {
  652. Point second=(Point)A.get(j);
  653. if(first.getY()==second.getY())
  654. {
  655. Point temp=new Point((int)first.getX()-(int)second.getX(),(int)first.getY());
  656. ans.add(temp);
  657. added=true;
  658. visited[j]=true;
  659. }
  660. }
  661. if(!added)
  662. ans.add(first);
  663. }
  664. for(int j=0;j<size2;j++)
  665. {
  666. if(!visited[j])
  667. {
  668. Point temp=new Point((Point)B.get(j));
  669. temp.setLocation(-temp.getX(),temp.getY());
  670. ans.add(temp);
  671. }
  672. }
  673. }
  674. else if(poly1=='C'&&poly2=='A')
  675. {
  676. if(C.isEmpty())
  677. throw new ArithmeticException("A is empty");
  678. if(A.isEmpty())
  679. throw new ArithmeticException("C is empty");
  680. int size1=C.size(),size2=A.size();
  681. boolean[]visited=new boolean[size2];
  682. for(int i=0;i<size1;i++)
  683. {
  684. boolean added=false;
  685. Point first= (Point) C.get(i);
  686. for(int j=0;j<size2;j++)
  687. {
  688. Point second=(Point)A.get(j);
  689. if(first.getY()==second.getY())
  690. {
  691. Point temp=new Point((int)first.getX()-(int)second.getX(),(int)first.getY());
  692. ans.add(temp);
  693. added=true;
  694. visited[j]=true;
  695. }
  696. }
  697. if(!added)
  698. ans.add(first);
  699. }
  700. for(int j=0;j<size2;j++)
  701. {
  702. if(!visited[j])
  703. {
  704. Point temp=new Point((Point)B.get(j));
  705. temp.setLocation(-temp.getX(),temp.getY());
  706. ans.add(temp);
  707. }
  708. }
  709.  
  710. }
  711. else if(poly1=='A'&&poly2=='C')
  712. {
  713. if(A.isEmpty())
  714. throw new ArithmeticException("A is empty");
  715. if(C.isEmpty())
  716. throw new ArithmeticException("C is empty");
  717. int size1=A.size(),size2=C.size();
  718. boolean[]visited=new boolean[size2];
  719. for(int i=0;i<size1;i++)
  720. {
  721. boolean added=false;
  722. Point first= (Point) A.get(i);
  723. for(int j=0;j<size2;j++)
  724. {
  725. Point second=(Point)C.get(j);
  726. if(first.getY()==second.getY())
  727. {
  728. Point temp=new Point((int)first.getX()-(int)second.getX(),(int)first.getY());
  729. ans.add(temp);
  730. added=true;
  731. visited[j]=true;
  732. }
  733. }
  734. if(!added)
  735. ans.add(first);
  736. }
  737. for(int j=0;j<size2;j++)
  738. {
  739. if(!visited[j])
  740. {
  741. Point temp=new Point((Point)B.get(j));
  742. temp.setLocation(-temp.getX(),temp.getY());
  743. ans.add(temp);
  744. }
  745. }
  746.  
  747. }
  748. else if(poly1=='B'&&poly2=='C')
  749. {
  750. if(B.isEmpty())
  751. throw new ArithmeticException("B is empty");
  752. if(C.isEmpty())
  753. throw new ArithmeticException("C is empty");
  754. int size1=B.size(),size2=C.size();
  755. boolean[]visited=new boolean[size2];
  756. for(int i=0;i<size1;i++)
  757. {
  758. boolean added=false;
  759. Point first= (Point) B.get(i);
  760. for(int j=0;j<size2;j++)
  761. {
  762. Point second=(Point)C.get(j);
  763. if(first.getY()==second.getY())
  764. {
  765. Point temp=new Point((int)first.getX()-(int)second.getX(),(int)first.getY());
  766. ans.add(temp);
  767. added=true;
  768. visited[j]=true;
  769. }
  770. }
  771. if(!added)
  772. ans.add(first);
  773. }
  774. for(int j=0;j<size2;j++)
  775. {
  776. if(!visited[j])
  777. {
  778. Point temp=new Point((Point)B.get(j));
  779. temp.setLocation(-temp.getX(),temp.getY());
  780. ans.add(temp);
  781. }
  782. }
  783. }
  784. else if(poly1=='C'&&poly2=='B')
  785. {
  786. if(C.isEmpty())
  787. throw new ArithmeticException("C is empty");
  788. if(B.isEmpty())
  789. throw new ArithmeticException("B is empty");
  790. int size1=C.size(),size2=B.size();
  791. boolean[]visited=new boolean[size2];
  792. for(int i=0;i<size1;i++)
  793. {
  794. boolean added=false;
  795. Point first= (Point) C.get(i);
  796. for(int j=0;j<size2;j++)
  797. {
  798. Point second=(Point)B.get(j);
  799. if(first.getY()==second.getY())
  800. {
  801. Point temp=new Point((int)first.getX()-(int)second.getX(),(int)first.getY());
  802. ans.add(temp);
  803. added=true;
  804. visited[j]=true;
  805. }
  806. }
  807. if(!added)
  808. ans.add(first);
  809. }
  810. for(int j=0;j<size2;j++)
  811. {
  812. if(!visited[j])
  813. {
  814. Point temp=new Point((Point)B.get(j));
  815. temp.setLocation(-temp.getX(),temp.getY());
  816. ans.add(temp);
  817. }
  818. }
  819. }
  820. if((poly1=='A'&&poly2=='A'))
  821. {
  822. if(A.isEmpty())
  823. throw new ArithmeticException("A is empty");
  824. int size1=A.size(),size2=A.size();
  825. boolean[]visited=new boolean[size2];
  826. for(int i=0;i<size1;i++)
  827. {
  828. boolean added=false;
  829. Point first= (Point) A.get(i);
  830. for(int j=0;j<size2;j++)
  831. {
  832. Point second=(Point)A.get(j);
  833. if(first.getY()==second.getY())
  834. {
  835. Point temp=new Point((int)first.getX()-(int)second.getX(),(int)first.getY());
  836. ans.add(temp);
  837. added=true;
  838. visited[j]=true;
  839. }
  840. }
  841. if(!added)
  842. ans.add(first);
  843. }
  844. for(int j=0;j<size2;j++)
  845. {
  846. if(!visited[j])
  847. {
  848. Point temp=new Point((Point)A.get(j));
  849. temp.setLocation(-temp.getX(),temp.getY());
  850. ans.add(temp);
  851. }
  852. }
  853. }
  854. if((poly1=='B'&&poly2=='B'))
  855. {
  856. if(B.isEmpty())
  857. throw new ArithmeticException("B is empty");
  858. int size1=B.size(),size2=B.size();
  859. boolean[]visited=new boolean[size2];
  860. for(int i=0;i<size1;i++)
  861. {
  862. boolean added=false;
  863. Point first= (Point) B.get(i);
  864. for(int j=0;j<size2;j++)
  865. {
  866. Point second=(Point)B.get(j);
  867. if(first.getY()==second.getY())
  868. {
  869. Point temp=new Point((int)first.getX()-(int)second.getX(),(int)first.getY());
  870. ans.add(temp);
  871. added=true;
  872. visited[j]=true;
  873. }
  874. }
  875. if(!added)
  876. ans.add(first);
  877. }
  878. for(int j=0;j<size2;j++)
  879. {
  880. if(!visited[j])
  881. {
  882. Point temp=new Point((Point)B.get(j));
  883. temp.setLocation(-temp.getX(),temp.getY());
  884. ans.add(temp);
  885. }
  886. }
  887. }
  888. if((poly1=='C'&&poly2=='C'))
  889. {
  890. if(C.isEmpty())
  891. throw new ArithmeticException("C is empty");
  892. int size1=A.size(),size2=B.size();
  893. boolean[]visited=new boolean[size2];
  894. for(int i=0;i<size1;i++)
  895. {
  896. boolean added=false;
  897. Point first= (Point) C.get(i);
  898. for(int j=0;j<size2;j++)
  899. {
  900. Point second=(Point)C.get(j);
  901. if(first.getY()==second.getY())
  902. {
  903. Point temp=new Point((int)first.getX()-(int)second.getX(),(int)first.getY());
  904. ans.add(temp);
  905. added=true;
  906. visited[j]=true;
  907. }
  908. }
  909. if(!added)
  910. ans.add(first);
  911. }
  912. for(int j=0;j<size2;j++)
  913. {
  914. if(!visited[j])
  915. {
  916. Point temp=new Point((Point)C.get(j));
  917. temp.setLocation(-temp.getX(),temp.getY());
  918. ans.add(temp);
  919. }
  920. }
  921. }
  922. else
  923. {
  924. throw new RuntimeException ("You entered a wrong letter");
  925. }
  926. int[][] result=new int[ans.size()][2];
  927. for(int i=0;i<ans.size();i++)
  928. {
  929. for(int j=i+1;j<ans.size();j++)
  930. {
  931. if(ans.elementAt(i).getY()>ans.elementAt(j).getY())
  932. {
  933. Point temp=ans.elementAt(i);
  934. ans.set(i,ans.elementAt(j));
  935. ans.set(j,temp);
  936. }
  937. }
  938. }
  939. for(int i=0;i<ans.size();i++)
  940. {
  941. for(int j=i+1;j<ans.size();j++)
  942. {
  943. if(ans.elementAt(i).getY()==ans.elementAt(j).getY())
  944. {
  945. Point temp1=ans.elementAt(i),temp2=ans.elementAt(j);;
  946. Point fin=new Point((int)temp1.getX()+(int)temp2.getX(),(int)temp1.getY());
  947. ans.set(i,fin);
  948. ans.remove(j);
  949. j--;
  950. }
  951. }
  952. }
  953. R.clear();
  954. for(int i=0;i<ans.size();i++)
  955. R.add(ans.elementAt(i));
  956. for(int i=0;i<ans.size();i++)
  957. {
  958. result[i][0]=(int)ans.elementAt(i).getX();
  959. result[i][1]=(int)ans.elementAt(i).getY();
  960. }
  961. return result;
  962. }
  963.  
  964. @Override
  965. public int[][] multiply(char poly1, char poly2)
  966. {
  967. Vector<Point> ans=new Vector<Point>();
  968. if((poly1=='A'&&poly2=='B')||(poly1=='B'&&poly2=='A'))
  969. {
  970. if(A.isEmpty())
  971. throw new ArithmeticException("A is empty");
  972. if(B.isEmpty())
  973. throw new ArithmeticException("B is empty");
  974. int size1=A.size(),size2=B.size();
  975. for(int i=0;i<size1;i++)
  976. {
  977. Point first= (Point) A.get(i);
  978. for(int j=0;j<size2;j++)
  979. {
  980. Point second=(Point)B.get(j);
  981. Point temp=new Point((int)first.getX()*(int)second.getX(),(int)first.getY()+(int)second.getY());
  982. ans.add(temp);
  983.  
  984. }
  985. }
  986. }
  987. else if((poly1=='A'&&poly2=='C')||(poly1=='C'&&poly2=='A'))
  988. {
  989. if(A.isEmpty())
  990. throw new ArithmeticException("A is empty");
  991. if(C.isEmpty())
  992. throw new ArithmeticException("C is empty");
  993. int size1=A.size(),size2=C.size();
  994. for(int i=0;i<size1;i++)
  995. {
  996. Point first= (Point) A.get(i);
  997. for(int j=0;j<size2;j++)
  998. {
  999. Point second=(Point)C.get(j);
  1000. Point temp=new Point((int)first.getX()*(int)second.getX(),(int)first.getY()+(int)second.getY());
  1001. ans.add(temp);
  1002.  
  1003. }
  1004. }
  1005.  
  1006. }
  1007. else if((poly1=='C'&&poly2=='B')||(poly1=='B'&&poly2=='C'))
  1008. {
  1009. if(C.isEmpty())
  1010. throw new ArithmeticException("C is empty");
  1011. if(B.isEmpty())
  1012. throw new ArithmeticException("B is empty");
  1013. int size1=C.size(),size2=B.size();
  1014. for(int i=0;i<size1;i++)
  1015. {
  1016. Point first= (Point) C.get(i);
  1017. for(int j=0;j<size2;j++)
  1018. {
  1019. Point second=(Point)B.get(j);
  1020. Point temp=new Point((int)first.getX()*(int)second.getX(),(int)first.getY()+(int)second.getY());
  1021. ans.add(temp);
  1022.  
  1023. }
  1024. }
  1025. }
  1026. else if(poly1=='C'&&poly2=='C')
  1027. {
  1028. if(C.isEmpty())
  1029. throw new ArithmeticException("C is empty");
  1030. int size1=C.size(),size2=C.size();
  1031. for(int i=0;i<size1;i++)
  1032. {
  1033. Point first= (Point) C.get(i);
  1034. for(int j=0;j<size2;j++)
  1035. {
  1036. Point second=(Point)C.get(j);
  1037. Point temp=new Point((int)first.getX()*(int)second.getX(),(int)first.getY()+(int)second.getY());
  1038. ans.add(temp);
  1039.  
  1040. }
  1041. }
  1042. }
  1043. else if(poly1=='B'&&poly2=='B')
  1044. {
  1045. if(B.isEmpty())
  1046. throw new ArithmeticException("B is empty");
  1047. int size1=B.size(),size2=B.size();
  1048. for(int i=0;i<size1;i++)
  1049. {
  1050. Point first= (Point) B.get(i);
  1051. for(int j=0;j<size2;j++)
  1052. {
  1053. Point second=(Point)B.get(j);
  1054. Point temp=new Point((int)first.getX()*(int)second.getX(),(int)first.getY()+(int)second.getY());
  1055. ans.add(temp);
  1056.  
  1057. }
  1058. }
  1059. }
  1060. else if(poly1=='A'&&poly2=='A')
  1061. {
  1062. if(A.isEmpty())
  1063. throw new ArithmeticException("A is empty");
  1064. int size1=A.size(),size2=A.size();
  1065. for(int i=0;i<size1;i++)
  1066. {
  1067. Point first= (Point) A.get(i);
  1068. for(int j=0;j<size2;j++)
  1069. {
  1070. Point second=(Point)A.get(j);
  1071. Point temp=new Point((int)first.getX()*(int)second.getX(),(int)first.getY()+(int)second.getY());
  1072. ans.add(temp);
  1073.  
  1074. }
  1075. }
  1076. }
  1077. else
  1078. {
  1079. throw new RuntimeException ("You entered a wrong letter");
  1080. }
  1081. int[][] result=new int[ans.size()][2];
  1082. for(int i=0;i<ans.size();i++)
  1083. {
  1084. for(int j=i+1;j<ans.size();j++)
  1085. {
  1086. if(ans.elementAt(i).getY()>ans.elementAt(j).getY())
  1087. {
  1088. Point temp=ans.elementAt(i);
  1089. ans.set(i,ans.elementAt(j));
  1090. ans.set(j,temp);
  1091. }
  1092. }
  1093. }
  1094. for(int i=0;i<ans.size();i++)
  1095. {
  1096. for(int j=i+1;j<ans.size();j++)
  1097. {
  1098. if(ans.elementAt(i).getY()==ans.elementAt(j).getY())
  1099. {
  1100. Point temp1=ans.elementAt(i),temp2=ans.elementAt(j);;
  1101. Point fin=new Point((int)temp1.getX()+(int)temp2.getX(),(int)temp1.getY());
  1102. ans.set(i,fin);
  1103. ans.remove(j);
  1104. j--;
  1105. }
  1106. }
  1107. }
  1108. R.clear();
  1109. for(int i=0;i<ans.size();i++)
  1110. R.add(ans.elementAt(i));
  1111. for(int i=0;i<ans.size();i++)
  1112. {
  1113. result[i][0]=(int)ans.elementAt(i).getX();
  1114. result[i][1]=(int)ans.elementAt(i).getY();
  1115. }
  1116.  
  1117. return result;
  1118. }
  1119.  
  1120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement