CSE142 Sample Final, Spring 2022 handout #18 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 = 1; i < list.length - 1; i++) { if (i % 2 == 0) { list[i] = list[i] - list[i - 1]; } else { list[i] = list[i] + list[i - 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 ------------------------------------------------------------ {1} ____________________________ {2, 5, 9} ____________________________ {2, 3, 7, 2} ____________________________ {2, 4, 5, 4, 3} ____________________________ {5, 1, 8, 4, 8, 4} ____________________________ 3. Inheritance, 6 points. Assume the following classes have been defined: public class A extends D { public String toString() { return "a"; } public void method1() { System.out.println("a 1"); } } public class B extends C { public String toString() { return "b"; } } 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 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? 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 switchData that takes as a parameter a Scanner containing a label followed by a sequence of integers and that prints to System.out the same information with each successive pair of integers switched in order. For example, suppose that a Scanner called data contains the following tokens: Jan 1 2 3 4 5 6 Here the label is "Jan". The label will always be a single word that appears at the beginning. After the label, we have a series of six integers. If we make the following call: switchData(data); the method should produce the following output: Jan 2 1 4 3 6 5 Notice that the first pair of integers (1, 2) has been switched (2, 1), and the second pair of integers (3, 4) has been switched (4, 3), and so on. This first example involved sequential integers to make the switching more obvious, but this won't always be the case. You also shouldn't assume that you have an even number of integers. If there is an odd number of integers, then the final value should not be moved. For example, if the Scanner had instead contained these tokens: Feb 38 14 79 4 -3 then the method would have produced the following output: Feb 14 38 4 79 -3 There will always be a one-word label, but the list of integers might be empty, in which case the method simply prints the label on a line by itself. Your method should produce a complete line of output. In other words, if it is called n times, it will produce n lines of output. You may assume that the input is legal (a one-word label followed by 0 or more integer values). You may not construct any extra data structures to solve this problem. 5. Line-Based File Processing, 9 points. Write a static method called analyzeParagraphs that takes as a parameter a Scanner containing a text file and that produces output that describes the paragraph structure of the file, returning the maximum number of lines in any given paragraph. Each paragraph in the input file will be terminated by the text "<p>" on a line by itself. For example, consider the following input file: This is an example of an input file with four different paragraphs. <p> The second paragraph is the longest with three lines, so your method should return 3 when processing this file. <p> <p> The third paragraph was empty. This one is just short. <p> The method should count the number of lines in each paragraph and report that information to System.out. For example, if the input above is stored in a Scanner called input and we make the following call: int max = analyzeParagraphs(input); It should produce the following output: 2-line paragraph 3-line paragraph 0-line paragraph 1-line paragraph It would assign max the value 3 because the method returns the maximum number of lines in any given paragraph. You must exactly reproduce the format of this output. You may assume that the input file has no blank lines, that it contains at least one paragraph, and that each paragraph is terminated by a line containing just "<p>". You may not construct any extra data structures to solve this problem. 6. Arrays, 10 points. Write a static method called isAllPairs that takes an array of integers as a parameter and that returns whether or not the array is made up entirely of pairs of the same value. For example, if a variable called "list" stores the following values: {3, 3, 4, 4, 5, 5, 6, 6, 7, 7} | | | | | | | | | | +--+ +--+ +--+ +--+ +--+ pair pair pair pair pair then the following call: isAllPairs(list) should return true because the array is entirely made up of pairs of the same value: the first two values are both 3, the second two values are both 4 and so on. Your method should return false if passed an array with an odd number of values. You may assume that the array contains at least one value. Below are several sample calls. Contents of Array Value Returned ------------------------------------------------------------ {1} false {1, 1} true {1, 1, 2, 2, 3, 3} true {2, 2, 55, 55, 33, 33, 33, 33}; true {2, 2, 5, 5, 6, 7, 3, 4} false {3, 3, 2, 4, 4}; false {2, 2, 1, 1, 1}; false 7. ArrayList, 10 points. Write a static method called split that takes an ArrayList of integer values as a parameter and that replaces each value in the list with a pair of values, each half the original. If a number in the original list is odd, then the first number in the new pair should be one higher than the second so that the sum equals the original number. For example, if a variable called list stores this sequence of values: [18, 7, 4, 24, 11] The number 18 is split into the pair (9, 9), the number 7 is split into (4, 3), the number 4 is split into (2, 2), the number 24 is split into (12, 12) and the number 11 is split into (6, 5). Thus, the call: split(list); should cause list to store the following sequence of values afterwards: [9, 9, 4, 3, 2, 2, 12, 12, 6, 5] You may assume that all numbers in the list are nonnegative. 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 Sponge that extends the Critter class. The instances of the Sponge class infect if an enemy is front of them, hop if there is an empty space in front of them, and otherwise turn left or right. They display themselves as square brackets with one or more dashes inside, as in "[-]" or "[----]". They are always colored yellow. They should always display at least one dash and should initially display one dash. Each time a Sponge infects, it increases the number of dashes in the display by one and each time a Sponge turns it decreases the number of dashes in the display by one unless it has gotten down to a single dash. In deciding which direction to turn, each Sponge should follow a pattern that repeats every three turns. The first turn should be left and the second and third turns should be right. Then it repeats with another left turn followed by two right turns, 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 surroundWithN that takes two arrays of integers and an integer n as parameters and that returns a new array that contains the result of adding the first n values from the second array at both the front and the back of the first array. For example if the variables list1 and list2 contain the following values: list1: [2, 8, 17, 3, 9, 0] list2: [103, 72, -8, 15, 42] The call surroundWithN(list1, list2, 3) should return a new array storing: [103, 72, -8, 2, 8, 17, 3, 9, 0, 103, 72, -8] | | | | | | +--------+ +---------------+ +--------+ from list2 list1 from list2 Notice that the new array has the first three values from list2 followed by the values from list1 followed by the first three values from list2. You may assume that the value n passed as a parameter is legal (between 0 and the length of the second array). 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 acronym that takes as a parameter a string containing a phrase and that returns a string that has the 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 passed as a parameter 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" You are allowed to construct extra data structures to solve this problem (an array, ArrayList, Scanner, String, etc), but you may use only methods listed on the cheat sheet.
Stuart Reges
Last modified: Wed May 25 11:38:31 PDT 2022