Java Problem: Implementing a method to print a cell's neighbors on a 2D array. The method's header is provided. The main method (do not alter it)w ill read N numbers to create the 2D array and it will call the method for you. Do not go out of the bounds of the 2D array. I have attached what it should look like with the following input: 4 4 1 2 3 4 7 8 9 10 13 14 15 16 19 20 21 22 2 2   Code I have so far:  import java.util.Arrays; import java.util.Scanner; public class Problem3 { public static void printNeighbours(int[][] data, int row, int col){ } public static void main(String[] args) { Scanner kb = new Scanner(System.in); int[][] data = new int[kb.nextInt()][kb.nextInt()]; for (int i = 0; i < data.length; i++) { for (int j = 0; j < data[0].length; j++) { data[i][j] = kb.nextInt(); } } System.out.println("The 2D Array:"); for (int[] cols : data) { System.out.println(Arrays.toString(cols)); } int row = kb.nextInt(); int col = kb.nextInt(); System.out.printf("The centre point is: %d %d (row,col)\n\n", row,col);

Programming Logic & Design Comprehensive
9th Edition
ISBN:9781337669405
Author:FARRELL
Publisher:FARRELL
Chapter8: Advanced Data Handling Concepts
Section: Chapter Questions
Problem 14RQ
icon
Related questions
Question

Java

Problem: Implementing a method to print a cell's neighbors on a 2D array. The method's header is provided. The main method (do not alter it)w ill read N numbers to create the 2D array and it will call the method for you. Do not go out of the bounds of the 2D array.

I have attached what it should look like with the following input:

4 4

1 2 3 4 7 8 9 10 13 14 15 16 19 20 21 22

2 2

 

Code I have so far: 

import java.util.Arrays;
import java.util.Scanner;

public class Problem3 {
public static void printNeighbours(int[][] data, int row, int col){

}
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
int[][] data = new int[kb.nextInt()][kb.nextInt()];

for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[0].length; j++) {
data[i][j] = kb.nextInt();
}
}

System.out.println("The 2D Array:");
for (int[] cols : data) {
System.out.println(Arrays.toString(cols));
}

int row = kb.nextInt();
int col = kb.nextInt();
System.out.printf("The centre point is: %d %d (row,col)\n\n", row,col);

printNeighbours(data, row, col);
}
}

1 The 2D Array:
2 [1, 2, 3, 4]
3 [7, 8, 9, 10]
4 [13, 14, 15, 16]
5 [19, 20, 21, 22]
6 The centre point is: 2 2 (row,col)
C.
7
8 {1,1}: 8
9 {1,2}: 9
10 {1,3}: 10
11 {2,1}: 14
12 {2,3}: 16
13 {3,1}: 20
14 {3,2}: 21
15 {3,3}: 22
Transcribed Image Text:1 The 2D Array: 2 [1, 2, 3, 4] 3 [7, 8, 9, 10] 4 [13, 14, 15, 16] 5 [19, 20, 21, 22] 6 The centre point is: 2 2 (row,col) C. 7 8 {1,1}: 8 9 {1,2}: 9 10 {1,3}: 10 11 {2,1}: 14 12 {2,3}: 16 13 {3,1}: 20 14 {3,2}: 21 15 {3,3}: 22
Expert Solution
steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr