1. Expression Value ---------- ----- (5 + 4 * 2 - 1 / 4) % 5 3 line.length() > 10 && numbers[1] > numbers.length true lineScan.hasNextInt() || (numbers[0] % 10 == 0) false 3 * 3 + "TEST" + 1 + 1 + 2 * 2 "9TEST114" line.toUpperCase().charAt(numbers.length - numbers[2]) 'R' 2. Method Call Contents of array ----------- ----------------- int[] a1 = {7, 5}; mystery(a1); {1, 2} int[] a2 = {3, 11, 4, 9}; mystery(a2); {1, 2, 1, 3} int[] a3 = {0, 0, 0, 1, 1, 1}; mystery(a3); {1, 2, 3, 1, 2, 3} int[] a4 = {26, 13, 37, -6, 16, 45, 12}; mystery(a4); {1, 1, 2, -6, 2, 3, 3} int[] a5 = {-1, 1, -2, 16, 3, -3, -4, 5}; mystery(a5); {-1, 1, -2, 1, 2, -3, -4, 3} 3. tuition upperclass ditch tuition books newbie party old study tuition books study 4. public static void bestGPA(Scanner input) { while (input.hasNextLine()) { String line = input.nextLine(); Scanner lineScan = new Scanner(line); String bestName = lineScan.next(); double bestGPA = lineScan.nextDouble(); while (lineScan.hasNext()) { String name = lineScan.next(); double gpa = lineScan.nextDouble(); if (gpa > bestGPA) { bestGPA = gpa; bestName = name; } } System.out.println("Best GPA: " + bestGPA + " (" + bestName + ")"); } } 5. public static int countDuplicates(int[] a) { int count = 0; for (int i = 1; i < a.length; i++) { if (a[i] == a[i - 1]) { count++; } } return count; } 6. Two solutions are shown. // 'count the lesser elements' solution public static int[] ordinal(int[] a) { int[] result = new int[a.length]; for (int i = 0; i < a.length; i++) { for (int j = 0; j < a.length; j++) { if (a[j] < a[i]) { result[i]++; } } } return result; } // 'sorted array' solution public static int[] ordinal(int[] a) { int[] sorted = new int[a.length]; for (int i = 0; i < a.length; i++) { sorted[i] = a[i]; // copy 'a' into 'sorted' } Arrays.sort(sorted); int[] result = new int[a.length]; for (int i = 0; i < a.length; i++) { // index of element a[i] in sorted order should be stored as result[i] result[i] = Arrays.binarySearch(sorted, a[i]); } return result; } 7. Two solutions are shown. // 'count days in each month' solution public int getDayOfYear() { int result = this.day; for (int i = 1; i < this.month; i++) { result += getDaysInMonth(i, this.year); } return result; } // 'count days from Jan. 1' solution public int getDayOfYear() { Date temp = new Date(this.year, 1, 1); int result = 1; while (!this.equals(temp)) { temp.nextDay(); result++; } return result; }