Guest User

Untitled

a guest
Jul 22nd, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. package matrix;
  2.  
  3. /**
  4. *
  5. * @author Den
  6. */
  7.  
  8. public class Main {
  9.  
  10. public static void main(String[] args) {
  11. Matrix m1 = new Matrix(3, 3);
  12.  
  13. try {
  14. m1.put(0, 0, 100);
  15. m1.put(0, 1, -5);
  16. m1.put(0, 2, 0);
  17. m1.put(1, 0, 100005);
  18. m1.put(1, 1, -20);
  19. m1.put(1, 2, 64);
  20. m1.put(2, 0, 199910);
  21. m1.put(2, 1, -35);
  22. m1.put(100, 1000, 128);
  23. } catch (MatrixIndex)
  24. {
  25.  
  26. }
  27.  
  28. System.out.println(m1.toString());
  29.  
  30. Matrix m2 = new Matrix(m1);
  31.  
  32. System.out.println(m2.toString());
  33.  
  34. System.out.println(m1.equals(m2));
  35.  
  36. Matrix m3 = new Matrix(2, 2);
  37.  
  38. m3.put(0, 0, 10);
  39. m3.put(0, 1, 53);
  40. m3.put(1, 0, 20);
  41. m3.put(1, 1, 106);
  42.  
  43. System.out.println(m3.toString());
  44. System.out.println(m1.equals(m3));
  45. }
  46.  
  47. }
  48.  
  49.  
  50.  
  51. }
  52.  
  53. package matrix;
  54.  
  55. /**
  56. *
  57. * @author Adil
  58. */
  59. public class Matrix {
  60. private int row;
  61. private int col;
  62. private int[][] data;
  63.  
  64. Matrix(int row, int col) {
  65. this.row = row;
  66. this.col = col;
  67. data = new int[row][col];
  68. }
  69.  
  70. Matrix(Matrix matrix) {
  71. this.row = matrix.getRow();
  72. this.col = matrix.getCol();
  73. data = new int[row][col];
  74.  
  75. for (int i = 0; i < row; i++) {
  76. for (int j = 0; j < col; j++) {
  77. data[i][j] = matrix.data[i][j];
  78. }
  79. }
  80. }
  81.  
  82. public int get(int row, int col) {
  83. return data[row][col];
  84. }
  85.  
  86. void put(int row, int col, int value) {
  87. data[row][col] = value;
  88. }
  89.  
  90. private int getRow() {
  91. return row;
  92. }
  93.  
  94. private int getCol() {
  95. return col;
  96. }
  97.  
  98. @Override
  99. public boolean equals(Object obj) {
  100. Matrix m = (Matrix) obj;
  101.  
  102. if (m.getRow() != row || m.getCol() != col) {
  103. return false;
  104. }
  105.  
  106. for (int i = 0; i < row; i++) {
  107. for (int j = 0; j < col; j++) {
  108. if (data[i][j] != m.data[i][j]) {
  109. return false;
  110. }
  111. }
  112. }
  113.  
  114. return true;
  115. }
  116.  
  117. @Override
  118. public String toString() {
  119. StringBuilder out = new StringBuilder();
  120. out.append("Matrix:n[ ");
  121. for (int i = 0; i < row; i++) {
  122. if (i != 0) {
  123. out.append("n");
  124. out.append(" ");
  125. }
  126. for (int j = 0; j < col; j++) {
  127. out.append(data[i][j]);
  128. if (j == col - 1) continue;
  129. for (int k = 0; k < getMaxLength() - getIntLength(data[i][j]) + 2; k++) {
  130. out.append(" ");
  131. }
  132. }
  133. }
  134. out.append(" ]");
  135. return out.toString();
  136. }
  137.  
  138. private int getMaxLength() {
  139. int max = Integer.MIN_VALUE;
  140. for (int i = 0; i < row; i++) {
  141. for (int j = 0; j < col; j++) {
  142. int k = data[i][j];
  143. if (k > max) {
  144. max = k;
  145. }
  146. }
  147. }
  148. return getIntLength(max);
  149. }
  150.  
  151. private int getIntLength(int i) {
  152. return String.valueOf(i).length();
  153. }
Add Comment
Please, Sign In to add comment