Skip to content
This repository was archived by the owner on Sep 22, 2024. It is now read-only.

Commit 208a959

Browse files
committed
Finish Matrix.java
1 parent 53951b5 commit 208a959

6 files changed

+64
-237
lines changed

ArrayUtil.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
public class ArrayUtil {
2+
public static void reverseArray(int[] arr) {
3+
int start = 0;
4+
int end = arr.length - 1;
5+
while (start < end) {
6+
int temp = arr[start];
7+
arr[start] = arr[end];
8+
arr[end] = temp;
9+
start++;
10+
end--;
11+
}
12+
}
13+
}

FourDigitInteger.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import java.util.ArrayList;
21
import java.util.Scanner;
32

43
public class FourDigitInteger {

Matrix.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
public class Matrix {
2+
private int[][] mat;
3+
4+
public Matrix (int[][] m) {
5+
mat = m;
6+
}
7+
8+
public void reverseAllRows() {
9+
int i;
10+
for (i = 0; i < mat.length; i++) {
11+
int[] arr = mat[i];
12+
ArrayUtil.reverseArray(arr);
13+
mat[i] = arr;
14+
}
15+
}
16+
17+
public void reverseAllColumns() {
18+
int[][] newMatrix = new int[mat.length][mat[0].length];
19+
for (int i = 0; i < mat.length; i++) {
20+
for (int j = 0; j < mat[i].length; j++) {
21+
newMatrix[mat.length - i - 1][j] = mat[i][j];
22+
}
23+
}
24+
mat = newMatrix;
25+
}
26+
27+
public void reverseMatrix() {
28+
reverseAllRows();
29+
reverseAllColumns();
30+
}
31+
32+
public static void main(String[] args) {
33+
int[][] mat = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
34+
int[][] desiredMat = {{4, 3, 2, 1}, {8, 7, 6, 5}, {12, 11, 10, 9}};
35+
Matrix mat1 = new Matrix(mat);
36+
mat1.reverseAllRows();
37+
System.out.println(mat1.mat[0][1] == desiredMat[0][1]);
38+
System.out.println(mat1.mat[2][1] == desiredMat[2][1]);
39+
System.out.println(mat1.mat[1][3] == desiredMat[1][3]);
40+
int[][] anotherMat = {{1, 2}, {3, 4}, {5, 6}};
41+
int[][] anotherDesiredMat = {{6, 5}, {4, 3}, {2, 1}};
42+
Matrix mat2 = new Matrix(anotherMat);
43+
mat2.reverseMatrix();
44+
System.out.println(mat2.mat[0][0] == anotherDesiredMat[0][0]);
45+
System.out.println(mat2.mat[0][1] == anotherDesiredMat[0][1]);
46+
System.out.println(mat2.mat[1][0] == anotherDesiredMat[1][0]);
47+
System.out.println(mat2.mat[1][1] == anotherDesiredMat[1][1]);
48+
System.out.println(mat2.mat[2][0] == anotherDesiredMat[2][0]);
49+
System.out.println(mat2.mat[2][1] == anotherDesiredMat[2][1]);
50+
}
51+
}

Poll.java

Lines changed: 0 additions & 30 deletions
This file was deleted.

PollControlPanel.java

Lines changed: 0 additions & 56 deletions
This file was deleted.

PollDisplayPanel.java

Lines changed: 0 additions & 150 deletions
This file was deleted.

0 commit comments

Comments
 (0)