CSE142 Final Winter 2009 Name of Student_______________________________________________________________ Section (e.g., AA)___________________ TA_____________________________________ This is an open-book/open-note exam. Space is provided for your answers. Use the backs of pages if necessary. The exam is divided into eleven questions with the following points: # Problem Area Points Score ----------------------------------------------------- 1 Expressions 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 _____ In general the exam is not graded on style and you do not need to include comments, although all fields of a class should be declared private. 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. Anyone caught using an electronic device will receive a 10 point penalty. 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. 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 8.0/4/4 + 3/4 + 0.5 * 3 __________ 88 % 10 % 5/2 + 7 % 8 __________ 2 * 3 + 4 + " = " + 5 * 6 + 7 __________ 54/5/2.0/4 * 3 __________ 2 * (0.75 + 1.25)/5 * 10 - 7/4 __________ 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 = 0; i < list.length - 1; i++) { if (i % 2 == 0) { list[i]++; } else { 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 ------------------------------------------------------------ {6, 3} ____________________________ {2, 4, 6} ____________________________ {1, 2, 3, 4} ____________________________ {2, 2, 2, 2, 2} ____________________________ {7, 3, 2, 0, 5, 1} ____________________________ 3. Inheritance, 6 points. Assume the following classes have been defined: public class D extends C { public String toString() { return "d"; } public void method2() { System.out.println("d 2"); } } public class B { public String toString() { return "b"; } public void method1() { System.out.println("b 1"); } public void method2() { System.out.println("b 2"); } } public class A extends D { public String toString() { return "a"; } } public class C extends B { public void method1() { System.out.println("c 1"); } } Consider the following code fragment: B[] elements = {new C(), new A(), new D(), new B()}; 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, 10 points. Write a static method called evaluate that takes as a parameter a Scanner containing a series of tokens that represent a numeric expression involving addition and subtraction and that returns the value of the expression. For example, if a Scanner called data contains the following tokens: 4.2 + 3.4 - 4.1 and we make the following call: evaluate(data) the method should evaluate the result as (4.2+3.4-4.1) = (7.6-4.1) = 3.5 and should return this value as its result. Every expression will begin with a real number and then will have a series of operator/number pairs that follow. The operators will be either "+" (addition) or "-" (subtraction). As in the example above, there will be spaces separating numbers and operators. You may assume the expression is legal. Your program should evaluate operators sequentially from left to right. For example, for this expression: 7.3 - 4.1 - 2.0 your method should evaluate the operators as follows: 7.3 - 4.1 - 2.0 = (7.3 - 4.1) - 2.0 = 3.2 - 2.0 = 1.2 The Scanner might contain just a number, in which case your method should return that number as its result. 5. Line-Based File Processing, 9 points. Write a static method called processScores that takes as a parameter a Scanner containing a series of lines that represent student records. Each student record takes up two lines of input. The first line has the student's name and the second line has a series of plus and minus characters. Below is a sample input: Kane, Erica --+-+ Chandler, Adam ++-+ Martin, Jake +++++++ Dillon, Amanda ++-++-+- The number of plus/minus characters will vary, but you may assume that at least one such character appears and that no other characters appear on the second line of each pair. For each student you should produce a line of output with the student's name followed by a colon followed by the percent of plus characters. For example, if the input above is stored in a Scanner called input and we make the following call: processScores(input); The following output should be produced: Kane, Erica: 40.0% plus Chandler, Adam: 75.0% plus Martin, Jake: 100.0% plus Dillon, Amanda: 62.5% plus You must exactly reproduce this format. 6. Arrays, 10 points. Write a static method called isFibLike that takes an array of integers as a parameter and that returns whether or not the sequence matches the pattern of the Fibonacci sequence (true if it does, false if it does not). The Fibonacci sequence begins with the number 1 followed by the number 1 and each successive value is the sum of the two previous values: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, and so on. It is possible to follows this pattern with different starting values. For example, Lucas numbers start with the values 2 and 1 but otherwise follow the Fibonacci pattern. Your method should determine whether each value after the first two is the sum of the previous two values in the sequence, returning true if the sequence has that pattern and returning false if it does not. If the array has two or fewer values, your method should return true. Below are sample arrays and the value that should be returned for each: Contents of Array Value returned passed to isFibLike by isFibLike --------------------------- -------------- {} true {42} true {18, 42} true {1, 1, 1} false {1, 2, 3} true {0, 0, 0, 0, 0} true {1, 1, 2, 3, 5, 8, 13, 21} true {2, 1, 3, 4, 7, 11, 18, 29} true {1, 1, 2, 3, 5, 12, 17} false 7. ArrayList, 10 points. Write a static method called reverse3 that takes an ArrayList of integer values as a parameter and that reverses each successive sequence of three values in the list. For example, suppose that a variable called list stores the following sequence of values: [3, 8, 19, 42, 7, 26, 19, -8, 193, 204, 6, -4] and we make the following call: reverse3(list); Afterwards the list should store the following sequence of values: [19, 8, 3, 26, 7, 42, 193, -8, 19, -4, 6, 204] The first sequence of three values (3, 8, 19) has been reversed to be (19, 8, 3). The second sequence of three values (42, 7, 26) has been reversed to be (26, 7, 42). And so on. If the list has extra values that are not part of a sequence of three, those values are unchanged. For example, if the list had instead stored: [3, 8, 19, 42, 7, 26, 19, -8, 193, 204, 6, -4, 99, 2] The result would have been: [19, 8, 3, 26, 7, 42, 193, -8, 19, -4, 6, 204, 99, 2] Notice that the values (99, 2) are unchanged in position because they were not part of a sequence of three values. 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 8. Critters, 15 points. Write a class called Orca that extends the Critter class. The instances of the Orca class follow a pattern of moving forward four times, then turning around, then moving back four times and turning around again so that they return to their original position and direction. Each Orca is always either in moving-mode or in turning-mode. They start out in moving-mode. While in moving-mode, they try to hop forward if possible until they have hopped four times, at which point they switch into turning-mode. If it is not possible to hop while in moving-mode, an Orca instead infects whatever is in front of it. When in turning-mode, the Orca turns left twice and then switches back to moving-mode. Don't worry about the fact that if the Orca encounters a wall while in moving-mode, it gets stuck trying to infect the wall indefinitely. The Orca displays itself as "M" while in moving-mode and as "T" while in turning-mode. Its color should be the default color for critters. 9. Arrays, 15 points. Write a static method called append that takes two integer arrays as parameters and that returns a new array that contains the result of appending the values of the second array at the end of the first array. For example, suppose that arrays list1 and list2 have been declared as follows: int[] list1 = {2, 4, 6}; int[] list2 = {1, 2, 3, 4, 5}; If we make the following call: append(list1, list2) The method will return a new array that contains the following values: [2, 4, 6, 1, 2, 3, 4, 5] If the call instead had been: append(list2, list1) Then the method would return a new array that contains: [1, 2, 3, 4, 5, 2, 4, 6] Your method should not change either of the arrays passed as parameters. 10. Programming, 10 points. Write a static method called acronym that takes as a parameter a String containing a phrase and that returns an acronym for the phrase. For example, the following call: acronym("self-contained underwater breathing apparatus") should return "SCUBA". The acronym is formed by combining the capitalized first letters of each word in the phrase. Words in the phrase will be separated by some combination of dashes and spaces. There might be extra spaces or dashes at the beginning or end of the phrase. The String will not contain any characters other than dashes, spaces, and letters, and is guaranteed to contain at least one word. Below are several sample calls. Method Call Value Returned ----------------------------------------------- -------------- acronym(" automatic teller machine ") "ATM" acronym("personal identification number") "PIN" acronym("computer science") "CS" acronym("merry-go-round") "MGR" acronym("All my Children") "AMC" acronym("Troubled Assets Relief Program") "TARP" acronym("--quite-- confusing - punctuation-") "QCP" acronym(" loner ") "L" 11. Art, 1 point (bonus). President Obama is busy filling cabinet positions. Give an artistic rendering of how you see your TA fitting into the new administration (Drug czar? Director of the CIA? Supreme Court Justice?). 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:06:48 PDT 2011