CSE142 Sample Final handout #26 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 (6 + 7)/4/2.0 __________ 13/2 * 3 % 10 - 1 __________ 30/(2 * 6) + 1.5 __________ 13 % 5 + 5 * 3/4 __________ "3 * 4" + 3 * 4 + 10 __________ 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 = 2; i < list.length; i++) { list[i] = list[i] + list[i - 1] + list[i - 2]; } } 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 ------------------------------------------------------------ (8) ____________________________ () ____________________________ (3, 0, 1, 4, 7) ____________________________ (0, 1, 2, 3, 4, 5) ____________________________ (7, 4, -10, 8, 2) ____________________________ 3. Polymorphism, 6 points. Assume the following classes have been defined: public class Mammal extends SeaCreature { public void method1() { System.out.println("warm-blooded"); } } public class SeaCreature { public void method1() { System.out.println("creature 1"); } public void method2() { System.out.println("creature 2"); } public String toString() { return "ocean-dwelling"; } } public class Whale extends Mammal { public void method1() { System.out.println("spout"); } public String toString() { return "BIG!"; } } public class Squid extends SeaCreature { public void method2() { System.out.println("tentacles"); } public String toString() { return "squid"; } } Consider the following code fragment: SeaCreature[] elements = {new Squid(), new Whale(), new SeaCreature(), new Mammal()}; 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 processData that takes as a parameter a Scanner holding a sequence of integers and that reports each of the cumulative sums of the sequence along with the average of the numbers. For example, if the Scanner contains the following data: 8 4 2 9 7 13 5 9 your method should produce the following output: Sum of 1 = 8 Sum of 2 = 12 Sum of 3 = 14 Sum of 4 = 23 Sum of 5 = 30 Sum of 6 = 43 Sum of 7 = 48 Sum of 8 = 57 Average = 7.125 Notice that the various lines of output report the sum including just the first number, then including the first two numbers, then including the first three numbers, and so on, up to the sum including all numbers. The final line of output reports the average of the sequence of numbers. Notice that this is the average of the numbers themselves, not the average of the cumulative sums. The amount of output will vary depending upon how many numbers are in the sequence. For example, if the Scanner contains the following values: 1 2 3 4 the method should produce the following output: Sum of 1 = 1 Sum of 2 = 3 Sum of 3 = 6 Sum of 4 = 10 Average = 2.5 You are to exactly reproduce the format of these sample outputs. You may assume that the Scanner has at least one integer to be processed. Write your solution to processData below. 5. Line-Based File Processing, 10 points. Write a static method processFile that takes a Scanner containing an input file as a parameter and that prints the output file with certain lines underlined. The lines to be underlined will all begin with a period. The period should not be printed. You should print the text that follows the period on a line by itself followed by a line of dashes that is equal in length to the text that follows the period. For example, given the following input file: .Statement of Purpose I didn't expect to major in computer science until I took cse142. I liked it more than I expected and that got me hooked on cs. .High School Performance I got very good grades in high school, graduating in the top 10% of my class. .College Performance I have done well in my college classes, with an overall gpa of 3.5. Your method should print the following output: Statement of Purpose -------------------- I didn't expect to major in computer science until I took cse142. I liked it more than I expected and that got me hooked on cs. High School Performance ----------------------- I got very good grades in high school, graduating in the top 10% of my class. College Performance ------------------- I have done well in my college classes, with an overall gpa of 3.5. Notice that some of the input lines can be blank lines. Write your solution to processFile below. 6. Arrays, 10 points. Write a static method printNumber that takes an array of integers that represent the digits of a large integer and that prints the integer with commas between every 3 digits starting from the right. Each array element will store one digit of the number with array element 0 storing the first digit of the number, array element 1 storing the second digit of the number and so on. You may assume that the values in the array are all between 0 and 9 and that there are no leading 0's. For example, if the array contains the digits (1, 5, 0, 0), then your method should produce the following output: 1,500 If the array instead contains the digits (3, 8, 4, 9, 2, 1, 4, 7), then your method should produce the following output: 38,492,147 Your method might not print any commas if the number is small enough. For example, if the array contains the digits (2, 5, 0), then your method should produce the following output: 250 Your method should produce an entire line of output so that if it is called several times in a row, each call will produce a separate line of output. 7. ArrayList, 10 points. Write a static method manyStrings that takes an ArrayList of Strings and an integer n as parameters and that replaces every String in the original list with n of that String. For example, suppose that an ArrayList called "list" contains the following values: ("squid", "octopus") And you make the following call: manyStrings(list, 2); Then list should store the following values after the call: ("squid", "squid", "octopus", "octopus") As another example, suppose that list contains the following: ("a", "a", "b", "c") and you make the following call: manyStrings(list, 3); Then list should store the following values after the call: ("a", "a", "a", "a", "a", "a", "b", "b", "b", "c", "c", "c") You may assume that the ArrayList you are passed contains only Strings and that the integer n is greater than 0. Recall that the primary methods for manipulating an ArrayList are: add(Object value) appends value at end of list add(int index, Object 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 value at given index, shifting subsequent values left set(int index, Object value) replaces value at given index with given value size() returns the number of elements in list Write your solution to manyStrings below. 8. Critters, 15 points. Write a class Ant that implements the Critter interface. The instances of the Ant class go through cycles of ten moves. For each cycle, the Ant randomly chooses between going north-east or going south-west (each choice should be equally likely). For a north-east cycle, the ant goes north 5 times and then goes east 5 times. For a south-west cycle, the ant goes south 5 times and then goes west 5 times. Each Ant object should return the letter "A" for display purposes. Remember that the Critter interface is defined as follows: public interface Critter { public char getChar(); public int getMove(); <definitions for constants NORTH, SOUTH, EAST and WEST> } Write your solution to the Ant class below. 9. Arrays, 15 points. Write a static method interleave that takes two arrays of integers as parameters and that returns a new array that contains the result of interleaving the elements of the two arrays. It should not alter either of its parameters. Two arrays are interleaved by taking elements in the following order: 1st element of 1st list 1st element of 2nd list 2nd element of 1st list 2nd element of 2nd list 3rd element of 1st list 3rd element of 2nd list and so on For example, if the variables list1 and list2 contain the following values: list1: (1, 8, 3, 9) list2: (2, 12, 6, 14) Then the call interleave(list1, list2) should return a new array that stores the following values: (1, 2, 8, 12, 3, 6, 9, 14) The order of the parameters matters. For example, interleave(list2, list1) should produce the following array as its result: (2, 1, 12, 8, 6, 3, 14, 9) One list might be longer than the other, in which case any extra values from the longer list should simply be appended at the end of the result. For example, given the following lists: list1: (1, 8, 3, 9) list2: (82, 7, 4, 2, 1, 6, 5, 0, 18) The call interleave(list1, list2) should produce the following result: (1, 82, 8, 7, 3, 4, 9, 2, 1, 6, 5, 0, 18) Notice that the first four values of the two arrays have been interleaved and the excess values from the second list (1, 6, 5, 0, 18) have been included at the end of the result. In this case the second list was longer, but it could be the case that the first list is longer. Either list could also be empty, in which case the result should contain the values from the other list. Notice that your method is to return a new array. Write your solution to interleave below. 10. Programming, 10 points. Write a static method indexOf that takes two arrays of integers and that returns the index of the first occurrence of the first list in the second list or -1 if the first list does not appear in the second list. For example, suppose that you have two integer arrays called list1 and list2 that store the following values: list1: (1, 3, 5, 8, 12, 1, 3, 17, 1, 3, 6, 9, 1, 3, 6) list2: (1, 3, 6) then the call: indexOf(list2, list1) should return 8 because the sequence of values stored in list2 appears in list1 starting with index 8. Notice that list2 actually appears twice in list1, starting at position 8 and starting at position 12. Your method is to return the first such position. If the second list is not contained in the first list, then the method should return the value -1. For example, if list1 had the same value as before but list2 stored (12, 1, 3, 6), then the call indexOf(list2, list1) should return -1 because list2 is not contained in list1. If the first list is empty, your method should return 0. Write your solution to indexOf below.
Stuart Reges
Last modified: Wed Jun 1 13:27:45 PDT 2005