Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package eg.edu.alexu.csd.datastructure.linkedList.cs22_cs70;
- /*This lab was done by Pierre Maged (22)
- and Mina Ashraf(70)
- */
- import java.awt.*;
- import java.util.Vector;
- public interface IPolynomialSolver {
- void setPolynomial(char poly, int[][] terms);
- String print(char poly);
- void clearPolynomial(char poly);
- float evaluatePolynomial(char poly, float value);
- int[][] add(char poly1, char poly2);
- int[][] subtract(char poly1, char poly2);
- int[][] multiply(char poly1, char poly2);
- }
- class solver implements IPolynomialSolver
- {
- SingleLinkedList A=new SingleLinkedList();
- SingleLinkedList B=new SingleLinkedList();
- SingleLinkedList C=new SingleLinkedList();
- SingleLinkedList R=new SingleLinkedList();
- @Override
- public void setPolynomial(char poly, int[][] terms)
- {
- int row = terms.length;
- if(poly=='A')
- A.clear();
- else if(poly=='B')
- B.clear();
- else if(poly=='C')
- C.clear();
- else
- {
- throw new RuntimeException ("You entered a wrong letter");
- }
- Vector<Point> vect=new Vector<Point>();
- for(int i=0;i<row;i++){
- Point couple=new Point();
- couple.x=terms[i][0];
- couple.y=terms[i][1];
- vect.add(couple);
- }
- for(int i=0;i<vect.size();i++){
- for(int j=i+1;j<vect.size();j++){
- Point first=new Point();
- Point second=new Point();
- first=vect.get(i);second=vect.get(j);
- if(first.getY()==second.getY()){
- Point fin=new Point((int)first.getX()+(int)second.getX(),(int)first.getY());
- vect.set(i,fin);
- vect.remove(j);
- j--;
- }
- }
- }
- for(int i=0;i<vect.size();i++){
- if(poly=='A')
- A.add(vect.get(i));
- else if(poly=='B')
- B.add(vect.get(i));
- else if(poly=='C')
- C.add(vect.get(i));
- }
- }
- @Override
- public String print(char poly) {
- Point couple = new Point();int count=0;
- String sol=new String();int n=0;
- if(poly=='A'){
- n = A.size();}
- else if(poly=='B'){
- n = B.size();}
- else if(poly=='C'){
- n = C.size();}
- else if(poly=='R'){
- n = R.size();}
- else
- {
- throw new RuntimeException ("You entered a wrong letter");
- }
- if(n==0)
- {
- String ans=new String();
- ans="(0,0)";
- return ans;
- }
- Point[] ans=new Point[n];
- if (poly == 'A') {
- for(int i=0;i<ans.length;i++){
- ans[i]=(Point)A.get(i);
- }
- }
- else if(poly=='B'){
- for(int i=0;i<ans.length;i++){
- ans[i]=(Point)B.get(i);
- }
- }
- else if(poly=='C'){
- for(int i=0;i<ans.length;i++){
- ans[i]=(Point)C.get(i);
- }
- }
- else if(poly=='R'){
- for(int i=0;i<ans.length;i++){
- ans[i]=(Point)R.get(i);
- }
- }
- for(int i=0;i<ans.length;i++){
- for(int j=i+1;j<ans.length;j++){
- if(ans[i].getY()<ans[j].getY()){
- Point test=new Point();
- test=ans[i];
- ans[i]=ans[j];
- ans[j]=test;
- }
- }
- }
- for(int i=0;i<ans.length;i++) {
- if(ans[i].x==0)
- continue;
- if(ans.length==1){
- if(ans[i].y==0){
- sol+=ans[i].x;
- }else{
- if(ans[i].x==1){
- if(ans[i].y==1){
- sol+="x";
- }else{
- sol+="x^";
- sol+=ans[i].y;
- }
- }else{
- if (ans[i].x == -1) sol += "-";
- else {
- sol += ans[i].x;
- sol += "x";
- }
- }
- return sol;
- }
- if(count==0){
- if(ans[i].y==0){
- sol+=ans[i].x;
- }else{
- if(ans[i].x==1){
- if(ans[i].y==1){
- sol+="x";
- }else{
- sol+="x^";
- sol+=ans[i].y;
- }
- }else {
- if (ans[i].y == 1) {
- if (ans[i].x == -1) sol += "-";
- else {
- sol += ans[i].x;
- sol += "x";
- }
- } else {
- if (ans[i].x == -1) {
- sol += "-";
- sol += "x^";
- sol += ans[i].y;
- } else {
- sol += ans[i].x;
- sol += "x^";
- sol += ans[i].y;
- }
- }
- }
- }
- }
- else{
- if (ans[i].y == 0){
- if (ans[i].x > 0){
- sol+="+";
- sol+=ans[i].x;
- }
- else{
- sol+=ans[i].x;}
- }
- else {
- if (ans[i].x > 0) {
- sol+="+";
- if(ans[i].x!=1)
- sol+=ans[i].x;
- if(ans[i].y==1)sol+="x";
- else {sol+="x^";
- sol+=ans[i].y;}
- } else {
- if(ans[i].x==-1)sol+="-";
- else sol+=ans[i].x;
- if(ans[i].y==1)sol+="x";
- else {sol+="x^";
- sol+=ans[i].y;}
- }
- }
- }
- count++;
- }
- return sol;
- }
- @Override
- public void clearPolynomial(char poly)
- {
- if(poly=='A')
- A.clear();
- else if(poly=='B')
- B.clear();
- else if(poly=='C')
- C.clear();
- else
- {
- throw new RuntimeException ("You entered a wrong letter");
- }
- }
- @Override
- public float evaluatePolynomial(char poly, float value) {
- float ans = 0;
- Point couple = new Point();
- if (poly == 'A') {
- if(A.isEmpty())
- throw new RuntimeException("A is empty");
- int n = A.size();
- for(int i=0;i<n;i++){
- couple = (Point)A.get(i);
- ans += (couple.x) * Math.pow(value, couple.y);
- }
- }
- else if(poly == 'B'){
- if(B.isEmpty())
- throw new RuntimeException("B is empty");
- int n = B.size();
- for(int i=0;i<n;i++){
- couple =(Point)B.get(i);
- ans += (couple.x) * Math.pow(value, couple.y);
- }
- }
- else if(poly == 'C') {
- if(C.isEmpty())
- throw new RuntimeException("C is empty");
- int n = C.size();
- for (int i = 0; i < n; i++) {
- couple =(Point) C.get(i);
- ans += (couple.x) * Math.pow(value, couple.y);
- }
- }
- else if(poly == 'R'){
- if(R.isEmpty())
- throw new RuntimeException("R is empty");
- int n = R.size();
- for(int i=0;i<n;i++){
- couple =(Point)R.get(i);
- ans += (couple.x) * Math.pow(value, couple.y);
- }
- }
- else
- {
- throw new RuntimeException ("You entered a wrong letter");
- }
- return ans;
- }
- @Override
- public int[][] add(char poly1, char poly2)
- {
- Vector<Point> ans=new Vector<Point>();
- if((poly1=='A'&&poly2=='B'))
- {
- if(A.isEmpty())
- throw new ArithmeticException("A is empty");
- if(B.isEmpty())
- throw new ArithmeticException("B is empty");
- int size1=A.size(),size2=B.size();
- boolean[]visited=new boolean[size2];
- for(int i=0;i<size1;i++)
- {
- boolean added=false;
- Point first= (Point) A.get(i);
- for(int j=0;j<size2;j++)
- {
- Point second=(Point)B.get(j);
- if(first.getY()==second.getY())
- {
- Point temp=new Point((int)first.getX()+(int)second.getX(),(int)first.getY());
- ans.add(temp);
- added=true;
- visited[j]=true;
- }
- }
- if(!added)
- ans.add(first);
- }
- for(int j=0;j<size2;j++)
- {
- if(!visited[j])
- ans.add(new Point((Point)B.get(j)));
- }
- }
- else if((poly1=='B'&&poly2=='A'))
- {
- if(B.isEmpty())
- throw new ArithmeticException("A is empty");
- if(A.isEmpty())
- throw new ArithmeticException("B is empty");
- int size1=B.size(),size2=A.size();
- boolean[]visited=new boolean[size2];
- for(int i=0;i<size1;i++)
- {
- boolean added=false;
- Point first= (Point) B.get(i);
- for(int j=0;j<size2;j++)
- {
- Point second=(Point)A.get(j);
- if(first.getY()==second.getY())
- {
- Point temp=new Point((int)first.getX()+(int)second.getX(),(int)first.getY());
- ans.add(temp);
- added=true;
- visited[j]=true;
- }
- }
- if(!added)
- ans.add(first);
- }
- for(int j=0;j<size2;j++)
- {
- if(!visited[j])
- ans.add(new Point((Point)B.get(j)));
- }
- }
- else if(poly1=='C'&&poly2=='A')
- {
- if(C.isEmpty())
- throw new ArithmeticException("A is empty");
- if(A.isEmpty())
- throw new ArithmeticException("C is empty");
- int size1=C.size(),size2=A.size();
- boolean[]visited=new boolean[size2];
- for(int i=0;i<size1;i++)
- {
- boolean added=false;
- Point first= (Point) C.get(i);
- for(int j=0;j<size2;j++)
- {
- Point second=(Point)A.get(j);
- if(first.getY()==second.getY())
- {
- Point temp=new Point((int)first.getX()+(int)second.getX(),(int)first.getY());
- ans.add(temp);
- added=true;
- visited[j]=true;
- }
- }
- if(!added)
- ans.add(first);
- }
- for(int j=0;j<size2;j++)
- {
- if(!visited[j])
- ans.add(new Point((Point)C.get(j)));
- }
- }
- else if(poly1=='A'&&poly2=='C')
- {
- if(A.isEmpty())
- throw new ArithmeticException("A is empty");
- if(C.isEmpty())
- throw new ArithmeticException("C is empty");
- int size1=A.size(),size2=C.size();
- boolean[]visited=new boolean[size2];
- for(int i=0;i<size1;i++)
- {
- boolean added=false;
- Point first= (Point) A.get(i);
- for(int j=0;j<size2;j++)
- {
- Point second=(Point)C.get(j);
- if(first.getY()==second.getY())
- {
- Point temp=new Point((int)first.getX()+(int)second.getX(),(int)first.getY());
- ans.add(temp);
- added=true;
- visited[j]=true;
- }
- }
- if(!added)
- ans.add(first);
- }
- for(int j=0;j<size2;j++)
- {
- if(!visited[j])
- ans.add(new Point((Point)C.get(j)));
- }
- }
- else if(poly1=='B'&&poly2=='C')
- {
- if(B.isEmpty())
- throw new ArithmeticException("B is empty");
- if(C.isEmpty())
- throw new ArithmeticException("C is empty");
- int size1=B.size(),size2=C.size();
- boolean[]visited=new boolean[size2];
- for(int i=0;i<size1;i++)
- {
- boolean added=false;
- Point first= (Point) B.get(i);
- for(int j=0;j<size2;j++)
- {
- Point second=(Point)C.get(j);
- if(first.getY()==second.getY())
- {
- Point temp=new Point((int)first.getX()+(int)second.getX(),(int)first.getY());
- ans.add(temp);
- added=true;
- visited[j]=true;
- }
- }
- if(!added)
- ans.add(first);
- }
- for(int j=0;j<size2;j++)
- {
- if(!visited[j])
- ans.add(new Point((Point)C.get(j)));
- }
- }
- else if(poly1=='C'&&poly2=='B')
- {
- if(C.isEmpty())
- throw new ArithmeticException("C is empty");
- if(B.isEmpty())
- throw new ArithmeticException("B is empty");
- int size1=C.size(),size2=B.size();
- boolean[]visited=new boolean[size2];
- for(int i=0;i<size1;i++)
- {
- boolean added=false;
- Point first= (Point) C.get(i);
- for(int j=0;j<size2;j++)
- {
- Point second=(Point)B.get(j);
- if(first.getY()==second.getY())
- {
- Point temp=new Point((int)first.getX()+(int)second.getX(),(int)first.getY());
- ans.add(temp);
- added=true;
- visited[j]=true;
- }
- }
- if(!added)
- ans.add(first);
- }
- for(int j=0;j<size2;j++)
- {
- if(!visited[j])
- ans.add(new Point((Point)C.get(j)));
- }
- }
- else if(poly1=='C'&&poly2=='C')
- {
- if(C.isEmpty())
- throw new ArithmeticException("C is empty");
- int size1=C.size(),size2=C.size();
- boolean[]visited=new boolean[size2];
- for(int i=0;i<size1;i++)
- {
- boolean added=false;
- Point first= (Point) C.get(i);
- for(int j=0;j<size2;j++)
- {
- Point second=(Point)C.get(j);
- if(first.getY()==second.getY())
- {
- Point temp=new Point((int)first.getX()+(int)second.getX(),(int)first.getY());
- ans.add(temp);
- added=true;
- visited[j]=true;
- }
- }
- if(!added)
- ans.add(first);
- }
- for(int j=0;j<size2;j++)
- {
- if(!visited[j])
- ans.add(new Point((Point)C.get(j)));
- }
- }
- else if(poly1=='B'&&poly2=='B')
- {
- if(B.isEmpty())
- throw new ArithmeticException("B is empty");
- int size1=B.size(),size2=B.size();
- boolean[]visited=new boolean[size2];
- for(int i=0;i<size1;i++)
- {
- boolean added=false;
- Point first= (Point) B.get(i);
- for(int j=0;j<size2;j++)
- {
- Point second=(Point)B.get(j);
- if(first.getY()==second.getY())
- {
- Point temp=new Point((int)first.getX()+(int)second.getX(),(int)first.getY());
- ans.add(temp);
- added=true;
- visited[j]=true;
- }
- }
- if(!added)
- ans.add(first);
- }
- for(int j=0;j<size2;j++)
- {
- if(!visited[j])
- ans.add(new Point((Point)B.get(j)));
- }
- }
- else if(poly1=='A'&&poly2=='A')
- {
- if(A.isEmpty())
- throw new ArithmeticException("A is empty");
- int size1=A.size(),size2=A.size();
- boolean[]visited=new boolean[size2];
- for(int i=0;i<size1;i++)
- {
- boolean added=false;
- Point first= (Point) A.get(i);
- for(int j=0;j<size2;j++)
- {
- Point second=(Point)A.get(j);
- if(first.getY()==second.getY())
- {
- Point temp=new Point((int)first.getX()+(int)second.getX(),(int)first.getY());
- ans.add(temp);
- added=true;
- visited[j]=true;
- }
- }
- if(!added)
- ans.add(first);
- }
- for(int j=0;j<size2;j++)
- {
- if(!visited[j])
- ans.add(new Point((Point)A.get(j)));
- }
- }
- else
- {
- throw new RuntimeException ("You entered a wrong letter");
- }
- int[][] result=new int[ans.size()][2];
- for(int i=0;i<ans.size();i++)
- {
- for(int j=i+1;j<ans.size();j++)
- {
- if(ans.elementAt(i).getY()>ans.elementAt(j).getY())
- {
- Point temp=ans.elementAt(i);
- ans.set(i,ans.elementAt(j));
- ans.set(j,temp);
- }
- }
- }
- for(int i=0;i<ans.size();i++)
- {
- for(int j=i+1;j<ans.size();j++)
- {
- if(ans.elementAt(i).getY()==ans.elementAt(j).getY())
- {
- Point temp1=ans.elementAt(i),temp2=ans.elementAt(j);;
- Point fin=new Point((int)temp1.getX()+(int)temp2.getX(),(int)temp1.getY());
- ans.set(i,fin);
- ans.remove(j);
- j--;
- }
- }
- }
- R.clear();
- for(int i=0;i<ans.size();i++)
- R.add(ans.elementAt(i));
- for(int i=0;i<ans.size();i++)
- {
- result[i][0]=(int)ans.elementAt(i).getX();
- result[i][1]=(int)ans.elementAt(i).getY();
- }
- return result;
- }
- @Override
- public int[][] subtract(char poly1, char poly2) {
- Vector<Point> ans=new Vector<Point>();
- if((poly1=='A'&&poly2=='B'))
- {
- if(A.isEmpty())
- throw new ArithmeticException("A is empty");
- if(B.isEmpty())
- throw new ArithmeticException("B is empty");
- int size1=A.size(),size2=B.size();
- boolean[]visited=new boolean[size2];
- for(int i=0;i<size1;i++)
- {
- boolean added=false;
- Point first= (Point) A.get(i);
- for(int j=0;j<size2;j++)
- {
- Point second=(Point)B.get(j);
- if(first.getY()==second.getY())
- {
- Point temp=new Point((int)first.getX()-(int)second.getX(),(int)first.getY());
- ans.add(temp);
- added=true;
- visited[j]=true;
- }
- }
- if(!added)
- ans.add(first);
- }
- for(int j=0;j<size2;j++)
- {
- if(!visited[j])
- {
- Point temp=new Point((Point)B.get(j));
- temp.setLocation(-temp.getX(),temp.getY());
- ans.add(temp);
- }
- }
- }
- else if((poly1=='B'&&poly2=='A'))
- {
- if(B.isEmpty())
- throw new ArithmeticException("A is empty");
- if(A.isEmpty())
- throw new ArithmeticException("B is empty");
- int size1=B.size(),size2=A.size();
- boolean[]visited=new boolean[size2];
- for(int i=0;i<size1;i++)
- {
- boolean added=false;
- Point first= (Point) B.get(i);
- for(int j=0;j<size2;j++)
- {
- Point second=(Point)A.get(j);
- if(first.getY()==second.getY())
- {
- Point temp=new Point((int)first.getX()-(int)second.getX(),(int)first.getY());
- ans.add(temp);
- added=true;
- visited[j]=true;
- }
- }
- if(!added)
- ans.add(first);
- }
- for(int j=0;j<size2;j++)
- {
- if(!visited[j])
- {
- Point temp=new Point((Point)B.get(j));
- temp.setLocation(-temp.getX(),temp.getY());
- ans.add(temp);
- }
- }
- }
- else if(poly1=='C'&&poly2=='A')
- {
- if(C.isEmpty())
- throw new ArithmeticException("A is empty");
- if(A.isEmpty())
- throw new ArithmeticException("C is empty");
- int size1=C.size(),size2=A.size();
- boolean[]visited=new boolean[size2];
- for(int i=0;i<size1;i++)
- {
- boolean added=false;
- Point first= (Point) C.get(i);
- for(int j=0;j<size2;j++)
- {
- Point second=(Point)A.get(j);
- if(first.getY()==second.getY())
- {
- Point temp=new Point((int)first.getX()-(int)second.getX(),(int)first.getY());
- ans.add(temp);
- added=true;
- visited[j]=true;
- }
- }
- if(!added)
- ans.add(first);
- }
- for(int j=0;j<size2;j++)
- {
- if(!visited[j])
- {
- Point temp=new Point((Point)B.get(j));
- temp.setLocation(-temp.getX(),temp.getY());
- ans.add(temp);
- }
- }
- }
- else if(poly1=='A'&&poly2=='C')
- {
- if(A.isEmpty())
- throw new ArithmeticException("A is empty");
- if(C.isEmpty())
- throw new ArithmeticException("C is empty");
- int size1=A.size(),size2=C.size();
- boolean[]visited=new boolean[size2];
- for(int i=0;i<size1;i++)
- {
- boolean added=false;
- Point first= (Point) A.get(i);
- for(int j=0;j<size2;j++)
- {
- Point second=(Point)C.get(j);
- if(first.getY()==second.getY())
- {
- Point temp=new Point((int)first.getX()-(int)second.getX(),(int)first.getY());
- ans.add(temp);
- added=true;
- visited[j]=true;
- }
- }
- if(!added)
- ans.add(first);
- }
- for(int j=0;j<size2;j++)
- {
- if(!visited[j])
- {
- Point temp=new Point((Point)B.get(j));
- temp.setLocation(-temp.getX(),temp.getY());
- ans.add(temp);
- }
- }
- }
- else if(poly1=='B'&&poly2=='C')
- {
- if(B.isEmpty())
- throw new ArithmeticException("B is empty");
- if(C.isEmpty())
- throw new ArithmeticException("C is empty");
- int size1=B.size(),size2=C.size();
- boolean[]visited=new boolean[size2];
- for(int i=0;i<size1;i++)
- {
- boolean added=false;
- Point first= (Point) B.get(i);
- for(int j=0;j<size2;j++)
- {
- Point second=(Point)C.get(j);
- if(first.getY()==second.getY())
- {
- Point temp=new Point((int)first.getX()-(int)second.getX(),(int)first.getY());
- ans.add(temp);
- added=true;
- visited[j]=true;
- }
- }
- if(!added)
- ans.add(first);
- }
- for(int j=0;j<size2;j++)
- {
- if(!visited[j])
- {
- Point temp=new Point((Point)B.get(j));
- temp.setLocation(-temp.getX(),temp.getY());
- ans.add(temp);
- }
- }
- }
- else if(poly1=='C'&&poly2=='B')
- {
- if(C.isEmpty())
- throw new ArithmeticException("C is empty");
- if(B.isEmpty())
- throw new ArithmeticException("B is empty");
- int size1=C.size(),size2=B.size();
- boolean[]visited=new boolean[size2];
- for(int i=0;i<size1;i++)
- {
- boolean added=false;
- Point first= (Point) C.get(i);
- for(int j=0;j<size2;j++)
- {
- Point second=(Point)B.get(j);
- if(first.getY()==second.getY())
- {
- Point temp=new Point((int)first.getX()-(int)second.getX(),(int)first.getY());
- ans.add(temp);
- added=true;
- visited[j]=true;
- }
- }
- if(!added)
- ans.add(first);
- }
- for(int j=0;j<size2;j++)
- {
- if(!visited[j])
- {
- Point temp=new Point((Point)B.get(j));
- temp.setLocation(-temp.getX(),temp.getY());
- ans.add(temp);
- }
- }
- }
- if((poly1=='A'&&poly2=='A'))
- {
- if(A.isEmpty())
- throw new ArithmeticException("A is empty");
- int size1=A.size(),size2=A.size();
- boolean[]visited=new boolean[size2];
- for(int i=0;i<size1;i++)
- {
- boolean added=false;
- Point first= (Point) A.get(i);
- for(int j=0;j<size2;j++)
- {
- Point second=(Point)A.get(j);
- if(first.getY()==second.getY())
- {
- Point temp=new Point((int)first.getX()-(int)second.getX(),(int)first.getY());
- ans.add(temp);
- added=true;
- visited[j]=true;
- }
- }
- if(!added)
- ans.add(first);
- }
- for(int j=0;j<size2;j++)
- {
- if(!visited[j])
- {
- Point temp=new Point((Point)A.get(j));
- temp.setLocation(-temp.getX(),temp.getY());
- ans.add(temp);
- }
- }
- }
- if((poly1=='B'&&poly2=='B'))
- {
- if(B.isEmpty())
- throw new ArithmeticException("B is empty");
- int size1=B.size(),size2=B.size();
- boolean[]visited=new boolean[size2];
- for(int i=0;i<size1;i++)
- {
- boolean added=false;
- Point first= (Point) B.get(i);
- for(int j=0;j<size2;j++)
- {
- Point second=(Point)B.get(j);
- if(first.getY()==second.getY())
- {
- Point temp=new Point((int)first.getX()-(int)second.getX(),(int)first.getY());
- ans.add(temp);
- added=true;
- visited[j]=true;
- }
- }
- if(!added)
- ans.add(first);
- }
- for(int j=0;j<size2;j++)
- {
- if(!visited[j])
- {
- Point temp=new Point((Point)B.get(j));
- temp.setLocation(-temp.getX(),temp.getY());
- ans.add(temp);
- }
- }
- }
- if((poly1=='C'&&poly2=='C'))
- {
- if(C.isEmpty())
- throw new ArithmeticException("C is empty");
- int size1=A.size(),size2=B.size();
- boolean[]visited=new boolean[size2];
- for(int i=0;i<size1;i++)
- {
- boolean added=false;
- Point first= (Point) C.get(i);
- for(int j=0;j<size2;j++)
- {
- Point second=(Point)C.get(j);
- if(first.getY()==second.getY())
- {
- Point temp=new Point((int)first.getX()-(int)second.getX(),(int)first.getY());
- ans.add(temp);
- added=true;
- visited[j]=true;
- }
- }
- if(!added)
- ans.add(first);
- }
- for(int j=0;j<size2;j++)
- {
- if(!visited[j])
- {
- Point temp=new Point((Point)C.get(j));
- temp.setLocation(-temp.getX(),temp.getY());
- ans.add(temp);
- }
- }
- }
- else
- {
- throw new RuntimeException ("You entered a wrong letter");
- }
- int[][] result=new int[ans.size()][2];
- for(int i=0;i<ans.size();i++)
- {
- for(int j=i+1;j<ans.size();j++)
- {
- if(ans.elementAt(i).getY()>ans.elementAt(j).getY())
- {
- Point temp=ans.elementAt(i);
- ans.set(i,ans.elementAt(j));
- ans.set(j,temp);
- }
- }
- }
- for(int i=0;i<ans.size();i++)
- {
- for(int j=i+1;j<ans.size();j++)
- {
- if(ans.elementAt(i).getY()==ans.elementAt(j).getY())
- {
- Point temp1=ans.elementAt(i),temp2=ans.elementAt(j);;
- Point fin=new Point((int)temp1.getX()+(int)temp2.getX(),(int)temp1.getY());
- ans.set(i,fin);
- ans.remove(j);
- j--;
- }
- }
- }
- R.clear();
- for(int i=0;i<ans.size();i++)
- R.add(ans.elementAt(i));
- for(int i=0;i<ans.size();i++)
- {
- result[i][0]=(int)ans.elementAt(i).getX();
- result[i][1]=(int)ans.elementAt(i).getY();
- }
- return result;
- }
- @Override
- public int[][] multiply(char poly1, char poly2)
- {
- Vector<Point> ans=new Vector<Point>();
- if((poly1=='A'&&poly2=='B')||(poly1=='B'&&poly2=='A'))
- {
- if(A.isEmpty())
- throw new ArithmeticException("A is empty");
- if(B.isEmpty())
- throw new ArithmeticException("B is empty");
- int size1=A.size(),size2=B.size();
- for(int i=0;i<size1;i++)
- {
- Point first= (Point) A.get(i);
- for(int j=0;j<size2;j++)
- {
- Point second=(Point)B.get(j);
- Point temp=new Point((int)first.getX()*(int)second.getX(),(int)first.getY()+(int)second.getY());
- ans.add(temp);
- }
- }
- }
- else if((poly1=='A'&&poly2=='C')||(poly1=='C'&&poly2=='A'))
- {
- if(A.isEmpty())
- throw new ArithmeticException("A is empty");
- if(C.isEmpty())
- throw new ArithmeticException("C is empty");
- int size1=A.size(),size2=C.size();
- for(int i=0;i<size1;i++)
- {
- Point first= (Point) A.get(i);
- for(int j=0;j<size2;j++)
- {
- Point second=(Point)C.get(j);
- Point temp=new Point((int)first.getX()*(int)second.getX(),(int)first.getY()+(int)second.getY());
- ans.add(temp);
- }
- }
- }
- else if((poly1=='C'&&poly2=='B')||(poly1=='B'&&poly2=='C'))
- {
- if(C.isEmpty())
- throw new ArithmeticException("C is empty");
- if(B.isEmpty())
- throw new ArithmeticException("B is empty");
- int size1=C.size(),size2=B.size();
- for(int i=0;i<size1;i++)
- {
- Point first= (Point) C.get(i);
- for(int j=0;j<size2;j++)
- {
- Point second=(Point)B.get(j);
- Point temp=new Point((int)first.getX()*(int)second.getX(),(int)first.getY()+(int)second.getY());
- ans.add(temp);
- }
- }
- }
- else if(poly1=='C'&&poly2=='C')
- {
- if(C.isEmpty())
- throw new ArithmeticException("C is empty");
- int size1=C.size(),size2=C.size();
- for(int i=0;i<size1;i++)
- {
- Point first= (Point) C.get(i);
- for(int j=0;j<size2;j++)
- {
- Point second=(Point)C.get(j);
- Point temp=new Point((int)first.getX()*(int)second.getX(),(int)first.getY()+(int)second.getY());
- ans.add(temp);
- }
- }
- }
- else if(poly1=='B'&&poly2=='B')
- {
- if(B.isEmpty())
- throw new ArithmeticException("B is empty");
- int size1=B.size(),size2=B.size();
- for(int i=0;i<size1;i++)
- {
- Point first= (Point) B.get(i);
- for(int j=0;j<size2;j++)
- {
- Point second=(Point)B.get(j);
- Point temp=new Point((int)first.getX()*(int)second.getX(),(int)first.getY()+(int)second.getY());
- ans.add(temp);
- }
- }
- }
- else if(poly1=='A'&&poly2=='A')
- {
- if(A.isEmpty())
- throw new ArithmeticException("A is empty");
- int size1=A.size(),size2=A.size();
- for(int i=0;i<size1;i++)
- {
- Point first= (Point) A.get(i);
- for(int j=0;j<size2;j++)
- {
- Point second=(Point)A.get(j);
- Point temp=new Point((int)first.getX()*(int)second.getX(),(int)first.getY()+(int)second.getY());
- ans.add(temp);
- }
- }
- }
- else
- {
- throw new RuntimeException ("You entered a wrong letter");
- }
- int[][] result=new int[ans.size()][2];
- for(int i=0;i<ans.size();i++)
- {
- for(int j=i+1;j<ans.size();j++)
- {
- if(ans.elementAt(i).getY()>ans.elementAt(j).getY())
- {
- Point temp=ans.elementAt(i);
- ans.set(i,ans.elementAt(j));
- ans.set(j,temp);
- }
- }
- }
- for(int i=0;i<ans.size();i++)
- {
- for(int j=i+1;j<ans.size();j++)
- {
- if(ans.elementAt(i).getY()==ans.elementAt(j).getY())
- {
- Point temp1=ans.elementAt(i),temp2=ans.elementAt(j);;
- Point fin=new Point((int)temp1.getX()+(int)temp2.getX(),(int)temp1.getY());
- ans.set(i,fin);
- ans.remove(j);
- j--;
- }
- }
- }
- R.clear();
- for(int i=0;i<ans.size();i++)
- R.add(ans.elementAt(i));
- for(int i=0;i<ans.size();i++)
- {
- result[i][0]=(int)ans.elementAt(i).getX();
- result[i][1]=(int)ans.elementAt(i).getY();
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement