Final_09au_key - CSE 142 Autumn 2009 Final Exam Key 1 Array Mystery Expression int a1 = cfw_42 42 arrayMystery(a1 int a2 = cfw_6 2 4

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
1 of 10 CSE 142, Autumn 2009 Final Exam Key 1. Array Mystery Expression int[] a1 = {42, 42}; arrayMystery(a1); int[] a2 = {6, 2, 4}; arrayMystery(a2); int[] a3 = {7, 7, 3, 8, 2}; arrayMystery(a3); int[] a4 = {4, 2, 3, 1, 2, 5}; arrayMystery(a4); int[] a5 = {6, 0, -1, 3, 5, 0, -3}; arrayMystery(a5); Final Contents of Array {42, 42} {6, 8, 4} {7, 3, 8, 2, 2} {4, 5, 3, 4, 7, 5} {6, 5, 9, 11, 6, 3, -3} 3. Reference Semantics Mystery 11 [21] 30,41 10 [21] 30,41 11 [23] 31,42 10 [23] 31,42 3. Inheritance Mystery Biggie a JayZ a Tupac b Tupac b Biggie Tupac a Tupac b Tupac JayZ a Tupac b Tupac b Tupac Biggie a JayZ a FiftyCent b FiftyCent b Biggie
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 of 10 4. File Processing (3 solutions shown) // count up money as a double of dollars/cents; use printf at end public static void countCoins(Scanner input) { double total = 0.0; while (input.hasNext()) { int count = input.nextInt(); String coin = input.next().toLowerCase(); if (coin.equals("nickels")) { count = count * 5; } else if (coin.equals("dimes")) { count = count * 10; } else if (coin.equals("quarters")) { count = count * 25; } total = total + (double) count / 100; } System.out.printf("Total money: $%.2f\n", total); } // count up money as a total number of cents; use / and % at end public static void countCoins(Scanner input) { int totalCents = 0; while (input.hasNext()) { int count = input.nextInt(); String coin = input.next().toLowerCase(); if (coin.equals("nickels")) { totalCents += count * 5; } else if (coin.equals("dimes")) { totalCents += count * 10; } else if (coin.equals("quarters")) { totalCents += count * 25; } } int dollars = totalCents / 100; int cents = totalCents % 100; System.out.print("Total money: $" + dollars + "."); if (cents < 10) { System.out.print("0"); } System.out.println(cents); } // implicit /100 in the multipliers public static void countCoins(Scanner input) { double total = 0.0; while (input.hasNext()) { double count = input.nextInt(); String coin = input.next().toLowerCase(); if (coin.equals("pennies")) { count *= 0.01; } else if (coin.equals("nickels")) { count *= 0.05; } else if (coin.equals("dimes")) { count *= 0.10; } else { count *= 0.25; } total += count; } System.out.printf("Total money: $%.2f\n", total); }
Background image of page 2
3 of 10 5. File Processing public static void matchIndex(Scanner input) { int lines = 0; while (input.hasNextLine()) { String line1 = input.nextLine(); String line2 = input.nextLine(); lines += 2; System.out.print("lines " + (lines - 1) + " and " + lines + ":"); // print any matches found boolean matchedAny = false; int length = Math.min(line1.length(), line2.length()); for (int i = 0; i < length; i++) { if (line1.charAt(i) == line2.charAt(i)) { matchedAny = true; System.out.print(" " + i); } } if (!matchedAny) { System.out.print(" none"); } System.out.println(); } }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 of 10 6. Array Programming (3 solutions shown) // concise solution w/Math.max public static String[] longer(String[] a1, String[] a2) { String[] a3 = new String[Math.max(a1.length, a2.length)];
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/15/2011 for the course CSE 142 taught by Professor Martystepp during the Spring '07 term at University of Washington.

Page1 / 10

Final_09au_key - CSE 142 Autumn 2009 Final Exam Key 1 Array Mystery Expression int a1 = cfw_42 42 arrayMystery(a1 int a2 = cfw_6 2 4

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online