This is for my homework: how to create a public cage method (char [] [] arr) that returns char [] []. The method should place Xs along the boundaries of the grid represented by a two-dimensional array. In addition, it should place the “columns” along the columns of the array, skipping one column for each bar. For example, if arr has 8 columns, the returned array looks like this:
XXXXXXX XXXX XXXX XXXXXXX
my other form was this: Create an ArrayArt Java class with static methods as follows: a public method called frame (char [] [] arr) that returns char [] []. The method should put Xs along the boundaries of the grid represented by the two-dimensional array, and then should return this array. For example, if arr has 4 columns and 4 rows, the resulting array should be:
----jGRASP exec: java ArrayArt XXXX XX XX XXXX ----jGRASP: operation complete.
The source code for printing the frame is as follows:
public class ArrayArt{ public static void main(String[] args){ printArray(frame(4,4)); } // frame printing public static char[][] frame(int n, int m ){ char[][] x=new char[n][m]; for(int row=0;row<x.length;row++) for(int col=0;col<x[row].length;col++) if( row == 0 || row == n-1 || row == col+row || row == (row+col)-(m-1) ) x[row][col]= 'X'; else x[row][col]= ' '; return x; } //printArray public static void printArray(char[][] arr){ for(int row=0;row<arr.length;row++){ for (int col=0;col<arr[row].length;col++) System.out.print(" "+arr[row][col]); System.out.println(); } } }
source share