This repository was archived by the owner on Nov 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoDArray.java
More file actions
59 lines (50 loc) · 1.26 KB
/
TwoDArray.java
File metadata and controls
59 lines (50 loc) · 1.26 KB
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
class TwoDArray {
private int[][] arr2d;
public TwoDArray(int l, int w){
arr2d = new int[l][w];
}
public void randomize(int min, int max){
int range = max - min + 1;
for(int i = 0; i < arr2d.length; i++){
for(int j = 0; j < arr2d[i].length; j++){
arr2d[i][j] = (int)(Math.random() * range) + min;
}
}
}
public void display(){
for(int i = 0; i < arr2d.length; i++){
for(int j = 0; j < arr2d[i].length; j++){
System.out.print(arr2d[i][j] + "\t");
}
System.out.println();
}
}
public String sumEachRow(){
String sumRow = "The sum of each row is: \n";
for(int i = 0; i < arr2d.length; i++){
sumRow += sumRow(i) + "\n";
}
return sumRow;
}
public String sumEachCollumn(){
String sumCollumn = "The sum of each collumn is: \n";
for(int i = 0; i < arr2d[0].length; i++){
sumCollumn += sumCollumn(i) + "\t";
}
return sumCollumn;
}
private int sumRow(int row){
int sum = 0;
for(int i = 0; i < arr2d[row].length; i++){
sum += arr2d[row][i];
}
return sum;
}
private int sumCollumn(int collumn){
int sum = 0;
for(int i = 0; i < arr2d.length; i++){
sum += arr2d[i][collumn];
}
return sum;
}
}