/** * Write a description of class LabWeek9 here. * * @author (your name) * @version (a version number or a date) */ public class LabWeek9 { // returns the largest element in a // e.g. max({{1, 3}, {7, -2, 0}, {4, 4}}) = 7 public static int max(int[][] a) { int max = a[0][0]; for (int[] r : a) for (int x : r) max = Math.max(max, x); return max; } // returns the sum of the elements in the xth row in a // e.g. rowSum({{3}, {8, -2, 6}, {5, -2}}, 1) = 8 + (-2) + 6 = 12 public static int rowSum(int[][] a, int x) { int sum = 0; if (0 <= x && x < a.length) for (int y : a[x]) sum += y; return sum; } // returns the sum of the elements in the xth column in a // e.g. columnSum({{3, 4}, {6}, {0, 8, -2}}, 1) = 4 + 8 = 12 public static int columnSum(int[][] a, int x) { int sum = 0; if (0 <= x) for (int[] r : a) if (x < r.length) sum += r[x]; return sum; } // returns an array holding the sums of the rows in a // e.g. allRowSums({{1}, {2, 3}, {4, 5, 6}}) = {1, 5, 15} public static int[] allRowSums(int[][] a) { int[] sums = new int[a.length]; for (int i = 0; i < a.length; i++) sums[i] = rowSum(a, i); return sums; } // returns true iff a is square, // i.e. iff every row has the same length as the number of rows public static boolean isSquare(int[][] a) { for (int[] r : a) if (r.length != a.length) return false; return true; } }