CSE142 Final
Autumn 2013
Name of Student_______________________________________________________________
Section (e.g., AA)___________________ TA_____________________________________
This 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 _____
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 are not allowed to access any other paper during the exam
(not even scratch paper). Use the backs of the pages of this test if
necessary. Anyone caught with extra paper will lose at least 10 points.
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 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
43 % 15 / 3 + 15 / 2 __________
6.2 * 5 / 10 + 3.5 __________
6 * 2.5 / 4 + (2.3 + 2.7) / 4 __________
"18" + 3 * 4 + (8 + 5) __________
59 % 10 / (2 + 2) * 2.5 / 2 __________
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 (list[i] % 2 == 0) {
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
------------------------------------------------------------
{8, 3} ____________________________
{2, 5, 4} ____________________________
{3, 4, 5, 7} ____________________________
{2, 5, 4, 2} ____________________________
{4, 5, 7, 9, 3} ____________________________
3. Inheritance, 6 points. Assume the following classes have been defined:
public class A extends B {
public void method2() {
System.out.println("a 2");
}
}
public class B extends C {
public String toString() {
return "b";
}
public void method2() {
System.out.println("b 2");
}
}
public class C {
public String toString() {
return "c";
}
public void method1() {
System.out.println("c 1");
}
public void method2() {
System.out.println("c 2");
}
}
public class D extends B {
public void method1() {
System.out.println("d 1");
}
}
Consider the following code fragment:
C[] elements = {new A(), new B(), new C(), new D()};
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
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 some of the input lines can be blank lines. You may not
construct any extra data structures to solve this problem.
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: 2, 1, 3, 4, 7, 11, 18, 29, 47, 76, and so on.
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
--------------------------- --------------
{} 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
You may not construct any extra data structures to solve this problem.
7. ArrayList, 10 points. Write a static method called expand that takes an
ArrayList of integer values as a parameter and that replaces pairs of
integer values with sequences of numbers. In particular, the ArrayList will
contain pairs of numbers that indicate the number of occurrences of a
particular value (in that order). For example, suppose a variable called
list stores this sequence of values:
[2, 4, 3, 5, 1, 9, 12, 8]
The first pair indicates that we want 2 occurrences of the number 4. The
second pair indicates that we want 3 occurrences of the number 5. The third
pair indicates that we want 1 occurrence of the number 9. The final pair
indicates that we want 12 occurrences of the number 8. If we make the call:
expand(list);
We expect that the ArrayList will store these values afterwards:
[4, 4, 5, 5, 5, 9, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8]
You may assume that the ArrayList stores a legal sequence of values, which
means that it has an even length and all of the counts are greater than or
equal to 1.
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 Salmon that extends the Critter
class. The instances of the Salmon class should always infect when an enemy
is in front of them and should otherwise hop if the space in front of them
is empty. When they can't either infect or hop, they should randomly choose
between turning left and turning right with each choice being equally
likely. They should be colored pink (there is a color constant for pink).
They should initially display themselves with the text "<*>" before they
have made any turns. After they have made a turn, they should display
themselves with the text "" if their last turn was left and "" if
their last turn was right.
As in assignment 9, all fields must be declared private and fields that need
to be initialized to a non-default value must be set in a constructor.
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).
The method should not construct any extra data structures other than the
array to be returned and it should not alter its two array parameters.
10. Programming, 10 points. Write a static method called encode that takes a
string s and an integer n as parameters and that returns a new string that
scrambles the order of the characters from s in a particular way. Taking
the characters from s in order, imagine filling a grid of n rows column by
column. When s is "abcdefghijklmnopqrstuvwxyz" and n is 3, you get:
row 1: a d g j m p s v y
row 2: b e h k n q t w z
row 3: c f i l o r u x
The method should return the result of concatenating these characters
together with row 1 first, then row 2, and then row 3. Notice that the
final column will not necessarily be complete, as in the example above
where the final column has only two characters. So if you make the call:
encode("abcdefghijklmnopqrstuvwxyz", 3)
You should get back the string "adgjmpsvybehknqtwzcfilorux". The string
might contain any characters, including spaces. For example, the call:
encode("four score and seven", 4)
returns "f rneosedvuc eroasn" because the following grid would be produced:
row 1: f r n e
row 2: o s e d v
row 3: u c e
row 4: r o a s n
You may assume that the string passed as a parameter is not empty and that
the integer passed as a parameter is greater than or equal to 1 and less
than the length of the string. You are not allowed to construct any
structured objects other than strings to solve this problem (no array,
ArrayList, etc).
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