-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathExercise08_28.java
71 lines (52 loc) · 1.59 KB
/
Exercise08_28.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package ch_08;
/*8.28 (Strictly identical arrays) The two-dimensional
* arrays m1 and m2 are strictly identical if their corresponding
* elements are equal. Write a method that returns true if m1 and m2
* are strictly identical, using the following header:
*
* public static boolean equals(int[][] m1, int[][] m2)
*
* Write a test program that prompts the user to enter
* two 3 � 3 arrays of integers and displays whether the
* two are strictly identical.
*/
import java.util.Scanner;
public class Exercise08_28 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int[][] m1 = new int[3][3];
System.out.print("Enter m1 (a 3 by 3 matrix) row by row: ");
for(int i = 0; i < m1.length; i++) {
for( int j = 0; j < m1[i].length; j++) {
m1[i][j] = input.nextInt();
}
}
int[][] m2 = new int[3][3];
System.out.print("Enter m2 (a 3 by 3 matrix) row by row: ");
for(int i = 0; i < m2.length; i++) {
for( int j = 0; j < m2[i].length; j++) {
m2[i][j] = input.nextInt();
}
}
if(equals(m1,m2)) {
System.out.println("The two arrays are strictly equal!");
}else
System.out.println("The two arrays are NOT strictly equal :(");
}
public static boolean equals(int[][] m1, int[][] m2) {
boolean isEqual = true;
for(int i = 0; i < m1.length; i++) {
if(m1[i][0] != m2[i][0]) {
isEqual = false;
break;
}else if(m1[i][1] != m2[i][1]) {
isEqual = false;
break;
}else if(m1[i][2] != m2[i][2]) {
isEqual = false;
break;
}
}
return isEqual;
}
}