Question 2 20 points Write a complete Java class that will c

Question 2 (20 points)

Write a complete Java class that will create a 2D array of randomly generated ints with dimensions of 5 rows and 10 columns. Print the values of each cell to a 5x10 table.
(Note: a complete Java class means the code should compile and run without issue. Be sure the output displays a set of random ints formatted in 5 rows by 10 columns.

Solution

Please follow the code and comments for description :

CODE :

import java.util.*;

public class rand2DArray {

public static void main(String[] args) {

//create the matrix with the predefine sizes of the rows and columns
final int rowSize = 5;
final int columnSize = 10;

Random rand = new Random(); // random method used to geenrate a random number randomly

int[][] matrixArray = new int[rowSize][columnSize]; // initialising the array with the defined sizes
//inserting the values to the array randomly
for (int[] rowMatrixArray : matrixArray) {
for (int col = 0; col < rowMatrixArray.length; col++) {
rowMatrixArray[col] = rand.nextInt(10);
}
}

//display the inserted random outputs
System.out.println(\"The Generated 5x10 2D Random Array is : \");

for (int[] colMatrixArray : matrixArray) {
for (int j = 0; j < colMatrixArray.length; j++) {
System.out.print(colMatrixArray[j] + \" \");
}
System.out.println();
}
}
}


OUTPUT :

case 1 :

The Generated 5x10 2D Random Array is :
8 7 0 7 6 0 3 5 1 1
2 8 2 3 0 0 4 6 4 0
7 8 9 9 0 0 3 5 1 1
6 3 1 8 6 9 3 1 3 7
8 5 6 9 3 3 2 6 8 8

case 2 :

The Generated 5x10 2D Random Array is :
3 4 8 5 9 4 9 3 8 2
0 6 8 9 3 3 4 4 7 8
8 1 5 2 7 9 4 4 3 4
5 4 3 2 0 8 5 1 9 1
4 2 0 8 4 8 2 8 3 1


Hope this is helpful.

Question 2 (20 points) Write a complete Java class that will create a 2D array of randomly generated ints with dimensions of 5 rows and 10 columns. Print the va
Question 2 (20 points) Write a complete Java class that will create a 2D array of randomly generated ints with dimensions of 5 rows and 10 columns. Print the va

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site