This is for my homework: How to create a public method called cage(char[][] arr) that returns a char[][]. The method should place Xs along the borders of the grid represented by the 2D array. In addition, it should place "bars" along the columns of the array, skipping one column for every bar. For example, if arr has 8 columns, the returning array looks like this:
X X X X X X X
X X X X
X X X X
X X X X X X X
my other shape was this: Create a java class ArrayArt with static methods as specified below: a public method called frame(char[][] arr) that returns a char[][]. The method should put Xs along the borders of the grid represented by the 2D array and then it should return that array. For example, if arr has 4 columns and 4 rows, the resulting array should be:
----jGRASP exec: java ArrayArt
X X X X
X X
X X
X X X X
----jGRASP: operation complete.
The source code for the frame printing is next:
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();
}
}
}
Note: To create a static member(block, variable, method, nested class), precede its declaration with the keyword static. When a member is declared static, it can be accessed before any objects of its class are created, and without reference to any object.
A static method can be called directly from the class, without having to create an instance of the class. A static method can only access static variables; it cannot access instance variables. Since the static method refers to the class, the syntax to call or refer to a static method is: class name. method name.
We can declare a class static by using the static keyword. A class can be declared static only if it is a nested class. It does not require any reference of the outer class. The property of the static class is that it does not allows us to access the non-static members of the outer class.
Call a static Method in Another Class in Java In the case of a static method, we don't need to create an object to call the method. We can call the static method by using the class name as we did in this example to call the getName() static method.
Java Programming Java8 Object Oriented Programming. The static keyword is used to create methods that will exist independently of any instances created for the class. Static methods do not use any instance variables of any object of the class they are defined in. Static methods take all the data from parameters and compute something from those ...
3 Best Practices to Test a Code That Calls Static Methods 1 Solution 1: Use dependency injection. If you have access to the source of the static method and you can/allowed to refactor it, the best solution is to change the static ... 2 Solution 2: Wrap static call in an instance method. ... 3 Solution 3: Mock the static methods. ...
The static keyword is used to create methods that will exist independently of any instances created for the class. Static methods do not use any instance variables of any object of the class they are defined in. Static methods take all the data from parameters and compute something from those parameters, with no reference to variables.
Java static method vs instance method. Instance method requires an object of its class to be created before it can be called while static method doesn't require object creation. class Difference {. public static void main(String[] args) {. display(); //calling without object. Difference t = new Difference();
Just add this code after your else condition:
for(int i=1;i<x[row].length;i++)if(col == i*2)x[row][col]= 'X';
That shood do it! Good luck!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With