Key to CSE142 Final handout #35 1. List Value returned -------------------------------------- (8) 0 (14, 7) 1 (7, 1, 3, 2, 0, 4) 3 (10, 8, 9, 5, 6) 2 (8, 10, 8, 6, 4, 2) 4 2. Polymorphism. The output produced is as follows. sock sock 1 book 2 sock pen 1 lamp 2 lamp lamp 1 lamp 2 sock sock 1 lamp 2 3. File Processing. One possible solution appears below. public static void processData(Scanner input) { int count = 0; int sumLength = 0; while (input.hasNext()) { String next = input.next(); count++; sumLength += next.length(); } double average = (double)sumLength/count; System.out.println("Total words = " + count); System.out.println("Average length = " + average); } 4. File Processing. One possible solution appears below. public static void processFile(Scanner input) { while (input.hasNextLine()) { String first = input.nextLine(); if (input.hasNextLine()) { String second = input.nextLine(); System.out.println(second); } System.out.println(first); } } 5. Arrays. One possible solution appears below. public static int minGap(int[] list) { if (list.length < 2) return 0; else { int min = list[1] - list[0]; for (int i = 2; i < list.length; i++) { int gap = list[i] - list[i - 1]; if (gap < min) min = gap; } return min; } } 6. ArrayLists. One possible solution appears below. public static void markLength4(ArrayList list) { int index = 0; while (index < list.size()) { String next = (String)list.get(index); if (next.length() == 4) { list.add(index, "****"); index += 2; } else index++; } } 7. Critters. One possible solution appears below. public class Quail implements Critter { private int myPhase; // 1 for N, 2 for W, 3 for E, 4 for S private int myMove; // where in phase private int myLength; public Quail() { myPhase = 1; myMove = 0; } public char getChar() { return 'Q'; } public int getMove() { myMove++; // pick length if at beginning of cycle if (myPhase == 1 && myMove == 1) if (Math.random() < 0.5) myLength = 10; else myLength = 20; int direction; if (myPhase == 1) direction = NORTH; else if (myPhase == 2) direction = WEST; else if (myPhase == 3) direction = EAST; else // myPhase == 4 direction = SOUTH; if (myMove == myLength) { myPhase++; myMove = 0; if (myPhase == 5) myPhase = 1; } return direction; } } 8. Classes. One possible solution appears below. public class PhoneCard { private double myCredit; private double mySpent; private int myMinutes; public PhoneCard(double credit) { myCredit = credit; } public void call(int minutes) { call(minutes, 1.0); } public void call(int minutes, double multiplier) { double spent = 0.25 + minutes * 0.1 * multiplier; mySpent += spent; myCredit -= spent; myMinutes += minutes; } public double getCredit() { return myCredit; } public double getAverage() { if (myMinutes == 0) return 0.1; else return mySpent/myMinutes; } } 9. Arrays. One possible solution appears below. public static int numSame(int[] list1, int[] list2) { int count = 0; int index1 = 0; int index2 = 0; while (index1 < list1.length && index2 < list2.length) if (list1[index1] == list2[index2]) { count++; index1++; index2++; } else if (list1[index1] < list2[index2]) index1++; else // list1[index1] > list2[index2] index2++; return count; } The following solution would get 4 points. public static int numSame(int[] list1, int[] list2) { int count = 0; for (int i = 0; i < list1.length; i++) for (int j = 0; j < list2.length; j++) if (list1[i] == list2[j]) count++; return count; }
Stuart Reges
Last modified: Mon Dec 27 15:15:32 PST 2004