Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //q1.a answer - class header and attributes
- public class Matrix {
- private int[][] mtrx;
- private int n1;
- private int n2;
- /*
- * q1.a.1 answer - inner method
- * Checks if two Matrix objects are the same
- */
- public boolean isSame(Matrix obj) {
- //condition to checks the equality of the numbers and the length of the 2d-array lengths
- if(this.n1 != obj.getN1() || this.n2 != obj.getN2() ||
- this.mtrx.length != obj.getMtrx().length || this.mtrx[0].length != obj.getMtrx()[0].length)
- return false;
- //loop to checks the values inside the 2d-arrays
- for (int i = 0; i < this.mtrx.length; i++) {
- for (int j = 0; j < this.mtrx[i].length; j++) {
- if(this.mtrx[i][j] != obj.getMtrx()[i][j])
- return false;
- }
- }
- return true;
- } //end of method
- /*
- * q1.a.2 answer - returns the numbers of the rows that n1 and n2 appears in them
- */
- public int appearsInside() {
- boolean found_n1, found_n2;
- int count_rows=0;
- for (int i = 0; i < this.mtrx.length; i++) {
- found_n1 = false;
- found_n2 = false;
- for (int j = 0; j < this.mtrx[i].length; j++) {
- if(this.mtrx[i][j] == this.n1)
- found_n1 = true;
- if(this.mtrx[i][j] == this.n2)
- found_n2 = true;
- }
- if(found_n1 && found_n2)
- count_rows++;
- }
- return count_rows;
- } //end of method
- /*
- * q1.a.3 answer - receives k
- * looks for inner 2d-array that keeps the question criteria
- */
- /*
- * int[][] m1_nums = { {1,2,2,3},
- {5,4,3,0},
- {3,0,1,7},
- {7,7,1,3}};
- */
- public boolean look_4_KxK(int k) {
- for (int i = 0; i < this.mtrx.length-k ; i++) {
- for (int j = 0; j < this.mtrx[i].length-k; j++) {
- if( (this.mtrx[i][j] == this.n1) && (this.mtrx[i+k][j+k] == this.n2))
- return true;
- }
- }
- return false;
- }
- //constructor and getters/setters methods - added for compiling purpose only
- public Matrix(int[][] m, int n1, int n2) {
- this.mtrx = m;
- this.n1 = n1;
- this.n2 = n2;
- }
- public int[][] getMtrx() {
- return mtrx;
- }
- public void setMtrx(int[][] mtrx) {
- this.mtrx = mtrx;
- }
- public int getN1() {
- return n1;
- }
- public void setN1(int n1) {
- this.n1 = n1;
- }
- public int getN2() {
- return n2;
- }
- public void setN2(int n2) {
- this.n2 = n2;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement