Write the statements required to create and initialize the 2D array: Write the s
ID: 3573141 • Letter: W
Question
Write the statements required to create and initialize the 2D array: Write the statements required to swap the top-right element with the bottom-left element of the 2D array above. You may hard-code the indexes here. Write a method that takes a 2D array of integers as an argument The array returns a new 2D array of integers of the same size (same number of rows and columns). The values in the returned array are the same as those in the argument array EXCEPT that any negative values are made positive. The header for the method is given below: public static int [][] absoluteArray2D(int [][] in) {Explanation / Answer
TwoDimensionalArray.java
public class TwoDimensionalArray {
public static void main(String[] args) {
//Creating an integer type 2_D array and initialized with values
int arr[][]={{1,5,7,9},{2,4,6,8}};
//Displaying the elements inside the 2-D array
System.out.println("Displaying 2-Dimensional Array ::");
for(int i=0;i<arr.length;i++)
{
for(int j=0;j<arr[0].length;j++)
{
//Displaying the elements
System.out.print(arr[i][j]+" ");
}
System.out.println(" ");
}
int temp;
//Performing Swapping operation
temp=arr[0][3];
arr[0][3]=arr[1][0];
arr[1][0]=temp;
//Displaying the elements inside the 2-D array
System.out.println("Displaying 2-Dimensional Array After Swapping::");
for(int i=0;i<arr.length;i++)
{
for(int j=0;j<arr[0].length;j++)
{
//Displaying the elements
System.out.print(arr[i][j]+" ");
}
System.out.println(" ");
}
}
}
_____________________
Output:
Displaying 2-Diemensional Array ::
1 5 7 9
2 4 6 8
Displaying 2-Diemensional Array After Swapping::
1 5 7 2
9 4 6 8
_______________________
25)
TwoDArrayAbsolute.java
import java.util.Scanner;
public class TwoDArrayAbsolute {
public static void main(String[] args) {
//Creating an integer array
int arr[][]=new int[4][4];
//Scanner class object is sued to read the inputs entered by the user
Scanner sc=new Scanner(System.in);
/* This nested for loop will get the values entered
* by the suer and populate them into an array
*/
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
//Getting the values entered by the user
System.out.print("Enter the element ["+i+","+j+"] :");
arr[i][j]=sc.nextInt();
}
}
//Displaying the elements inside the array
System.out.println("Displaying the elements inside the array :");
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
System.out.printf(" %d ",arr[i][j]);
}
System.out.println(" ");
}
//Calling the method by passing the array as argument
arr=absoluteArray2D(arr);
//Displaying the 2-d array after making all integers as positive
System.out.println("Displaying the elements inside the array :");
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println(" ");
}
}
private static int[][] absoluteArray2D(int[][] arr) {
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
if(arr[i][j]<0)
arr[i][j]=-(arr[i][j]);
}
}
return arr;
}
}
__________________________
output:
Enter the element [0,0] :-2
Enter the element [0,1] :3
Enter the element [0,2] :4
Enter the element [0,3] :-5
Enter the element [1,0] :-6
Enter the element [1,1] :-7
Enter the element [1,2] :5
Enter the element [1,3] :4
Enter the element [2,0] :-3
Enter the element [2,1] :-5
Enter the element [2,2] :-6
Enter the element [2,3] :-7
Enter the element [3,0] :6
Enter the element [3,1] :5
Enter the element [3,2] :4
Enter the element [3,3] :-3
Displaying the elements inside the array :
-2 3 4 -5
-6 -7 5 4
-3 -5 -6 -7
6 5 4 -3
Displaying the elements inside the array :
2 3 4 5
6 7 5 4
3 5 6 7
6 5 4 3
__________Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.