CSE142 Final
Autumn 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.
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 = 6;
int y = 3;
int[] data = {12, 8, 4};
data[1] = mystery1(y, data);
x--;
System.out.println(x + " " + Arrays.toString(data));
x = mystery2(y, x);
System.out.println(x + " " + y);
}
public static int mystery1(int y, int[] numbers) {
y--;
numbers[y]++;
System.out.println(y + " " + Arrays.toString(numbers));
numbers[0] = 2;
return y;
}
public static int mystery2(int x, int z) {
x = x + z;
z = x * 2;
return z;
}
}
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 - 2; i++) {
if (list[i + 1] > list[i]) {
list[i + 1] = list[i] + (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
-----------------------------------------------------------------
{2, 5, 6, 9} _________________________________
{1, 3, 3, 5, 8} _________________________________
{4, 4, 5, 8, 2} _________________________________
{3, 6, 9, 12, 15} _________________________________
{2, 4, 6, 8, 10, 12} _________________________________
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 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
underline that takes a Scanner containing an input file as a parameter and
that prints to System.out the same text with certain lines underlined. The
lines to be underlined 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 equal in length to the text that follows
the period. For example, consider the following input:
.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.
If the text above is stored in a Scanner called input and we make this call:
underline(input);
the method should print the following output to System.out:
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 input lines can be blank lines. You may not construct any extra
data structures to solve this problem other than strings.
6. Arrays, 10 points. Write a static method called numUnique that takes a
sorted array of integers as a parameter and that returns the number of
unique values in the array. The array is guaranteed to be in sorted order,
which means that duplicates will be grouped together. For example, if a
variable called "list" stores the following values:
[5, 7, 7, 7, 8, 22, 22, 23, 31, 35, 35, 40, 40, 40, 41]
then the following call:
numUnique(list)
should return 9 because this list has 9 unique values (5, 7, 8, 22, 23, 31,
35, 40 and 41). It is possible that the list might not have any
duplicates. For example if list instead stored this sequence of values:
[1, 2, 11, 17, 19, 20, 23, 24, 25, 26, 31, 34, 37, 40, 41]
then a call on the method would return 15 because this list contains 15
different values.
If passed an empty list, your method should return 0. Remember that you can
assume that the values in the array appear in sorted (nondecreasing) order.
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 rearrange that takes an
ArrayList of integer values as a parameter and that moves to the end of the
list all values originally appearing in odd index positions in the list,
preserving the relative order of these values. For example, suppose that a
variable called list stores the following sequence of values:
[0, 1, 2, 3, 4, 5, 6, 7, 8]
and we make the following call:
rearrange(list);
Afterwards the list should store the following sequence of values:
[0, 2, 4, 6, 8, 1, 3, 5, 7]
Notice that the values originally stored in odd indexes have been moved to
the end of the list (1, 3, 5, and 7). This example involved sequential
integers to make it easier to see exactly which values are moved, but this
won't always be the case. For example, if the list had instead stored:
[14, 8, 6, 3, 1, 4, 2, 19, 42, 9]
^ ^ ^ ^ ^
| | | | |
+-----+-----+-----+-------+
values to be moved
then it would store the following values after the method executes:
[14, 6, 1, 2, 42, 8, 3, 4, 19, 9]
Notice that what matters is whether values are in odd index positions, not
whether they are odd numbers (the values 8, 3, 4, 19, and 9 in the example).
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 Meeseeks that extends the Critter
class. The instances of the Meeseeks class sometimes display themselves as
"mister" and sometimes as "meeseeks". They randomly pick one of the strings
initially with each choice being equally likely. They display themselves
using this string just prior to their first move, their second move, their
third move, and their fourth move. Then they make another random selection
and use that value to display themselves just before their fifth, sixth,
seventh, and eighth move. They continue in this manner, making a new choice
every four moves. They are always colored blue. They hop when there is
nothing in front of them and turn left if another Meeseeks is in front of
them and otherwise infect.
Use a Random object to make random choices but each Meeseeks should
construct only one such object. As in assignment 9, 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 collapse that takes an
array of integers as a parameter 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. The method should not
construct any extra data structures other than the array to be returned and
it should not alter its parameter.
10. Programming, 10 points. Write a static method called sumCount that takes a
data array, a sum array, and a count array as parameters and that stores in
the sum and count arrays the results of tallying the values in the data
array. This problem is a variation of the personality test program that
you wrote for assignment 7. As in that program, the values in the data
array will appear in groups of seven values with the first value in each
group associated with dimension 0, the next two values associated with
dimension 1, the next two values associated with dimension 2, and the next
two values associated with dimension 3. In the personality program you
processed a string that had characters like "A" and "B" to indicate a
user's response to a question. In this case we assume the test has been
converted to a numeric scale where the user has provided a value between 1
and 10 to indicate how strongly they feel about one answer versus the
other. In the personality program the character "-" indicated that the
user did not answer that particular question. In the data array, a value
of 0 indicates that the user did not answer that question.
Your task is to process the values in the data array to give values to the
sum and count arrays. For example, suppose a variable called data stores:
[0, 1, 0, 6, 9, 2, 3, 7, 3, 7, 7, 5, 10, 1]
Assuming we have constructed arrays called sum and count of length 4 and we
make the following call:
sumCount(data, sum, count);
after the call, the sum and count arrays would store these values:
sum: [7, 11, 27, 16]
count: [1, 3, 4, 4]
In this case, the data array has two groups of seven values, so we would
normally expect two values for dimension 0 and four each for the other
dimensions. But in the data array, the values at index 0 and 2 are 0,
indicating that the user didn't answer those questions. That is why the
count array has a count of 1 for dimension 0 and a count of 3 for dimension
1. The sum array stores the sum of the individual responses for each
dimension. Although you do not have to solve this part of the problem, it
might be useful to understand that the combination of the sum and count
arrays would allow someone to compute a "percent B" value similar to what
we did in the homework.
You may assume that the sum and count arrays have already been constructed
before your method is called and that they are both of length 4, but you
are not allowed to assume that they contain all zero values. For example,
suppose that we reset the data array to store these values:
[7, 5, 0, 4, 0, 3, 0, 0, 1, 3, 0, 9, 8, 0, 7, 0, 9, 10, 4, 0, 6]
If we repeat the previous call, we expect sum and count to be set to:
sum: [14, 18, 27, 17]
count: [2, 4, 4, 3]
You may assume that the data array has complete groups of seven answers,
which means that its length will always be a multiple of seven.
You are not allowed to construct extra data structures to solve this
problem (no String, array, ArrayList, Scanner, etc).
Write your solution on the next page.
Write your solution to sumCount below.
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