Question #37719

Write a value-returning method that returns the number of columns with n elements in a two-dimensional array of chars, where n is a parameter of the method. Include code to test your method.

Expert's answer

public class MyClass {
    static char arr[][] = {{'l', 'l'}, {'k', 'k', 'f'}, {'g', 'g', 'g', 'g', 'g', 'g'}};
    static int getNum(int n) {
        int num = 0;
        for (int i = 0; i < arr.length; i++) {
            if (n == arr[i].length) {
                num = i + 1;
            }
        }
        return num;
    }
    public static void main(String args[]) {
        System.out.println(getNum(2)); // must print 1
        System.out.println(getNum(3)); // must print 2
        System.out.println(getNum(6)); // must print 3
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS