In: Computer Science
JAVA Create a support class named RowSumThread.java from which you can instantiate Runnable objects, each of which will sum one row of the two-dimensional array and then place the sum of that row into the appropriate slot of a one-dimensional, 20-element array. For your applicaiton, create MaxRowSum.java that instantiates a 20x20 two-dimentional array of integers, populates it with random integers drawn from the range 1 to 100, and then output the index of the row with the highest sum along all the rows.
import java.util.*;
// Creates a class RowSumThread by implementing Runnable interface
class RowSumThread implements Runnable
{
// Declares a 2D matrix
public int matrix[][];
// Declares a array to store each row sum
int rowSum[];
// Random class object declared
Random rm;
// To store number of rows and columns
int row, col;
// Default constructor to create matrix, array and Random class object
public RowSumThread(int r, int c)
{
row = r;
col = c;
matrix = new int[r][c];
rowSum = new int[r];
rm = new Random();
}// End of constructor
// Method to create 2D matrix
void createMatrix()
{
// Loops till number of rows
for(int r = 0; r < row; r++)
{
// Loops till number of columns
for(int c = 0; c < col; c++)
{
// Generates random number between 1 and 100
// Stores the random number at r for row and c for column index position
matrix[r][c] = 1 + rm.nextInt(100);
}// End of for loop for column
}// End of for loop for row
}// End of method
// Method to calculate each row sum
void eachRowSum()
{
// Calls the method to store random numbers in matrix
createMatrix();
System.out.print("\t\t\tOriginal Matrix \n");
// Calls the method to display matrix
displayMatrix();
// Loops till number of rows
for(int r = 0; r < row; r++)
{
// Loops till number of columns
for(int c = 0; c < col; c++)
{
// Calculates each row sum
rowSum[r] += matrix[r][c];
}// End of for loop for column
}// End of for loop for row
// Calls the method to find highest row sum
highestRowSum();
}// End of method
// Method to display matrix
void displayMatrix()
{
// Loops till number of rows
for(int r = 0; r < row; r++)
{
// Loops till number of columns
for(int c = 0; c < col; c++)
{
System.out.print(matrix[r][c] + " ");
}// End of for loop for column
// Displays new line
System.out.println();
}// End of for loop for row
}// End of method
// Method to find out the row number contains maximum sum
void highestRowSum()
{
// Initializes highest sum to rowSum zero index position
int highest = rowSum[0];
// Initializes position to zero
int pos = 0;
// Loops till number or rows
// Starts from one because zero index position is already considered above
for(int r = 1; r < row; r++)
{
// Checks if current rowSum index position value is greater than the earlier highest
if(rowSum[r] > highest)
{
// Update the highest by current rowSum index position value
highest = rowSum[r];
// Update the position by current loop value
pos = r;
}// End of if condition
}// End of for loop
// Displays highest sum with row index position
System.out.print("\n Highest row sum " + highest + " found for row " + pos);
}// End of method
// Overrides Runnable interface run() method
public void run()
{
// Displays current thread name
System.out.println(" Starting Thread..." + Thread.currentThread().getName());
// Calls the method to calculate each row sum
eachRowSum();
} // End of method
}// End of class
// Driver class MaxRowSum definition
public class MaxRowSum
{
// main method definition
public static void main(String a[])
{
// Displays current thread name
System.out.println(" Starting Thread..." + Thread.currentThread().getName());
// Creates an object of the class RowSumThread using parameterized constructor
RowSumThread rt = new RowSumThread(20, 20);
// Creates a thread
Thread t = new Thread(rt);
// Sets the name of the thread
t.setName("Row Sum");
// Starts the thread
t.start();
}// End of main method
}// End of driver class
Sample Output:
Starting Thread...main
Starting Thread...Row Sum
Original Matrix
86 33 33 19 12 85 25 7 48 80 93 89 93 35 33 81 54 34 10 46
35 13 85 33 63 66 87 62 40 88 25 23 87 10 25 77 55 55 41 29
89 42 63 5 70 46 66 39 89 84 89 67 35 54 18 70 73 24 27 13
57 98 17 65 66 48 61 61 2 95 32 31 73 97 41 28 20 72 83 26
52 80 6 43 62 58 32 56 20 74 1 93 22 57 38 9 2 48 28 67
61 60 97 35 36 95 77 54 72 37 22 84 82 16 52 28 70 82 37 15
7 8 30 15 5 85 41 56 55 25 47 4 52 100 83 74 25 11 28 97
24 21 29 91 100 91 29 78 65 54 18 92 43 91 22 87 27 65 64 60
26 83 65 55 87 13 89 45 64 29 49 78 97 23 79 10 17 66 70 60
29 42 60 17 55 46 60 48 4 98 37 46 94 23 47 61 2 76 76 22
90 18 98 72 66 96 31 49 5 91 6 19 91 21 74 83 19 37 41 98
36 100 75 70 92 68 17 50 60 53 25 18 30 36 92 88 9 56 11 63
85 24 11 90 23 60 40 31 80 24 13 70 58 83 18 12 11 10 74 69
91 72 25 38 64 13 2 13 45 3 23 66 88 29 54 34 17 11 18 75
29 40 11 20 96 91 7 69 23 81 95 98 37 88 30 60 69 45 77 85
34 28 73 89 67 56 42 50 49 30 92 25 3 28 52 19 36 23 92 31
93 50 48 59 64 6 98 6 56 98 17 88 71 52 3 84 97 23 4 83
19 37 7 19 25 92 60 32 24 56 47 91 16 80 69 65 54 94 72 54
34 10 54 31 81 51 59 7 22 1 60 33 79 59 77 59 43 69 2 18
98 10 59 6 33 23 37 74 64 12 80 42 62 89 63 21 69 41 31 58
Highest row sum 1151 found for row 7