0% found this document useful (0 votes)
14 views

Meeting2-JavaCode-Example of 2D Array With Method

Uploaded by

leenalkasem2001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Meeting2-JavaCode-Example of 2D Array With Method

Uploaded by

leenalkasem2001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Write a java program to do:

1-Void Method to read 2D array of 22 numbers


2- Void Method to print array elements
3-Method to return average of the second column

1 2
3 4

import java.util.*;
public class Main
{
public static void main(String[] args) {
int[][] arrayMain = new int[2][2];
enterElements (arrayMain);
printArray(arrayMain);
System.out.println(getAverage(arrayMain));
}

public static void enterElements (int[][] arrayMain){


Scanner scanner = new Scanner(System.in);
System.out.println("please enter the elements of the array");
for (int i = 0; i < arrayMain.length; i++) {
for (int j = 0; j < arrayMain[i].length; j++) {
arrayMain[i][j] = scanner.nextInt();
}
}
}//end method

public static void printArray (int[][] arrayMain){


for (int i = 0; i < arrayMain.length; i++) {
for (int j = 0; j < arrayMain[i].length; j++) {
System.out.println("arr[" + i + "][" + j + "] = "+arrayMain[i][j] );
}
}
}//end method
public static double getAverage(int[][] arrayMain)
{
double total = 0;
int value = 0;
for (int row = 0; row < arrayMain.length; row++)
{
for (int col = 1; col <= 1; col++)
{
value = arrayMain[row][col];
total = total + value;
}
}
return total / arrayMain.length;
}

You might also like