keymasterviriya1150

Untitled

Dec 3rd, 2015
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.16 KB | None | 0 0
  1. package matrix;
  2. import java.util.Scanner;
  3. import static java.lang.System.out;
  4. import static java.lang.System.err;
  5. import java.math.BigInteger;
  6. import java.util.ArrayList;
  7. import java.util.InputMismatchException;
  8.  
  9. public class mahterfucker extends output {
  10.    
  11.     static Scanner sc = new Scanner(System.in);    
  12.     public static void main(String[] args) {
  13.         ArrayList<Matrix> arrlist = new ArrayList<Matrix>();
  14.         arrlist.add(new Matrix(0,0));
  15.         arrlist.add(new Matrix(0,0));
  16.    
  17.         p("1.add Matrix\n"+
  18.           "2.subtract Matrix\n");
  19.         p("Choose action:");
  20.         try{
  21.             Byte i=sc.nextByte();
  22.             if(i==1)
  23.             {
  24.                 p(""+run(arrlist.get(0),i));
  25.             }
  26.             else if(i==2)
  27.             {
  28.                 p(""+run(arrlist.get(1),i));
  29.             }
  30.             else
  31.             {
  32.                 p("pls enter positive integer 1 or 2");
  33.             }
  34.                
  35.         }catch(InputMismatchException e)
  36.         {
  37.             p("ERROR"+e);
  38.             System.exit(0);
  39.         }
  40.     }
  41.     static public  int  inputrow()
  42.     {
  43.         int row;
  44.         try{
  45.        p("׺°”˜`”°º× ׺°”˜`”°º×\n");
  46.         p("ENTER ROW:");
  47.         row = sc.nextInt();
  48.         return row;
  49.         }catch(InputMismatchException e)
  50.         {
  51.             p("ERROR:"+e);
  52.             System.exit(1);
  53.         }
  54.         return 0;
  55.     }
  56.     static public  int  inputcol()
  57.     {
  58.         int col;
  59.         try{
  60.        p("ENTER Colum:");
  61.         col = sc.nextInt();
  62.         p("׺°”˜`”°º× ׺°”˜`”°º×\n");
  63.          return col;
  64.         }catch(InputMismatchException e)
  65.              {
  66.             p("ERROR:"+e);
  67.             System.exit(1);
  68.         }
  69.        return 0;
  70.     }
  71.    
  72.    
  73.     static public Matrix run(Matrix g,byte r) {
  74.  
  75.         int number;
  76.         int row=inputrow();
  77.         int col=inputcol();
  78.         p("\n.•*´¨`*•.¸¸.•*´¨`*Matrix A.¸¸.•.¸¸.•*´¨`*•.¸¸.\n");
  79.         Matrix m = new Matrix(row, col);
  80.         MyInteger intOne = null;
  81.         MyInteger inttwo = null;
  82.         for (int i = 0; i < row; i++) {
  83.             for (int j = 0; j < col; j++) {
  84.                 p("Position" + "[" + i + "]" + "[" + j + "]: ");
  85.                 try{
  86.                     number = sc.nextInt();
  87.                     intOne = new MyInteger(number);
  88.                     m.set(i, j, intOne);
  89.                 }catch(InputMismatchException e)
  90.                 {
  91.                     p("ERROR:"+e);
  92.                     System.exit(1);
  93.                 }
  94.             }
  95.         }
  96.          row=inputrow();
  97.          col=inputcol();
  98.        
  99.         p("\n.•*´¨`*•.¸¸.•*´¨`*Matrix B.¸¸.•.¸¸.•*´¨`*•.¸¸.\n");
  100.         Matrix n = new Matrix(row, col);
  101.         for (int i = 0; i < row; i++) {
  102.             for (int j = 0; j < col; j++) {
  103.                 p("Position" + "[" + i + "]" + "[" + j + "]: ");
  104.                 number = sc.nextInt();
  105.                 inttwo = new MyInteger(number);
  106.                 n.set(i, j, inttwo);
  107.             }
  108.         }
  109.         if(r==1)
  110.             return m.add(n);
  111.         else if(r==2)
  112.             return  m.subtract(n);
  113.         return g;
  114.     }
  115.    
  116. }
  117.  
  118. class Matrix extends output {
  119.  
  120.     private int row;
  121.     private int col;
  122.     private Object[][] m;
  123.  
  124.     Matrix(int row, int col) {
  125.         if (row < 0 || col < 0) {
  126.             err.println("Row and col must be >=0");
  127.             System.exit(1);
  128.         }
  129.         this.row = row;
  130.         this.col = col;
  131.         m = new Object[row][col];
  132.     }
  133.  
  134.     private void cheakindex(int i, int j) {
  135.         if (i < 0 || j < 0 || i > row || j > col) {
  136.             err.println("Invalid index");
  137.             System.exit(1);
  138.         }
  139.     }
  140.  
  141.     void set(int r, int c, Object value) {
  142.         cheakindex(r, c);
  143.         m[r][c] = value;
  144.     }
  145.  
  146.     public Matrix add(Matrix n) {
  147.         //MyInteger g = new MyInteger(0);
  148.         if (row != n.row || col != n.col) {
  149.             err.println("Size of matriced do not match");
  150.             System.exit(1);
  151.         }
  152.         Matrix p = new Matrix(row, col);
  153.         p("\n.•*´¨`*•.¸¸.•*´¨`*Matrix add.¸¸.•.¸¸.•*´¨`*•.¸¸.\n");
  154.         for (int i = 0; i < row; i++) {
  155.             for (int j = 0; j < col; j++) {
  156.                 p.m[i][j] = ((MyInteger) m[i][j]).add(n.m[i][j]);
  157.             }
  158.         }
  159.         return p;
  160.     }
  161.  
  162.     public Matrix subtract(Matrix n) {
  163.         if (row != n.row || col != n.col) {
  164.             err.println("Size of matrices do not math");
  165.             System.exit(1);
  166.         }
  167.         Matrix p = new Matrix(row, col);
  168.         p("\n.•*´¨`*•.¸¸.•*´¨`*Matrix subtract.¸¸.•.¸¸.•*´¨`*•.¸¸.\n");
  169.         for (int i = 0; i < row; i++) {
  170.             for (int j = 0; j < col; j++) {
  171.                 p.m[i][j] = ((Computable) m[i][j]).subtract(n.m[i][j]);
  172.             }
  173.         }
  174.         return p;
  175.     }
  176.  
  177.     @Override
  178.     public String toString() {
  179.         StringBuffer buf = new StringBuffer();
  180.         for (int i = 0; i < row; i++) {
  181.             for (int j = 0; j < col; j++) {
  182.                 buf.append(m[i][j].toString() + "\t");
  183.             }
  184.             buf.append("\n");
  185.         }
  186.         return new String(buf);
  187.        
  188.     }
  189.    
  190. }
  191.  
  192. class MyInteger implements Computable {
  193.  
  194.     private int value;
  195.  
  196.     public MyInteger(int value) {
  197.         this.value = value;
  198.     }
  199.  
  200.     @Override
  201.     public String toString() {
  202.         return Integer.toString(value);
  203.     }
  204.  
  205.     @Override
  206.     public Object add(Object m) {
  207.         return new MyInteger(value + ((MyInteger) m).value);
  208.     }
  209.  
  210.     @Override
  211.     public Object subtract(Object m) {
  212.         return new MyInteger(value - ((MyInteger) m).value);
  213.     }
  214.  
  215.     public Object muti(Object m) {
  216.         return new MyInteger(value * ((MyInteger) m).value);
  217.     }
  218.    
  219. }
  220.  
  221. interface Computable {
  222.  
  223.     public Object add(Object thisObject);
  224.  
  225.     public Object subtract(Object thisObject);
  226.     //public  Object muti(Object thisObject);
  227.    
  228. }
  229.  
  230. class output {
  231.  
  232.     public static void p(String input) {
  233.         out.print(input);
  234.     }
  235. }
Advertisement
Add Comment
Please, Sign In to add comment