Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 5730213093 Nantipat Tullwattana SE2
- package matrix;
- import java.util.Scanner;
- import static java.lang.System.out;
- import static java.lang.System.err;
- import java.util.ArrayList;
- import java.util.InputMismatchException;
- import static matrix.Programing.sc;
- public class Programing extends output {
- static Scanner sc = new Scanner(System.in);
- public static void main(String[] args) {
- ArrayList<Matrix> arrlist = new ArrayList<Matrix>();
- int n = 2;
- for (int i = 0; i < n; i++) {
- arrlist.add(i, new Matrix(0, 0));
- }
- p("1.add Matrix\n"
- + "2.subtract Matrix\n");
- p("Choose action:");
- try {
- Byte i = sc.nextByte();
- if (i == 1) {
- p("" + run(arrlist.get(0), i));
- } else if (i == 2) {
- p("" + run(arrlist.get(1), i));
- } else {
- p("pls enter positive integer 1 or 2");
- System.exit(0);
- }
- } catch (InputMismatchException e) {
- p("ERROR" + e);
- System.exit(0);
- }
- try {
- p("1.Search\n" + "2.Remove\n" + "3.Insert\n"
- + "Select Menu:");
- Byte c = sc.nextByte();
- if (c == 1) {
- p("Search Position Arraylsit:");
- int k = sc.nextByte();
- for (int i = 0; i < arrlist.size(); i++) {
- if (i == k) {
- p("true");
- System.exit(0);
- }
- }
- p("false");
- } else if (c == 2) {
- p("Remove index Arraylist :");
- int k = sc.nextByte();
- for (int i = 0; i < arrlist.size(); i++) {
- if (i == k) {
- arrlist.remove(i);
- p("Succes");
- System.exit(0);
- }
- }
- p("don't Succes");
- } else if (c == 3) {
- p("Enter position Array list for Insert");
- int y = sc.nextInt();
- arrlist.add(y, new Matrix(0, 0));
- } else {
- p("pls enter number 1 || 2 || 3");
- System.exit(0);
- }
- } catch (InputMismatchException e) {
- p("ERROR");
- System.exit(0);
- }
- }
- static public int inputrow() {
- int row;
- try {
- p("׺°”˜`”°º× ׺°”˜`”°º×\n");
- p("ENTER ROW:");
- row = sc.nextInt();
- return row;
- } catch (InputMismatchException e) {
- p("ERROR:" + e);
- System.exit(0);
- }
- return 0;
- }
- static public int inputcol() {
- int col;
- try {
- p("ENTER Colum:");
- col = sc.nextInt();
- p("׺°”˜`”°º× ׺°”˜`”°º×\n");
- return col;
- } catch (InputMismatchException e) {
- p("ERROR:" + e);
- System.exit(0);
- }
- return 0;
- }
- static public Matrix run(Matrix g, byte r) {
- int number;
- int row = inputrow();
- int col = inputcol();
- p("\n.•*´¨`*•.¸¸.•*´¨`*Matrix A.¸¸.•.¸¸.•*´¨`*•.¸¸.\n");
- Matrix m = new Matrix(row, col);
- MyInteger intOne = null;
- MyInteger inttwo = null;
- for (int i = 0; i < row; i++) {
- for (int j = 0; j < col; j++) {
- p("Position" + "[" + i + "]" + "[" + j + "]: ");
- try {
- number = sc.nextInt();
- intOne = new MyInteger(number);
- m.set(i, j, intOne);
- } catch (InputMismatchException e) {
- p("ERROR:" + e);
- System.exit(0);
- }
- }
- }
- row = inputrow();
- col = inputcol();
- p("\n.•*´¨`*•.¸¸.•*´¨`*Matrix B.¸¸.•.¸¸.•*´¨`*•.¸¸.\n");
- Matrix n = new Matrix(row, col);
- for (int i = 0; i < row; i++) {
- for (int j = 0; j < col; j++) {
- p("Position" + "[" + i + "]" + "[" + j + "]: ");
- number = sc.nextInt();
- inttwo = new MyInteger(number);
- n.set(i, j, inttwo);
- }
- }
- if (r == 1) {
- return m.add(n);
- } else if (r == 2) {
- return m.subtract(n);
- }
- return g;
- }
- }
- class Matrix extends output {
- private int row;
- private int col;
- private Object[][] m;
- Matrix(int row, int col) {
- if (row < 0 || col < 0) {
- err.println("Row and col must be >=0");
- System.exit(0);
- }
- this.row = row;
- this.col = col;
- m = new Object[row][col];
- }
- private void cheakindex(int i, int j) {
- if (i < 0 || j < 0 || i > row || j > col) {
- err.println("Invalid index");
- System.exit(0);
- }
- }
- void set(int r, int c, Object value) {
- cheakindex(r, c);
- m[r][c] = value;
- }
- public Matrix add(Matrix n) {
- //MyInteger g = new MyInteger(0);
- if (row != n.row || col != n.col) {
- err.println("Size of matriced do not match");
- System.exit(0);
- }
- Matrix p = new Matrix(row, col);
- for (int i = 0; i < row; i++) {
- for (int j = 0; j < col; j++) {
- p.m[i][j] = ((MyInteger) m[i][j]).add(n.m[i][j]);
- }
- }
- return p;
- }
- public Matrix subtract(Matrix n) {
- if (row != n.row || col != n.col) {
- err.println("Size of matrices do not math");
- System.exit(1);
- }
- Matrix p = new Matrix(row, col);
- //p("\n.•*´¨`*•.¸¸.•*´¨`*Matrix subtract.¸¸.•.¸¸.•*´¨`*•.¸¸.\n");
- for (int i = 0; i < row; i++) {
- for (int j = 0; j < col; j++) {
- p.m[i][j] = ((Computable) m[i][j]).subtract(n.m[i][j]);
- }
- }
- return p;
- }
- @Override
- public String toString() {
- StringBuffer buf = new StringBuffer();
- for (int i = 0; i < row; i++) {
- for (int j = 0; j < col; j++) {
- buf.append(m[i][j].toString() + "\t");
- }
- buf.append("\n");
- }
- buf.append(".•*´¨`*•.¸¸.•*´¨`*Matrix Edit result.¸¸.•.¸¸.•*´¨`*•.¸¸.\n");
- try{
- p("Edit Position\n");
- p("Select Position Edit Row:");
- int jungrow = sc.nextInt();
- p("Select Position Edit Col:");
- int jungcol = sc.nextInt();
- for (int i = 0; i < row; i++) {
- for (int j = 0; j < col; j++) {
- if (jungrow == i && jungcol == j) {
- p("Edit value:");
- m[i][j] = sc.next();
- }
- buf.append(m[i][j].toString() + "\t");
- }
- buf.append("\n");
- }
- }catch(InputMismatchException e)
- {
- p("ERROR"+e);
- }
- p("\n.•*´¨`*•.¸¸.•*´¨`*Matrix Result.¸¸.•.¸¸.•*´¨`*•.¸¸.\n");
- return new String(buf);
- }
- }
- class MyInteger implements Computable {
- private int value;
- public MyInteger(int value) {
- this.value = value;
- }
- @Override
- public String toString() {
- return Integer.toString(value);
- }
- @Override
- public Object add(Object m) {
- return new MyInteger(value + ((MyInteger) m).value);
- }
- @Override
- public Object subtract(Object m) {
- return new MyInteger(value - ((MyInteger) m).value);
- }
- public Object muti(Object m) {
- return new MyInteger(value * ((MyInteger) m).value);
- }
- }
- interface Computable {
- public Object add(Object thisObject);
- public Object subtract(Object thisObject);
- }
- class output {
- public static void p(String input) {
- out.print(input);
- }
- }
- /*
- run:
- 1.add Matrix
- 2.subtract Matrix
- Choose action:1
- ׺°”˜`”°º× ׺°”˜`”°º×
- ENTER ROW:2
- ENTER Colum:2
- ׺°”˜`”°º× ׺°”˜`”°º×
- .•*´¨`*•.¸¸.•*´¨`*Matrix A.¸¸.•.¸¸.•*´¨`*•.¸¸.
- Position[0][0]: 1
- Position[0][1]: 2
- Position[1][0]: 3
- Position[1][1]: 4
- ׺°”˜`”°º× ׺°”˜`”°º×
- ENTER ROW:2
- ENTER Colum:2
- ׺°”˜`”°º× ׺°”˜`”°º×
- .•*´¨`*•.¸¸.•*´¨`*Matrix B.¸¸.•.¸¸.•*´¨`*•.¸¸.
- Position[0][0]: 1
- Position[0][1]: 2
- Position[1][0]: 3
- Position[1][1]: 4
- Edit Position
- Select Position Edit Row:0
- Select Position Edit Col:0
- Edit value:9999
- .•*´¨`*•.¸¸.•*´¨`*Matrix Result.¸¸.•.¸¸.•*´¨`*•.¸¸.
- 2 4
- 6 8
- .•*´¨`*•.¸¸.•*´¨`*Matrix Edit result.¸¸.•.¸¸.•*´¨`*•.¸¸.
- 9999 4
- 6 8
- 1.Search
- 2.Remove
- 3.Insert
- Select Menu:1
- Search Position Arraylsit:1
- trueBUILD SUCCESSFUL (total time: 18 seconds)
- */
Advertisement
Add Comment
Please, Sign In to add comment