import java.util.Arrays; public class ArrayTester { /** Returns an array containing the elements of column c of arr2D in the same order as * they appear in arr2D. * Precondition: c is a valid column index in arr2D. * Postcondition: arr2D is unchanged. */ public static int[] getColumn(int[][] arr2D, int c) { /* to be implemented in part (a) */ } /** Returns true if square is a Latin square as described in part (b); * false otherwise. * Precondition: square has an equal number of rows and columns. * square has at least one row. */ public static boolean isLatin(int[][] square) { /* to be implemented in part (b) */ } /** Returns true if and only if every value in arr1 appears in arr2. * Precondition: arr1 and arr2 have the same length. * Postcondition: arr1 and arr2 are unchanged. */ public static boolean hasAllValues(int[] arr1, int[] arr2) { /* implementation by Chris Thiel, OFMCap */ for (int a1: arr1) { boolean found = false; for(int a2: arr2) if(a1 == a2) found =true; if (!found) return false; } return true; } /** Returns true if arr contains any duplicate values; * false otherwise. */ public static boolean containsDuplicates(int[] arr) { /* implementation by Chris Thiel, OFMCap */ for (int i = 0; i < arr.length; i++) for(int j=i+1; j < arr.length; j++ ) if(arr[i] == arr[j]) return true; return false; } public static String arr2DToString(int [][] a) { int rows = a.length; String s ="[ "+Arrays.toString(a[0])+",\n"; for(int r = 1; r < rows-1; r++) s += " " + Arrays.toString(a[r]) + ",\n"; //last row: s += " " + Arrays.toString(a[rows-1]) + " ]\n"; return s; } public static void main(String[] args) { System.out.println("Array Tester\nPart (a): getColumn"); System.out.println ("If arr2D contains "); int[][] arr2D = { {0, 1, 1}, {3, 4, 5}, {6, 7, 8}, {9, 5, 3} }; System.out.println(arr2DToString(arr2D)); System.out.print("getColumn(arr2D,0) should result in [0, 3, 6, 9]\nyour code returns " ); System.out.println ( Arrays.toString(getColumn(arr2D, 0) ) ); System.out.print("\ngetColumn(arr2D,1) should result in [1, 4, 7, 5]\nyour code returns " ); System.out.println ( Arrays.toString(getColumn(arr2D, 1) ) ); System.out.print("\ngetColumn(arr2D,2) should result in [2, 5, 8, 3]\nyour code returns " ); System.out.println ( Arrays.toString(getColumn(arr2D, 2) ) ); System.out.println("\nPart(b): isLatin: "); int [][] a1 = { {1, 2, 3}, {2, 3, 1}, {3, 1, 2} }; System.out.println("a1 is \n"+arr2DToString(a1)); System.out.println("a1 IS Latin, so isLatin(a1) should be true, "); System.out.println("your code returns "+Boolean.toString( isLatin(a1) ) ); int [][] a2 = { {10, 30, 20, 0}, { 0, 20, 30, 10}, {30, 0, 10, 20}, {20, 10, 0, 30} }; System.out.println("a2 is \n"+arr2DToString(a2)); System.out.println("a2 IS Latin, so isLatin(a2) should be true, "); System.out.println("your code returns "+Boolean.toString( isLatin(a2) ) ); int [][] a3 = { {1, 2, 1}, {2, 1, 1}, {1, 1, 2} }; System.out.println("a3 is \n"+arr2DToString(a3)); System.out.println("a3 IS NOT Latin, so isLatin(a3) should be false, "); System.out.println("your code returns "+Boolean.toString( isLatin(a3) ) ); int [][] a4 = { {1, 2}, {1, 2}, }; System.out.println("a4 is \n"+arr2DToString(a4)); System.out.println("a4 IS NOT Latin, so isLatin(a4) should be false, "); System.out.println("your code returns "+Boolean.toString( isLatin(a4) ) ); int [][] a5 = { {1, 2, 3, 4, 5}, {5, 1, 2, 3, 4}, {4, 5, 1, 2, 3}, {3, 4, 5, 1, 2}, {2, 3, 4, 5, 1} }; System.out.println("a5 is \n"+arr2DToString(a5)); System.out.println("a5 IS Latin, so isLatin(a5) should be true, "); System.out.println("your code returns "+Boolean.toString( isLatin(a5) ) ); } }