-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHomeWork_4_5.java
More file actions
49 lines (42 loc) · 1.43 KB
/
HomeWork_4_5.java
File metadata and controls
49 lines (42 loc) · 1.43 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
package com.company;
/* 4.5
Create matrix with dimensions 10x15
initialize with random ints in range 10 - 99
print it
find and print the maximum value */
import java.util.Random;
public class HWork_4_5 {
public static void main(String[] args) {
int[][] matrix = new int[10][15];
initializeArrayWithRandomInts(matrix);
printArrayItems(matrix);
findAndPrintMaxNumInMatrix(matrix);
}
public static void initializeArrayWithRandomInts(int[][] arrayToInt) {
Random random = new Random();
for (int i = 0; i < arrayToInt.length; i++) {
for (int j = 0; j < arrayToInt[i].length; j++) {
arrayToInt[i][j] = random.nextInt(89) + 10;
}
}
}
public static void printArrayItems(int[][] arrayToPrint) {
for (int i = 0; i < arrayToPrint.length ; i++) {
for (int j = 0; j < arrayToPrint[i].length; j++) {
System.out.print(arrayToPrint[i][j] + " ");
}
System.out.println();
}
}
public static void findAndPrintMaxNumInMatrix(int[][] matrix) {
int maxNum = matrix[0][0];
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
if (maxNum < matrix[i][j]) {
maxNum = matrix[i][j];
}
}
}
System.out.println("Largest number is " + maxNum);
}
}