Key to CSE142 Sample Final handout #34 1. Original List Final List -------------------------------------- (3) (0) (7, 8) (0, 10) (5, 4, 3) (0, 6, 10) (10, 11, 12, 13) (0, 13, 28, 45) (2, 4, 6, 8, 10) (0, 6, 16, 30, 48) 2. Polymorphism. The output produced is as follows. sally sally 1 fred 2 fred fred 1 fred 2 sally sally 1 george 2 harold sally 1 fred 2 3. File Processing. One possible solution appears below. public static void processData(Scanner input) { int numNums = 0; int numEvens = 0; int sum = 0; while (input.hasNextInt()) { int number = input.nextInt(); numNums++; sum += number; if (number % 2 == 0) numEvens++; } System.out.println("Total numbers = " + numNums); System.out.println("Sum of numbers = " + sum); System.out.println("Total evens = " + numEvens); System.out.println("Percent evens = " + 100.0 * numEvens/numNums); } 4. File Processing. One possible solution appears below. public static void processFile(Scanner input) { int numLines = 0; int numChars = 0; String maxLine = ""; while (input.hasNextLine()) { String next = input.nextLine(); numChars += next.length(); numLines++; if (next.length() > maxLine.length()) maxLine = next; } System.out.println("Total lines = " + numLines); System.out.println("Total chars = " + numChars); System.out.println("Length of longest line = " + maxLine.length()); System.out.println("Longest line = " + maxLine); } 5. Arrays. One possible solution appears below. public static int numDuplicates(int[] list) { int count = 0; for (int i = 0; i < list.length - 1; i++) if (list[i] == list[i + 1]) count++; return count; } 6. ArrayLists. One possible solution appears below. public static void replaceStrings(ArrayList list, String target, String replacement) { for (int i = 0; i < list.size(); i++) { String s = (String)list.get(i); if (s.equals(target)) list.set(i, replacement); } } 7. Critters. One possible solution appears below. public class Robot implements Critter { private int myMax; private int myCurrent; private boolean myGoingEast; public Robot() { myGoingEast = true; myCurrent = 0; myMax = 1; } public char getChar() { if (myGoingEast) return '>'; else return '<'; } public int getMove() { int result; if (myGoingEast) result = EAST; else result = WEST; myCurrent++; if (myCurrent == myMax) { myCurrent = 0; myMax++; myGoingEast = !myGoingEast; } return result; } } 8. Classes. One possible solution appears below. public class BonusPlan { private int myMonthly; private int myOther; private int myPaid; public BonusPlan() { myMonthly = 1; } public void rent(boolean free) { if (!free) { myPaid++; if (myPaid == 3) { myPaid = 0; myOther++; } } else if (myMonthly > 0) myMonthly--; else if (myOther > 0) myOther--; else System.out.println("no credits"); } public int getCredit() { return myMonthly + myOther; } public void endMonth() { myMonthly = 1; myPaid = 0; } } 9. Arrays. One possible solution appears below. public void evenOdd() { int numEvens = 0; for (int i = 0; i < myLength; i++) if (myList[i] % 2 == 0) { int temp = myList[i]; myList[i] = myList[numEvens]; myList[numEvens] = temp; numEvens++; } }
Stuart Reges
Last modified: Fri Dec 10 15:35:54 PST 2004