Public class Multi Array/This method prints out all the elements in a 2-dimensio
ID: 3785990 • Letter: P
Question
Public class Multi Array/This method prints out all the elements in a 2-dimensional array. *///have to use length. property public static void print 2D(int[] [] arr)/* This method adds 10 to every elements in a 2 dimensional array. *///change contented of arry//need to have a center This method prompts the user for the number of rows and c columns and the asks the user for each value to fill the array.*///needs a scanner, asks the user how many rows and columns there are public static int[] [] fill 2D() (])}Explanation / Answer
Here is the code for you:
import java.util.*;
public class MultiArray
{
/*This method prints out all the elements in a 2 dimensional array. */
// have to use length property
public static void print2D(int[][] arr)
{
for(int i = 0; i < arr.length; i++)
{
for(int j = 0; j < arr[0].length; j++)
System.out.print(arr[i][j] + " ");
System.out.println();
}
}
/*This method adds 10 to every element in a 2-dimensional array. */
// change contents of array.
// need to have a tester
public static void addTen(int[][] arr)
{
for(int i = 0; i < arr.length; i++)
for(int j = 0; j < arr[0].length; j++)
arr[i][j] += 10;
}
/*This method prompts the user for the number of rows and columns and then
asks the user for each value to fill the array. */
// needs a scanner, asks the user how many rows and columns there are.
public static int[][] fill2D()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int rows = sc.nextInt();
System.out.print("Enter the number of cols: ");
int cols = sc.nextInt();
int[][] arr = new int[rows][cols];
System.out.println("Enter the matrix of size " + rows + "x" + cols + ": ");
for(int i = 0; i < rows; i++)
for(int j = 0; j < cols; j++)
arr[i][j] = sc.nextInt();
return arr;
}
}
If you need any refinements, just get back to me.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.