CSE142 Sample Midterm, Spring 2017 handout #8 1. Expressions, 10 points. For each expression in the left-hand column, indicate its value in the right-hand column. Be sure to list a constant of appropriate type (e.g., 7.0 rather than 7 for a double, Strings in quotes). Expression Value 13 + 2 * 5 / 3 ________________ 2.5 * 2 * 5 / 10 + 1.5 ________________ 85 % 10 + 4 % 10 - 17 % 3 ________________ 2 + 3 + "." + (3 + 4) + 2 * 3 ________________ 482 / 10 / 5 / 2.0 * 2 + 14 / 5 ________________ 2. Parameter Mystery, 12 points. Consider the following program. public class Mystery { public static void main(String[] args) { String she = "it"; String it = "her"; String her = "you"; String you = "she"; saying(you, it, you); saying(it, her, she); saying(she, "you", her); saying(it, "him", "fred"); } public static void saying(String it, String her, String she) { System.out.println(she + " can't take " + it + " with " + her); } } List below the output produced by this program. 3. If/Else Simulation, 12 points. Consider the following method. public static void ifElseMystery(int a, int b) { if (a > b || a % 2 == 0) { a++; b--; } else if (a % 2 == 1) { b = 0; } if (b == 0 && b != a) { a = a + 2; b = a - 2; } System.out.println(a + " " + b); } For each call below, indicate what output is produced. Method Call Output Produced ifElseMystery(5, 20); _______________ ifElseMystery(42, 42); _______________ ifElseMystery(6, 1); _______________ ifElseMystery(2, 0); _______________ ifElseMystery(7, 10); _______________ ifElseMystery(4, 4); _______________ 4. While Loop Simulation, 12 points. Consider the following method: public static void mystery(int x) { int y = 1; int z = 2; while (x > z) { if (x % z == 0) { x = x / z; y++; } else { z++; } } System.out.println(y + " " + z); } For each call below, indicate what output is produced: Method Call Output Produced mystery(2); _______________ mystery(5); _______________ mystery(9); _______________ mystery(12); _______________ 5. Assertions, 15 points. You will identify various assertions as being either always true, never true or sometimes true/sometimes false at various points in program execution. The comments in the method below indicate the points of interest. public static void mystery(Scanner console) { int y = 42; int z = 0; // Point A while (y != 0) { // Point B y = console.nextInt(); if (y % 2 == 0) { z++; // Point C y--; } // Point D y--; } // Point E System.out.println("z = " + z); } Fill in the table below with the words ALWAYS, NEVER or SOMETIMES. y == 0 y % 2 == 0 z == 0 +---------------------+---------------------+---------------------+ Point A | | | | +---------------------+---------------------+---------------------+ Point B | | | | +---------------------+---------------------+---------------------+ Point C | | | | +---------------------+---------------------+---------------------+ Point D | | | | +---------------------+---------------------+---------------------+ Point E | | | | +---------------------+---------------------+---------------------+ 6. Programming, 15 points. Write a static method called spinWheel that takes a Random object and an integer n as parameters and that simulates the spinning of a wheel until the number 20 comes up n times in a row. On the wheel are the numbers 20, 30, 40, 50, and 60 and each number should be equally likely to come up when the wheel is spun. Your method should report the individual spins as well as indicating how many times it takes to get n occurrences of 20 in a row. For example, below are two sample calls: Random r = new Random(); spinWheel(r, 2); spinWheel(r, 3); The first call should produce two lines of output like this: spins: 40, 40, 50, 20, 50, 50, 40, 20, 30, 40, 50, 20, 20 2 in a row after 13 spins The second call should produce two lines of output like this: spins: 50, 50, 50, 20, 40, 20, 40, 20, 20, 20 3 in a row after 10 spins Notice that the spin values are separated by commas and that the method stops when it has seen n occurrences of the value 20 in a row. You are to exactly reproduce the format of these logs. You may assume that the value n passed to your method is greater than or equal to 1. 7. Programming, 15 points. Write a static method called balanceCheckbook that takes a console Scanner as a parameter and that prompts a user for information about transactions for a bank account, reporting the new balance after each transaction and the minimum balance at the end and returning whether or not the account was ever overdrawn (true if it was, false if it was not). The user is prompted for an initial balance, the number of transactions to process, and the individual transaction amounts. Deposits to the account are entered as positive numbers and checks and ATM withdrawals are entered as negative numbers. A new balance is reported after each transaction. For example, the method would be called as follows: Scanner console = new Scanner(System.in); balanceCheckbook(console); Below are two sample logs of execution that might be produced: initial balance? 48.50 initial balance? 39.75 how many transactions? 4 how many transactions? 5 1/4 amount? -20.00 1/5 amount? -18.50 new balance = \$28.5 new balance = \$21.25 2/4 amount? -5.75 2/5 amount? -7.20 new balance = \$22.75 new balance = \$14.05 3/4 amount? 138.20 3/5 amount? -23.10 new balance = \$160.95 new balance = \$-9.05 4/4 amount? -20.00 4/5 amount? 50.00 new balance = \$140.95 new balance = \$40.95 minimum balance = \$22.75 5/5 amount? -8.45 new balance = \$32.5 minimum balance = \$-9.05 In the log to the left, the user enters 4 different transactions and the minimum balance is not negative, so the method would return false to indicate that the account was never overdrawn. In the log to the right, the user enters 5 transactions and the minimum balance is negative, so the method would return true to indicate that the account was overdrawn. You are to exactly reproduce the format of these logs. You may assume that the number of transactions entered by the user is at least 1. 8. Programming, 9 points. Write a static method called sameDashes that takes two strings as parameters and that returns whether or not they have dashes in the same places (returning true if they do and returning false otherwise). For example, below are four pairs of strings of equal length that have the same pattern of dashes. Notice that the last pair has no dashes at all. string1: "hi--there-you." "-15-389" "criminal-plan" "abc" string2: "12--(134)-7539" "-xy-zzy" "(206)555-1384" "9.8" To be considered a match, the strings must have exactly the same number of dashes in exactly the same positions. The strings might be of different length. For example, the following calls should each return true: sameDashes("1st-has-more characters", "2nd-has-less") sameDashes("1st-has-less", "2nd-has-more characters") because the strings each have two dashes and they are in the same positions. But the following calls should each return false because the longer string has a third dash where the shorter string does not: sameDashes("1st-has-more-characters", "2nd-has-less") sameDashes("1st-has-less", "2nd-has-more-characters") In solving this problem you may use only methods listed on the cheat sheet.