Question
Problem 1: 2D arrays then solve the following questions. Write the following Java methods and test it in the main() method. 1- Write a method
Problem 1:
2D arrays then solve the following questions. Write the following Java methods and test it in the main() method.
1- Write a method printArray() to display the elements of a two-dimensional array of integers. The array is received as a parameter. public static void printArray(int [][] arr) Create a 2D array in the main method, call it array1, that is 10 rows and 5 columns and print it.
-
2- Write a method fillArray() to initialize a two-dimensional array of integers to random values. The integer array is received as a parameter. The initialization values are generated randomly between a minimum and a maximum value. public static void fillArray(int [][] arr, int min, int max)
Fill the array created in 1 (array1) with values between 10 and 100
-
3- Write a method to determine if a two dimensional array is square or not. The method returns
true if the number of rows is equal to the number of columns of the 2D array
public static boolean isSquare(int [][] arr)
Test your method with the created array array1.
Create another array, call it array2, that has 5 rows and 5 columns, then fill it and test again.
-
4- Write a method to determine and display the sum of all the values in the primary diagonal of a 2D
integer array. The 2D integer array is received as a parameter to the method.
public static int sumOfPrimary(int [][] arr)
In the main method, get and display the sum of primary for array2.
-
5- Write a method to determine and display the sum of all the values in the secondary diagonal of a
2D integer array. The 2D integer array is received as a parameter to the method.
public static int sumOfSecondary(int [][] arr)
In the main method, get and display the sum of secondary for array2.
-
6- Write a method to find the sum of each row in a 2-dimensional integer array. The return value is
an array of integers.
public static int[] sumofRows (int [][] arr)
In the main method, get and display the sum of rows for both array1 and array2.
-
7- Write a method to find the sum of each column in a 2-dimensional array. The return value is an
array of integers.
public static int[] sumOfCol(int [][] arr)
In the main method, get and display the sum of columns for both array1 and array2.
8- Write a method to create a copy of an existing 2D array. The newly created array has the same elements and dimensions of the given array. public static int[][] createCopy(int[][] arr) In the main method, test your method by creating a new array that is similar to array2 and call it array3.
-
9- Write a method to determine if two 2D arrays are equal or not. The method returns true if the two arrays have the same dimensions and the same elements, and false if they have different dimensions or different elements. Use two nested loops and compare the elements one by one. public static boolean areEqual(int[][] arr1, int[][] arr2)
In the main method, test your method twice: once with array2 and array3, and once with array2
and array1.
-
10- Write a method to determine if a 2D array is an identity matrix or not. An identity matrix is a
square 2D array, where there are 1 in the primary diagonal and 0 everywhere else.
public static boolean isIdentity(int[][] arr)
Example of an identity matrix: 1000 0100 0010
0001 In the main method, create a new 2D array, call it array4, that has 4 rows and 4 columns, and fill it as an identity matrix. Test your method with array4 and array2.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started