-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIT1A_Group13_Lab7.java
More file actions
351 lines (268 loc) · 11.7 KB
/
Copy pathIT1A_Group13_Lab7.java
File metadata and controls
351 lines (268 loc) · 11.7 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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/**
* A Java program that implements two-dimensional array and performscommon matrix manipulations like addition, subtraction, and
multiplication. If the sizes of arrays are equal, user will enter array
elements and the program would calculate a chosen arithmetic expression
for the elements.
* Group 13
* Authors: Lobingco, Kenneth F.
* Islanan, Lenito Laurencio Lamberto
* Laboratory #7
* Date: May 20, 2021
*/
import java.util.Scanner;
public class IT1A_Group13_Lab7
{
public static void main (String [] args) {
Scanner readInput = new Scanner (System.in);
int condition = 5;
char decide;
int [][] addRowArrayOne, addRowArrayTwo, subtractRowArrayOne, subtractRowArrayTwo, multiplyRowArrayOne, multiplyRowArrayTwo ; //arrays
int rowInput, columnInput, secondRowInput, secondColumnInput; //addition
int rowInputSubtract, columnInputSubtract, secondRowInputSubtract, secondColumnInputSubtract; //subtraction
int rowInputMultiply, columnInputMultiply, secondRowInputMultiply, secondColumnInputMultiply; //multiplication
do {
System.out.println(" Matrix Operations");
System.out.println(" Main Menu");
System.out.println("[1] Matrix Addition");
System.out.println("[2] Matrix Subtraction");
System.out.println("[3] Matrix Multiplication");
System.out.println("[4] Quit");
System.out.println("Enter your choice: ");
char whatNumber = readInput.next().charAt(0); //input chooser
if (whatNumber != '1' && whatNumber != '2' && whatNumber != '3' && whatNumber != '4') {
do {
System.out.println("Invalid input, please try again");
System.out.println(" Matrix Operations");
System.out.println(" Main Menu");
System.out.println("[1] Matrix Addition");
System.out.println("[2] Matrix Subtraction");
System.out.println("[3] Matrix Multiplication");
System.out.println("[4] Quit");
System.out.println("Enter your choice: ");
whatNumber = readInput.next().charAt(0);
} while (whatNumber != '1' && whatNumber != '2' && whatNumber != '3' && whatNumber != '4');
}
switch (whatNumber) {
case '1':
do{
do{
//Addition
//The two matrices should be same in size to perform addition
System.out.println("You chose addition");
System.out.println("[First size] ");
System.out.print("Enter the row: ");
rowInput = readInput.nextInt();
System.out.print("Enter the columns: ");
columnInput = readInput.nextInt();
addRowArrayOne = new int [rowInput][columnInput];
System.out.println("---------------------------");
System.out.println("[Second size] ");
System.out.print("Enter the row: ");
secondRowInput = readInput.nextInt();
System.out.print("Enter the columns: ");
secondColumnInput = readInput.nextInt();
addRowArrayTwo = new int [secondRowInput] [secondColumnInput];
if (rowInput != secondRowInput || columnInput != secondColumnInput) {
System.out.println("Unequal array size, please make sure that column\n and rows of the two arrays are matched. ");
}
} while (rowInput != secondRowInput || columnInput != secondColumnInput);
int r, c;
int sum = 0;
System.out.println("Enter elements: ");
for (r = 0; r < rowInput; r++) {
for (c = 0; c < columnInput; c++) {
addRowArrayOne [r] [c] = readInput.nextInt();
}
}
System.out.println("Enter second element: ");
for (int i = 0; i < rowInput; i++) {
for (int j = 0; j < columnInput; j++) {
addRowArrayTwo [i][j] = readInput.nextInt(); //Make sure i and j are corresponding with row and column
}
}
//Summation
int sumAddition [] [];
sumAddition = new int [rowInput] [columnInput];
System.out.println("Sum: \n");
for (int i = 0; i < secondRowInput; i++) {
for (int j = 0; j < secondColumnInput; j++) {
sumAddition [i][j] = addRowArrayTwo[i][j] + addRowArrayOne[i][j];
System.out.printf("%5d", sumAddition [i][j] );
}
System.out.println(" ");
}
System.out.println("Try again? (Y/N)");
decide = readInput.next().charAt(0);
if (decide == 'N' || decide == 'n')
{
break;
}
else
{
while ((decide != 'Y' && decide != 'y') && (decide != 'N' && decide != 'n'))
{
System.out.println("Invalid input, try again (Y/N)");
decide = readInput.next().charAt(0);
}
if (decide == 'N' || decide == 'n')
{
break;
}
}
} while (decide == 'Y' || decide == 'y');
break;
case '2':
do
{
do
{
//Subtraction
//Subtraction
//The two matrices should be same in size to perform subtraction
System.out.println("You chose subtraction");
System.out.println("First size ");
System.out.print("Enter the row: ");
rowInputSubtract = readInput.nextInt();
System.out.print("Enter the columns: ");
columnInputSubtract = readInput.nextInt();
subtractRowArrayOne = new int [rowInputSubtract][columnInputSubtract];
System.out.println("---------------------------");
System.out.println("Second size ");
System.out.print("Enter the row: ");
secondRowInputSubtract = readInput.nextInt();
System.out.print("Enter the columns: ");
secondColumnInputSubtract = readInput.nextInt();
subtractRowArrayTwo = new int [secondRowInputSubtract] [secondColumnInputSubtract];
if (rowInputSubtract != secondRowInputSubtract || columnInputSubtract != secondColumnInputSubtract)
{
System.out.println("Unequal array size, please make sure that column\n and rows of the two arrays are matched. ");
}
}while (rowInputSubtract != secondRowInputSubtract || columnInputSubtract != secondColumnInputSubtract);
int rMinus, cMinus;
int minus = 0;
System.out.println("Enter elements: ");
for (rMinus = 0; rMinus < rowInputSubtract; rMinus++)
{
for (cMinus = 0; cMinus < columnInputSubtract; cMinus++)
{
subtractRowArrayOne [rMinus] [cMinus] = readInput.nextInt();
}
}
System.out.println("Enter second element: ");
for (int iMinus = 0; iMinus < rowInputSubtract; iMinus++)
{
for (int jMinus = 0; jMinus < columnInputSubtract; jMinus++)
{
subtractRowArrayTwo [iMinus][jMinus] = readInput.nextInt();
}
}
//Summation
int minusSubtraction [] [];
minusSubtraction = new int [rowInputSubtract] [columnInputSubtract];
System.out.println("Difference: ");
for (int i = 0; i < secondRowInputSubtract; i++)
{
for (int j = 0; j < secondColumnInputSubtract; j++)
{
minusSubtraction [i][j] = subtractRowArrayTwo[i][j] - subtractRowArrayOne[i][j];
System.out.println(minusSubtraction[i][j] + "\n");
}
}
System.out.println("Try again? (Y/N)");
decide = readInput.next().charAt(0);
if (decide == 'N' || decide == 'n')
{
break;
}
else
{
while ((decide != 'Y' && decide != 'y') && (decide != 'N' && decide != 'n'))
{
System.out.println("Invalid input, try again (Y/N)");
decide = readInput.next().charAt(0);
}
if (decide == 'N' || decide == 'n') {
break;
}
}
} while (decide == 'Y' || decide == 'y');
break;
case '3':
do
{
do
{
//Multiplication
//The two matrices should be same in size to perform addition
System.out.println("You chose multiplication");
System.out.println("First size ");
System.out.print("Enter the row: ");
rowInputMultiply = readInput.nextInt();
System.out.print("Enter the columns: ");
columnInputMultiply = readInput.nextInt();
multiplyRowArrayOne = new int [rowInputMultiply][columnInputMultiply];
System.out.println("---------------------------");
System.out.println("Second size ");
System.out.print("Enter the row: ");
secondRowInputMultiply = readInput.nextInt();
System.out.print("Enter the columns: ");
secondColumnInputMultiply = readInput.nextInt();
multiplyRowArrayTwo = new int [secondRowInputMultiply] [secondColumnInputMultiply];
if (rowInputMultiply != secondRowInputMultiply || columnInputMultiply != secondColumnInputMultiply)
{
System.out.println("Unequal array size, please make sure that column\n and rows of the two arrays are matched. ");
}
} while (rowInputMultiply != secondRowInputMultiply || columnInputMultiply != secondColumnInputMultiply);
int rTimes, cTimes;
int times = 0;
System.out.println("Enter elements: ");
for (rTimes = 0; rTimes < rowInputMultiply; rTimes++)
{
for (cTimes = 0; cTimes < columnInputMultiply; cTimes++)
{
multiplyRowArrayOne [rTimes] [cTimes] = readInput.nextInt();
}
}
System.out.println("Enter second element: ");
for (int iTimes = 0; iTimes < rowInputMultiply; iTimes++)
{
for (int jTimes = 0; jTimes < columnInputMultiply; jTimes++)
{
multiplyRowArrayTwo [iTimes][jTimes] = readInput.nextInt();
}
}
//Summation
int timesMultiplication [] [];
timesMultiplication = new int [rowInputMultiply] [columnInputMultiply];
System.out.println("Product: ");
for (int i = 0; i < secondRowInputMultiply; i++)
{
for (int j = 0; j < secondColumnInputMultiply; j++)
{
timesMultiplication [i][j] = multiplyRowArrayTwo[i][j] * multiplyRowArrayOne[i][j];
System.out.println(timesMultiplication[i][j] + "\n");
}
}
System.out.println("Try again? (Y/N)");
decide = readInput.next().charAt(0);
if (decide == 'N' || decide == 'n') {
break;
}
else {
while ((decide != 'Y' && decide != 'y') && (decide != 'N' && decide != 'n'))
{
System.out.println("Invalid input, try again (Y/N)");
decide = readInput.next().charAt(0);
}
if (decide == 'N' || decide == 'n') {
break;
}
}
} while (decide == 'Y' || decide == 'y');
break;
case '4': System.exit(0);
break;
}
} while (condition == 5);
}
}