CSE142 Final Spring 2016 Name of Student_______________________________________________________________ Section (e.g., AA)___________________ TA_____________________________________ This exam is divided into eleven questions with the following points: # Problem Area Points Score ----------------------------------------------------- 1 Reference Mystery 5 _____ 2 Array Simulation 10 _____ 3 Inheritance 6 _____ 4 Token-Based File processing 10 _____ 5 Line-Based File Processing 9 _____ 6 Arrays 10 _____ 7 ArrayList 10 _____ 8 Critters 15 _____ 9 Arrays 15 _____ 10 Programming 10 _____ 11 Art (bonus) 1 _____ ---------------------------------- Total 100 _____ This is a closed-book/closed-note exam. Space is provided for your answers. There is a "cheat sheet" at the end that you can use as scratch paper or to write answers. You can also request scratch paper from a TA. You are not allowed to access any of your own papers during the exam. In general the exam is not graded on style and you do not need to include comments, although the Critter class has special requirements you must follow. You are limited to the constructs described in chapters 1 through 10 of the textbook, which means that you are not allowed to use break statements or to have a return from a void method. You are not allowed to use methods from the Arrays class or other methods that aren't included on the cheat sheet. You do not have to include any import statements. Do not abbreviate any code that you write (e.g., S.o.p versus System.out.print) and do not abbreviate any answer asking for sample output (show the complete output). You are NOT to use any electronic devices while taking the test, including calculators. Do not begin work on this exam until instructed to do so. Any student who starts early or who continues to work after time is called will receive a 10 point penalty. If you finish the exam early, please hand your exam to the instructor and exit quietly through the front door. 1. Reference Mystery, 5 points. What output is produced by this program? import java.util.*; public class ReferenceMystery { public static void main(String[] args) { int x = 1; int y = 7; int[] data = {2, 4, 6}; mystery1(data[x], data); System.out.println(x + " " + Arrays.toString(data)); x = mystery2(x, y); System.out.println(x + " " + y); } public static void mystery1(int z, int[] numbers) { z = z + 3; numbers[z / 3]++; z = 3; numbers[z - 2]--; System.out.println(z + " " + Arrays.toString(numbers)); } public static int mystery2(int x, int y) { y++; x = x + y; return x + y; } } 2. Array 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++) { if (i >= list[i - 1]) { list[i] = list[i - 1] * 2 + 1; } } } 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 ----------------------------------------------------------------- {0, 2, 3} _________________________________ {1, 1, 4, 5} _________________________________ {3, 1, 2, 9, 5} _________________________________ {5, 1, 1, 1, 8} _________________________________ {2, 1, 1, 2, 3, 4} _________________________________ 3. Inheritance, 6 points. Assume the following classes have been defined: public class A extends B { public String toString() { return "a"; } } public class B extends D { public void method1() { System.out.println("b 1"); } public void method2() { System.out.println("b 2"); } } public class C extends D { public void method1() { System.out.println("c 1"); } } public class D { public String toString() { return "d"; } public void method1() { System.out.println("d 1"); } public void method2() { System.out.println("d 2"); } } Consider the following code fragment: D[] elements = {new B(), new A(), new D(), new C()}; 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? Write the output as a series of 3-line columns in order from left to right (do not label columns or rows). 4. Token-Based File Processing, 10 points. Write a static method called tallyScores that takes as a parameter a Scanner containing a series of student records and that prints a summary of each student record. A student record will begin with a name followed by a sequence of integer tests scores. The name is guaranteed to be one word composed of letters. You may assume that each student has at least one test score. Your method should produce two lines of output for each student: one showing the student's name and test scores and a second line showing the average score. For example, if a Scanner called records contains the following: John 71 83 94 Sally 94 85 Fred 90 95 82 85 and the following call is made: tallyScores(records); the following output should be produced: John: 71 83 94 average = 82.66666666666667 Sally: 94 85 average = 89.5 Fred: 90 95 82 85 average = 88.0 You are to exactly reproduce the format of this output. Notice that line breaks in the input are not meaningful and that the average score is not rounded. You may not construct any extra data structures to solve this problem. 5. Line-Based File Processing, 9 points. Write a static method called switchNames that takes a Scanner containing an input file as a parameter and that echoes the input to System.out with names switched in order. More specifically, the input file will contain lines that have the text "name:". Each such line will be followed by a line that has a name in the form of a last name followed by a comma and exactly one space followed by a first name. Your program should produce one line of output for each such pair of input lines with the first name followed by the last name and a space in between. For example, if a Scanner called input contains this text: As we discussed, these are the students to discuss at the meeting: name: Von Trapp, Maria name: Thornton, Billy Bob If there is time, we should also discuss: name: Porter, Riley name: Reges, Stuart Then the call: switchNames(input); should produce the following output: As we discussed, these are the students to discuss at the meeting: Maria Von Trapp Billy Bob Thornton If there is time, we should also discuss: Riley Porter Stuart Reges You may not construct any extra data structures to solve this problem other than strings. 6. Arrays, 10 points. Write a static method called minGap that takes an array of integers as a parameter and that returns the minimum gap between adjacent values in the array. The gap between two adjacent values in a list is defined as the second value minus the first value. Consider a variable called list defined as follows: int[] list = {1, 3, 6, 7, 12}; The first gap is 2 (3 - 1), the second gap is 3 (6 - 3), the third gap is 1 (7 - 6) and the fourth gap is 5 (12 - 7). Thus, the call: minGap(list) should return 1 because that is the smallest gap in the list. Notice that the minimum gap could be a negative number. If the list has fewer than 2 elements, your method should return 0. Below are several examples of what value would be returned for a given array. Array passed as parameter Value Returned -------------------------------------- -------------- {} 0 {8} 0 {4, 17} 13 {3, 5, 11, 4, 8} -7 {2, 8, 8, 10, 15} 0 You may not construct any extra data structures to solve this problem and your method should not alter the array passed as a parameter. 7. ArrayList, 10 points. Write a static method called cloneOddsRemoveEvens that takes an ArrayList of integer values as a parameter and that replaces each odd number with two of that number and that removes all even values. For example, suppose that a variable called list stores the following sequence of values: [3, 8, 19, 42, 7, 26, 27, -8, 193, 204, 6, -4] and we make the following call: cloneOddsRemoveEvens(list); Afterwards the list should store the following sequence of values: [3, 3, 19, 19, 7, 7, 27, 27, 193, 193] Notice that each odd number has been duplicated (3, 19, 7, 27, 193) and that the even values have been removed (8, 42, 26, -8, 204, 6, -4). Below are several examples of how an ArrayList should be changed by the method. ArrayList contents before call ArrayList contents after call ------------------------------ ------------------------------ [] [] [82] [] [3, 8, 15] [3, 3, 15, 15] [2, 4, 6, 8] [] [17, 5, 3, 9] [17, 17, 5, 5, 3, 3, 9, 9] [1, 2, 3, 4, 5, 6] [1, 1, 3, 3, 5, 5] You may not construct any extra data structures to solve this problem. You must solve it by manipulating the ArrayList you are passed as a parameter. See the cheat sheet for a list of available ArrayList methods. 8. Critters, 15 points. Write a class called Spider that extends the Critter class. The instances of the Spider class alternate between "north" mode in which they are trying to travel north and "south" mode in which they are trying to travel south. Each spider should start out in north mode. Spiders always infect when an enemy is in front of them. Otherwise they turn left if they are not facing in the desired direction. If there is no enemy in front and they are facing in the desired direction, they hop if possible. When they are unable to hop in the desired direction, they switch to the other mode. Spiders are always green and their display string is composed of either "N-" or "S-" depending upon whether they are in north or south mode followed by the number of hops they have successfully made in that direction since they last switched. For example, each spider will initially display as "N-0" indicating that they are trying to move north but haven't yet made any moves. If they are able to move north, their display becomes "N-1", "N-2", and so on, until they switch to south mode, at which time they display as "S-0", and so on. As in assignment 8, fields must be declared private, fields initialized to a non-default value must be set in a constructor, and all updates to fields must occur in the getMove method. 9. Arrays, 15 points. Write a static method called insert that takes two arrays of integers and an integer index as parameters and that returns a new array that contains the result of inserting the values from the second array into the first array starting at the specified index. For example if the variables list1 and list2 contain the following values: list1: [7, 2, 8, 9, 4, 13, 7, 1, 9, 10] list2: [1, 2, 3, 4, 5] Then the call insert(list1, list2, 2) should return a new array that stores: [7, 2, 1, 2, 3, 4, 5, 8, 9, 4, 13, 7, 1, 9, 10] | | +---list2---+ Notice that the new array has all of the values from list1 in their original order, but starting at index 2 the new array has the five values from list2. The order of the parameters matters. For example, insert(list2, list1, 3) should produce the following array as its result: [1, 2, 3, 7, 2, 8, 9, 4, 13, 7, 1, 9, 10, 4, 5] | | +-----------list1------------+ You may assume that the index passed as a parameter is legal (between 0 and the length of the array passed as the first parameter). Your method should not construct any extra data structures other than the array to be returned and it should not alter its two array parameters. You are not allowed to call methods of the Arrays class to solve this problem. 10. Programming, 10 points. Write a static method called samePattern that returns true or false depending upon whether two strings have the same pattern of characters. More precisely, two strings have the same pattern if they are of the same length and if two characters in the first string are equal if and only if the characters in the corresponding positions in the second string are also equal. Below are some examples of patterns that are the same and patterns that differ (keep in mind that the method should return the same value no matter what order the two strings are passed). 1st String 2nd String Same Pattern? ------------ -------------- ------------- "" "" true "a" "x" true "a" "ab" false "ab" "ab" true "aa" "xy" false "aba" "+-+" true "---" "aba" false "abcabc" "zodzod" true "abcabd" "zodzoe" true "abcabc" "xxxxxx" false "aaassscccn" "aaabbbcccd" true "asasasasas" "xyxyxyxyxy" true "ascneencsa" "aeiouuoiea" true "aaassscccn" "aaabbbcccd" true "asasasasas" "xxxxxyyyyy" false "ascneencsa" "aeiouaeiou" false "aaassscccn" "xxxyyyzzzz" false "aaasssiiii" "gggdddfffh" false Your method should take two parameters: the two strings to compare. You are allowed to create new strings, but otherwise you are not allowed to construct extra data structures to solve this problem (no array, ArrayList, Scanner, etc). You are limited to the string methods on the cheat sheet. 11. Art, 1 point (bonus). You've programmed lions, bears, giants, and huskies, but what kind of critter would your TA be? Give an artistic rendering of how you see your TA fitting into the unforgiving world of the critter simulation. You may either write a poem or draw a picture. If your answer below indicates at least 1 minute of effort, you will receive full credit, although your TA would probably appreciate it if you put in a little more effort.
Stuart Reges
Last modified: Fri Sep 16 16:16:07 PDT 2011