CSE142 Sample Final handout #29 1. Expressions, 5 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 12/5 + 8/4 __________ 2.5 * 2 + 17/4 __________ 41 % 15 % 7 + 17 % 3 __________ 21/2 + "7 % 3" + 17 % 4 __________ 46/3/2.0/3 * 4/5 __________ 2. Simulation, 10 points. You are to simulate the execution of a method that manipulates an array of integers. Consider the following method: public static void mystery(int[] list) { for (int i = 0; i < list.length; i++) { list[i] = i * list[i]; } } In the left-hand column below are specific lists of integers. You are to indicate in the right-hand column what values would be stored in the list after method mystery executes if the integer list in the left-hand column is passed as a parameter to mystery. Original List Final List ------------------------------------------------------------ () ____________________________ (7) ____________________________ (3, 2) ____________________________ (5, 4, 3) ____________________________ (2, 4, 6, 8) ____________________________ 3. Polymorphism, 6 points. Assume the following classes have been defined: public class A extends B { public void method2() { System.out.println("a 2"); } } public class B extends C { public String toString() { return "b"; } public void method2() { System.out.println("b 2"); } } public class C { public String toString() { return "c"; } public void method1() { System.out.println("c 1"); } public void method2() { System.out.println("c 2"); } } public class D extends B { public void method1() { System.out.println("d 1"); } } Consider the following code fragment: C[] elements = {new A(), new B(), new C(), new D()}; for (int i = 0; i < elements.length; i++) { System.out.println(elements[i]); elements[i].method1(); elements[i].method2(); System.out.println(); } What output is produced by this code? 4. Token-Based File Processing, 9 points. Write a static method printStrings that takes as a parameter a Scanner holding a sequence of integer/String pairs and that prints to System.out one line of output for each pair with the given String repeated the given number of times. For example if the Scanner contains the following data: 6 fun. 3 hello 10 <> 4 wow! your method should produce the following output: fun.fun.fun.fun.fun.fun. hellohellohello <><><><><><><><><><> wow!wow!wow!wow! Notice that there is one line of output for each integer/String pair. The first line has 6 occurrences of "fun.", the second line has 3 occurrences of "hello", the third line has 10 occurrences of "<>" and the fourth line has 4 occurrences of "wow!". Notice that there are no extra spaces included in the output. You are to exactly reproduce the format of this sample output. You may assume that the input values always come in pairs with an integer followed by a String. If the Scanner is empty (no integer/String pairs), your method should produce no output. Write your solution to printStrings below. 5. Line-Based File Processing, 10 points. Write a static method reverseLines that takes a Scanner containing an input file as a parameter and that echos the input file to System.out with each line of text reversed. For example, given the following input file: If this method works properly, the lines of text in this file will be reversed. Remember that some lines might be blank. Your method should print the following output: ,ylreporp skrow dohtem siht fI elif siht ni txet fo senil eht .desrever eb lliw .knalb eb thgim senil emos taht rebmemeR Notice that some of the input lines can be blank lines. Write your solution to reverseLines below. 6. Arrays, 10 points. Write a static method isAllEven that takes an array of integers as a parameter and that returns a boolean value indicating whether or not all of the values are even numbers (true for yes, false for no). For example, if a variable called list stores the following values: (18, 0, 4, 204, 8, 4, 2, 18, 206, 1492, 42) Then the call: isAllEven(list) should return true because each of these integers is an even number. If instead the list had stored these values: (2, 4, 6, 8, 10, 208, 16, 7, 92, 14) Then the call should return false because, although most of these values are even, the value 7 is an odd number. Write your solution to isAllEven below. 7. ArrayList, 10 points. Write a static method removeShorterStrings that takes an ArrayList of Strings as a parameter and that removes from each successive pair of values the shorter String in the pair. For example, suppose that an ArrayList called "list" contains the following values: ("four", "score", "and", "seven", "years", "ago") In the first pair of Strings ("four" and "score") the shorter String is "four". In the second pair of Strings ("and" and "seven") the shorter String is "and". In the third pair of Strings ("years" and "ago") the shorter string is "ago". Therefore, the call: removeShorterStrings(list); should remove these shorter Strings, leaving the list with the following sequence of values after the method finishes executing: ("score", "seven", "years") If there is a tie (both Strings have the same length), your method should remove the first String in the pair. If there is an odd number of Strings in the list, the final value should be kept in the list. For example, if the list contains the following values: ("to", "be", "or", "not", "to", "be", "hamlet") After calling removeShorterStrings, it should contain the following: ("be", "not", "be", "hamlet") You may assume that the ArrayList you are passed contains only Strings. Recall that the primary methods for manipulating an ArrayList<E> are: add(E value) appends value at end of list add(int index, E value) inserts given value at given index, shifting subsequent values right clear() removes all elements of the list get(int index) returns the value at given index remove(int index) removes and returns value at given index, shifting subsequent values left set(int index, E value) replaces value at given index with given value size() returns the number of elements in list Write your solution to removeShorterStrings below. 8. Critters, 15 points. Write a class Pigeon that implements the Critter interface. The instances of the Pigeon class go through cycles in which they move a certain number of times south followed by moving the same number of times north (returning to their original position). On each cycle they either move 2 south followed by 2 north or they move 4 south followed by 4 north or they move 6 south followed by 6 north or they move 8 south followed by 8 north (each choice should be equally likely). Each Pigeon object should return the letter "P" for display purposes. Remember that the Critter interface is defined as follows: public interface Critter { public char getChar(); public int getMove(int x, int y); <definitions for constants NORTH, SOUTH, EAST and WEST> } Write your solution to the Pigeon class below. 9. Arrays, 15 points. Write a static method isUnique that takes an array of integers as a parameter and that returns a boolean value indicating whether or not the values in the array are unique (true for yes, false for no). The values in the list are considered unique if there is no pair of values that are equal. For example, if a variable called "list" stores the following values: (3, 8, 12, 2, 9, 17, 43, -8, 46, 203, 14, 97, 10, 4) then the call: isUnique(list) should return true because there are no duplicated values in this list. If instead the list stored these values: (4, 7, 2, 3, 9, 12, -47, -19, 308, 3, 74) then the call should return false because the value 3 appears twice in this list. Notice that given this definition, a list of 0 or 1 elements would be considered unique. Write your solution to isUnique below. 10. Programming, 10 points. Write a static method collapse that takes an array of integers as an argument and that returns a new array that contains the result of collapsing the original list by replacing each successive pair of integers with the sum of the pair. For example, if a variable called "list" stores this sequence of values: (7, 2, 8, 9, 4, 13, 7, 1, 9, 10) Then the following call: collapse(list); Should return a new array containing the following values: (9, 17, 17, 8, 19) The first pair from the original list is collapsed into 9 (7 + 2), the second pair is collapsed into 17 (8 + 9), the third pair is collapsed into 17 (4 + 13) and so on. If the list stores an odd number of elements, the final element is not collapsed. For example, if the list had been: (1, 2, 3, 4, 5) Then the call on collapse would produce the following list: (3, 7, 5) with the 5 at the end of the list unchanged. Keep in mind that your method is to return a new array of appropriate length that you construct. Your method should not change the array that is passed as a parameter. Write your solution to collapse below.
Stuart Reges
Last modified: Tue Dec 6 11:53:16 PST 2005