Advertisement
porteno

Untitled

Nov 3rd, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. //q1.a answer - class header and attributes
  2. public class Matrix {
  3. private int[][] mtrx;
  4. private int n1;
  5. private int n2;
  6.  
  7.  
  8. /*
  9. * q1.a.1 answer - inner method
  10. * Checks if two Matrix objects are the same
  11. */
  12. public boolean isSame(Matrix obj) {
  13. //condition to checks the equality of the numbers and the length of the 2d-array lengths
  14. if(this.n1 != obj.getN1() || this.n2 != obj.getN2() ||
  15. this.mtrx.length != obj.getMtrx().length || this.mtrx[0].length != obj.getMtrx()[0].length)
  16. return false;
  17. //loop to checks the values inside the 2d-arrays
  18. for (int i = 0; i < this.mtrx.length; i++) {
  19. for (int j = 0; j < this.mtrx[i].length; j++) {
  20. if(this.mtrx[i][j] != obj.getMtrx()[i][j])
  21. return false;
  22. }
  23. }
  24. return true;
  25. } //end of method
  26.  
  27.  
  28. /*
  29. * q1.a.2 answer - returns the numbers of the rows that n1 and n2 appears in them
  30. */
  31. public int appearsInside() {
  32. boolean found_n1, found_n2;
  33. int count_rows=0;
  34.  
  35. for (int i = 0; i < this.mtrx.length; i++) {
  36. found_n1 = false;
  37. found_n2 = false;
  38. for (int j = 0; j < this.mtrx[i].length; j++) {
  39. if(this.mtrx[i][j] == this.n1)
  40. found_n1 = true;
  41. if(this.mtrx[i][j] == this.n2)
  42. found_n2 = true;
  43. }
  44. if(found_n1 && found_n2)
  45. count_rows++;
  46. }
  47. return count_rows;
  48. } //end of method
  49.  
  50. /*
  51. * q1.a.3 answer - receives k
  52. * looks for inner 2d-array that keeps the question criteria
  53. */
  54. /*
  55. * int[][] m1_nums = { {1,2,2,3},
  56. {5,4,3,0},
  57. {3,0,1,7},
  58. {7,7,1,3}};
  59. */
  60. public boolean look_4_KxK(int k) {
  61. for (int i = 0; i < this.mtrx.length-k ; i++) {
  62. for (int j = 0; j < this.mtrx[i].length-k; j++) {
  63. if( (this.mtrx[i][j] == this.n1) && (this.mtrx[i+k][j+k] == this.n2))
  64. return true;
  65. }
  66. }
  67. return false;
  68.  
  69. }
  70.  
  71. //constructor and getters/setters methods - added for compiling purpose only
  72. public Matrix(int[][] m, int n1, int n2) {
  73. this.mtrx = m;
  74. this.n1 = n1;
  75. this.n2 = n2;
  76. }
  77.  
  78. public int[][] getMtrx() {
  79. return mtrx;
  80. }
  81.  
  82.  
  83. public void setMtrx(int[][] mtrx) {
  84. this.mtrx = mtrx;
  85. }
  86.  
  87.  
  88.  
  89. public int getN1() {
  90. return n1;
  91. }
  92.  
  93.  
  94.  
  95. public void setN1(int n1) {
  96. this.n1 = n1;
  97. }
  98.  
  99.  
  100.  
  101. public int getN2() {
  102. return n2;
  103. }
  104.  
  105.  
  106.  
  107. public void setN2(int n2) {
  108. this.n2 = n2;
  109. }
  110.  
  111.  
  112.  
  113.  
  114.  
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement