-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwoDTester_1Perrott.java
164 lines (142 loc) · 4.23 KB
/
TwoDTester_1Perrott.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Name: Samuel Perrott
// Assignment: Pics-A4: 2D Array lab
public class TwoDTester_1Perrott {
public static void main(String[] args) {
TwoDTable t1 = new MultiplicationTable(7,6);
TwoDTable t2 = new Checkerboard(6,5);
TwoDTable t3 = new RandomTable(4,4,9);
TwoDTable t4 = new TwoDTable(5,5);
t1.print();
System.out.println();
System.out.println("Sum of multiplication table is " + sum(t1.getRawData()) + ".\n");
System.out.println("Max of multiplication table is " + max(t1.getRawData()) + ".\n");
t2.print();
System.out.println();
t3.print();
System.out.println();
setAll(t4.getRawData(), 5);
System.out.println("A table of all fives:");
t4.print();
}
public static int sum(int[][] arr) {
int total = 0;
for (int[] row : arr) {
for (int i : row) {
total += i;
}
}
return total;
// iterate through whole arr and calc sum
}
public static void setAll(int[][] arr, int val) {
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
arr[i][j] = val;
}
}
// sets all arr vals to val
}
public static int max(int[][] arr) {
int largest = arr[0][0];
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
if (arr[i][j] > largest) largest = arr[i][j];
}
}
return largest;
//return largets val in arr
}
}
class TwoDTable {
final int ROWS;
final int COLS;
private int[][] array;
public TwoDTable(int rows, int cols) {
ROWS = rows;
COLS = cols;
array = new int[ROWS][COLS];
}
public void print() {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
System.out.printf("%4d", array[i][j]);
}
System.out.println();
}
}
public int[][] getRawData() {
return array;
}
}
class MultiplicationTable extends TwoDTable {
public MultiplicationTable(int x, int y) {
super(x,y);
int[][] array = getRawData();
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
array[i][j] = i * j;
}
}
}
@Override
public void print() {
System.out.println("MULTIPLICATION TABLE");
System.out.printf("%4s", " ");
for (int i = 0; i < COLS; i++) {
System.out.printf("%4d", i);
}
System.out.println();
for (int i = 0; i < ROWS; i++) {
System.out.printf("%4d", i);
for (int j = 0; j < COLS; j++) {
System.out.printf("%4d", getRawData()[i][j]);
}
System.out.println();
}
}
}
class Checkerboard extends TwoDTable {
public Checkerboard(int x, int y) {
super(x, y);
}
@Override
public void print() {
System.out.println("CHECKERBOARD");
boolean zero = true;
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (zero) System.out.printf("%4d", 0);
else System.out.printf("%4d", 1);
zero = !zero;
}
if (COLS%2 == 0) zero = !zero;
System.out.println();
}
}
}
class RandomTable extends TwoDTable {
public RandomTable(int rows, int cols, int max) {
super(rows, cols);
int[][] arr = getRawData();
for (int i = 0; i < arr.length; i++)
for (int j = 0; j < arr[i].length; j++) {
arr[i][j] = (int)(Math.random()*(max + 1));
}
}
@Override
public void print() {
System.out.println("RANDOM TABLE");
System.out.printf("%4s", " ");
for (int i = 0; i < COLS; i++) {
System.out.printf("%4d", i);
}
System.out.println();
for (int i = 0; i < ROWS; i++) {
System.out.printf("%4d", i);
for (int j = 0; j < COLS; j++) {
System.out.printf("%4d", getRawData()[i][j]);
}
System.out.println();
}
}
}