keymasterviriya1150

Untitled

Dec 3rd, 2015
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.22 KB | None | 0 0
  1. // 5730213093 Nantipat  Tullwattana SE2
  2. package matrix;
  3.  
  4. import java.util.Scanner;
  5. import static java.lang.System.out;
  6. import static java.lang.System.err;
  7. import java.util.ArrayList;
  8. import java.util.InputMismatchException;
  9. import static matrix.Programing.sc;
  10.  
  11. public class Programing extends output {
  12.  
  13.     static Scanner sc = new Scanner(System.in);
  14.  
  15.     public static void main(String[] args) {
  16.         ArrayList<Matrix> arrlist = new ArrayList<Matrix>();
  17.         int n = 2;
  18.         for (int i = 0; i < n; i++) {
  19.             arrlist.add(i, new Matrix(0, 0));
  20.         }
  21.         p("1.add Matrix\n"
  22.                 + "2.subtract Matrix\n");
  23.         p("Choose action:");
  24.         try {
  25.             Byte i = sc.nextByte();
  26.             if (i == 1) {
  27.                 p("" + run(arrlist.get(0), i));
  28.              
  29.             } else if (i == 2) {
  30.                 p("" + run(arrlist.get(1), i));
  31.             } else {
  32.                 p("pls enter positive integer 1 or 2");
  33.                 System.exit(0);
  34.             }
  35.  
  36.         } catch (InputMismatchException e) {
  37.             p("ERROR" + e);
  38.             System.exit(0);
  39.         }
  40.         try {
  41.             p("1.Search\n" + "2.Remove\n" + "3.Insert\n"
  42.                     + "Select Menu:");
  43.             Byte c = sc.nextByte();
  44.             if (c == 1) {
  45.                 p("Search Position Arraylsit:");
  46.                 int k = sc.nextByte();
  47.                 for (int i = 0; i < arrlist.size(); i++) {
  48.                     if (i == k) {
  49.                         p("true");
  50.                         System.exit(0);
  51.                     }
  52.                 }
  53.                 p("false");
  54.  
  55.             } else if (c == 2) {
  56.                 p("Remove index Arraylist :");
  57.                 int k = sc.nextByte();
  58.                 for (int i = 0; i < arrlist.size(); i++) {
  59.                     if (i == k) {
  60.                         arrlist.remove(i);
  61.                         p("Succes");
  62.                         System.exit(0);
  63.                     }
  64.                 }
  65.                 p("don't Succes");
  66.             } else if (c == 3) {
  67.                 p("Enter position Array list for Insert");
  68.                 int y = sc.nextInt();
  69.                 arrlist.add(y, new Matrix(0, 0));
  70.  
  71.             } else {
  72.                 p("pls enter number 1 || 2 || 3");
  73.                 System.exit(0);
  74.             }
  75.         } catch (InputMismatchException e) {
  76.             p("ERROR");
  77.             System.exit(0);
  78.         }
  79.  
  80.     }
  81.  
  82.     static public int inputrow() {
  83.         int row;
  84.         try {
  85.             p("׺°”˜`”°º× ׺°”˜`”°º×\n");
  86.             p("ENTER ROW:");
  87.             row = sc.nextInt();
  88.             return row;
  89.         } catch (InputMismatchException e) {
  90.             p("ERROR:" + e);
  91.             System.exit(0);
  92.         }
  93.         return 0;
  94.     }
  95.  
  96.     static public int inputcol() {
  97.         int col;
  98.         try {
  99.             p("ENTER Colum:");
  100.             col = sc.nextInt();
  101.             p("׺°”˜`”°º× ׺°”˜`”°º×\n");
  102.             return col;
  103.         } catch (InputMismatchException e) {
  104.             p("ERROR:" + e);
  105.             System.exit(0);
  106.         }
  107.         return 0;
  108.     }
  109.  
  110.     static public Matrix run(Matrix g, byte r) {
  111.  
  112.         int number;
  113.         int row = inputrow();
  114.         int col = inputcol();
  115.         p("\n.•*´¨`*•.¸¸.•*´¨`*Matrix A.¸¸.•.¸¸.•*´¨`*•.¸¸.\n");
  116.         Matrix m = new Matrix(row, col);
  117.         MyInteger intOne = null;
  118.         MyInteger inttwo = null;
  119.         for (int i = 0; i < row; i++) {
  120.             for (int j = 0; j < col; j++) {
  121.                 p("Position" + "[" + i + "]" + "[" + j + "]: ");
  122.                 try {
  123.                     number = sc.nextInt();
  124.                     intOne = new MyInteger(number);
  125.                     m.set(i, j, intOne);
  126.                 } catch (InputMismatchException e) {
  127.                     p("ERROR:" + e);
  128.                     System.exit(0);
  129.                 }
  130.             }
  131.         }
  132.         row = inputrow();
  133.         col = inputcol();
  134.  
  135.         p("\n.•*´¨`*•.¸¸.•*´¨`*Matrix B.¸¸.•.¸¸.•*´¨`*•.¸¸.\n");
  136.         Matrix n = new Matrix(row, col);
  137.         for (int i = 0; i < row; i++) {
  138.             for (int j = 0; j < col; j++) {
  139.                 p("Position" + "[" + i + "]" + "[" + j + "]: ");
  140.                 number = sc.nextInt();
  141.                 inttwo = new MyInteger(number);
  142.                 n.set(i, j, inttwo);
  143.             }
  144.         }
  145.         if (r == 1) {
  146.             return m.add(n);
  147.         } else if (r == 2) {
  148.             return m.subtract(n);
  149.         }
  150.         return g;
  151.     }
  152.  
  153. }
  154.  
  155. class Matrix extends output {
  156.  
  157.     private int row;
  158.     private int col;
  159.     private Object[][] m;
  160.  
  161.     Matrix(int row, int col) {
  162.         if (row < 0 || col < 0) {
  163.             err.println("Row and col must be >=0");
  164.             System.exit(0);
  165.         }
  166.         this.row = row;
  167.         this.col = col;
  168.         m = new Object[row][col];
  169.     }
  170.  
  171.     private void cheakindex(int i, int j) {
  172.         if (i < 0 || j < 0 || i > row || j > col) {
  173.             err.println("Invalid index");
  174.             System.exit(0);
  175.         }
  176.     }
  177.  
  178.     void set(int r, int c, Object value) {
  179.         cheakindex(r, c);
  180.         m[r][c] = value;
  181.     }
  182.  
  183.     public Matrix add(Matrix n) {
  184.         //MyInteger g = new MyInteger(0);
  185.         if (row != n.row || col != n.col) {
  186.             err.println("Size of matriced do not match");
  187.             System.exit(0);
  188.         }
  189.         Matrix p = new Matrix(row, col);
  190.         for (int i = 0; i < row; i++) {
  191.             for (int j = 0; j < col; j++) {
  192.                 p.m[i][j] = ((MyInteger) m[i][j]).add(n.m[i][j]);
  193.             }
  194.         }
  195.         return p;
  196.     }
  197.  
  198.     public Matrix subtract(Matrix n) {
  199.         if (row != n.row || col != n.col) {
  200.             err.println("Size of matrices do not math");
  201.             System.exit(1);
  202.         }
  203.         Matrix p = new Matrix(row, col);
  204.         //p("\n.•*´¨`*•.¸¸.•*´¨`*Matrix subtract.¸¸.•.¸¸.•*´¨`*•.¸¸.\n");
  205.         for (int i = 0; i < row; i++) {
  206.             for (int j = 0; j < col; j++) {
  207.                 p.m[i][j] = ((Computable) m[i][j]).subtract(n.m[i][j]);
  208.             }
  209.         }
  210.         return p;
  211.     }
  212.    
  213.        
  214.     @Override
  215.     public String toString() {
  216.         StringBuffer buf = new StringBuffer();
  217.  
  218.         for (int i = 0; i < row; i++) {
  219.             for (int j = 0; j < col; j++) {
  220.  
  221.                 buf.append(m[i][j].toString() + "\t");
  222.             }
  223.             buf.append("\n");
  224.         }
  225.         buf.append(".•*´¨`*•.¸¸.•*´¨`*Matrix Edit result.¸¸.•.¸¸.•*´¨`*•.¸¸.\n");
  226.        try{
  227.         p("Edit Position\n");
  228.         p("Select Position Edit Row:");
  229.         int jungrow = sc.nextInt();
  230.         p("Select Position Edit Col:");
  231.         int jungcol = sc.nextInt();
  232.         for (int i = 0; i < row; i++) {
  233.             for (int j = 0; j < col; j++) {
  234.                 if (jungrow == i && jungcol == j) {
  235.                     p("Edit value:");
  236.  
  237.                     m[i][j] = sc.next();
  238.                 }
  239.                 buf.append(m[i][j].toString() + "\t");
  240.             }
  241.             buf.append("\n");
  242.         }
  243.        }catch(InputMismatchException e)
  244.        {
  245.            p("ERROR"+e);
  246.        }
  247.         p("\n.•*´¨`*•.¸¸.•*´¨`*Matrix Result.¸¸.•.¸¸.•*´¨`*•.¸¸.\n");
  248.     return new String(buf);
  249.     }
  250. }
  251.  
  252.        
  253. class MyInteger implements Computable {
  254.  
  255.     private int value;
  256.  
  257.     public MyInteger(int value) {
  258.         this.value = value;
  259.     }
  260.  
  261.     @Override
  262.     public String toString() {
  263.         return Integer.toString(value);
  264.     }
  265.  
  266.     @Override
  267.     public Object add(Object m) {
  268.         return new MyInteger(value + ((MyInteger) m).value);
  269.     }
  270.  
  271.     @Override
  272.     public Object subtract(Object m) {
  273.         return new MyInteger(value - ((MyInteger) m).value);
  274.     }
  275.  
  276.     public Object muti(Object m) {
  277.         return new MyInteger(value * ((MyInteger) m).value);
  278.     }
  279.  
  280. }
  281.  
  282. interface Computable {
  283.  
  284.     public Object add(Object thisObject);
  285.  
  286.     public Object subtract(Object thisObject);
  287.  
  288. }
  289.  
  290. class output {
  291.  
  292.     public static void p(String input) {
  293.         out.print(input);
  294.     }
  295. }
  296. /*
  297. run:
  298. 1.add Matrix
  299. 2.subtract Matrix
  300. Choose action:1
  301. ׺°”˜`”°º× ׺°”˜`”°º×
  302. ENTER ROW:2
  303. ENTER Colum:2
  304. ׺°”˜`”°º× ׺°”˜`”°º×
  305.  
  306. .•*´¨`*•.¸¸.•*´¨`*Matrix A.¸¸.•.¸¸.•*´¨`*•.¸¸.
  307. Position[0][0]: 1
  308. Position[0][1]: 2
  309. Position[1][0]: 3
  310. Position[1][1]: 4
  311. ׺°”˜`”°º× ׺°”˜`”°º×
  312. ENTER ROW:2
  313. ENTER Colum:2
  314. ׺°”˜`”°º× ׺°”˜`”°º×
  315.  
  316. .•*´¨`*•.¸¸.•*´¨`*Matrix B.¸¸.•.¸¸.•*´¨`*•.¸¸.
  317. Position[0][0]: 1
  318. Position[0][1]: 2
  319. Position[1][0]: 3
  320. Position[1][1]: 4
  321. Edit Position
  322. Select Position Edit Row:0
  323. Select Position Edit Col:0
  324. Edit value:9999
  325.  
  326. .•*´¨`*•.¸¸.•*´¨`*Matrix Result.¸¸.•.¸¸.•*´¨`*•.¸¸.
  327. 2   4  
  328. 6   8  
  329. .•*´¨`*•.¸¸.•*´¨`*Matrix Edit result.¸¸.•.¸¸.•*´¨`*•.¸¸.
  330. 9999    4  
  331. 6   8  
  332. 1.Search
  333. 2.Remove
  334. 3.Insert
  335. Select Menu:1
  336. Search Position Arraylsit:1
  337. trueBUILD SUCCESSFUL (total time: 18 seconds)
  338. */
Advertisement
Add Comment
Please, Sign In to add comment