Question

In: Computer Science

JAVA Create a support class named RowSumThread.java from which you can instantiate Runnable objects, each of...

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.

Solutions

Expert Solution

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


Related Solutions

In java: -Create a class named Animal
In java: -Create a class named Animal
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class,...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class, you are to have the following data members: name: String (5 pts) id: String   (5 pts) hours: int   (5 pts) rate: double (5 pts) private members (5 pts) You are to create no-arg and parameterized constructors and the appropriate setters(accessors) and getters (mutators). (20 pts) The class definition should also handle the following exceptions: An employee name should not be empty, otherwise an exception...
java Objective: Create a class. Create objects. Use methods of a class. Create a class BankAccount...
java Objective: Create a class. Create objects. Use methods of a class. Create a class BankAccount to represent a bank account according to the following requirements: A bank account has three attributes: accountnumber, balance and customer name. Add a constructor without parameters. In the initialization of the attributes, set the number and the balance to zero and the customer name to an empty string. Add a constructor with three parameters to initialize all the attributes by specific values. Add a...
- Create a java class named SaveFile in which write the following: Constructor: The class's constructor...
- Create a java class named SaveFile in which write the following: Constructor: The class's constructor should take the name of a file as an argument A method save (String line): This method should open the file defined by the constructor, save the string value of line at the end of the file, and then close the file. - In the same package create a new Java class and it DisplayFile in which write the following: Constructor: The class's constructor...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class named Book. In the Book class: Add the following private instance variables: title (String) author (String) rating (int) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. Add a second constructor that receives only 2 String parameters, inTitle and inAuthor. This constructor should only assign input parameter values to title and...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class named Employee. In the Employee class: Add the following private instance variables: name (String) job (String) salary (double) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. (Refer to the Tutorial3 program constructor if needed to remember how to do this.) Add a public String method named getName (no parameter) that...
Create a Java project called 5 and a class named 5 Create a second new class...
Create a Java project called 5 and a class named 5 Create a second new class named CoinFlipper Add 2 int instance variables named headsCount and tailsCount Add a constructor with no parameters that sets both instance variables to 0; Add a public int method named flipCoin (no parameters). It should generate a random number between 0 & 1 and return that number. (Important note: put the Random randomNumbers = new Random(); statement before all the methods, just under the...
Create a new Java project called lab1 and a class named Lab1 Create a second class...
Create a new Java project called lab1 and a class named Lab1 Create a second class called VolumeCalculator. Add a static field named PI which = 1415 Add the following static methods: double static method named sphere that receives 1 double parameter (radius) and returns the volume of a sphere. double static method named cylinder that receives 2 double parameters (radius & height) and returns the volume of a cylinder. double static method named cube that receives 1 double parameter...
Java program Create a public method named saveData for a class named Signal that will hold...
Java program Create a public method named saveData for a class named Signal that will hold digitized acceleration data. Signal has the following field declarations private     double timeStep;               // time between each data point     int numberOfPoints;          // number of data samples in array     double [] acceleration = new double [1000];          // acceleration data     double [] velocity = new double [1000];        // calculated velocity data     double [] displacement = new double [1000];        // calculated disp...
In java Create a class named CellPhoneCase that extends your Case class – it inherits all...
In java Create a class named CellPhoneCase that extends your Case class – it inherits all the fields and methods from the Case class. It should also contain:  One field for a CellPhone object. Be sure to put in the appropriate access modifier. (private, public)  A constructor with 3 parameters – owner’s name, Case color, the phone number of the cell phone that will be in the Case. This constructor MUST call the super class’s constructor. Then set...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT