CSE142B Final
Winter 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).
5. Line-Based File Processing, 9 points. Write a static method called
reportBlankLines that takes a Scanner containing an input file as a
parameter and that reports to System.out the line numbers of any blank lines
and that reports the total number of blank lines in the file. For example,
given the following input file:
Remember that a file
can have blank lines
like the one below:
A blank line:
is read as a String
of length 0
by Scanner
Your method should print the following output:
line 4 is blank
line 6 is blank
line 9 is blank
total blank lines = 3
Notice that each blank line produces a line of output and that there is a
final line of output reporting the total number of blank lines. Also notice
that lines are numbered starting with 1 (first line is line 1, second line
is line 2, and so on). You are to exactly reproduce this format.
6. Arrays, 10 points. Write a static method called isPalindrome that takes an
array of integers as a parameter and that returns true if the sequence of
numbers is a palindrome and that returns false otherwise. A sequence of
integers is considered a palindrome if it has the same sequence of integers
when reversed. For example, if a variable called list stores the following:
[3, 5, 7, 5, 3, 2]
Then the call isPalindrome(list) should return false because when reversed
this sequence becomes [2, 3, 5, 7, 5, 3], which is not the same as the
original sequence. The following sequences are examples of palindromes:
[] [8] [1, 1] [42, 3, 42] [18, 9, 7, 7, 9, 18]
Notice that arrays of length 0 or 1 are considered palindromes. You may not
construct any extra data structures to solve this problem (not even a
string) and you are not allowed to alter the array passed as a parameter.
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.
8. Critters, 15 points. Write a class called Lizard that extends the Critter
class. The instances of the Lizard class display themselves in a pattern
that involves increasing and decreasing numbers. Lizards initially display
as a less-than followed by 1 followed by greater-than (<1>). On each
subsequent move the number should increase by one until it reaches 10. Then
it should start decreasing by one until it gets back down to 1. Then it
should repeat the process by increasing again. The sequence is:
<1>, <2>, <3>, ..., <9>, <10>, <9>, <8>, <7>, ..., <2>, <1>, <2>, ...
The display should always change after a move. Lizards should be green.
They should always infect when an enemy is in front of them. If there isn't
an enemy to infect, they should hop if they can. If there is no enemy in
front and they can't hop, they should turn right.
As in assignment 8, 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 interleave that takes two
arrays of integers as parameters and that returns a new array that contains
the result of interleaving the elements of the two arrays. Two arrays are
interleaved by taking elements in an alternating fashion from the two lists
(first value of first list, first value of second list, second value of
first list, second value of second list, etc). For example, if the
variables list1 and list2 contain the following values:
list1: [1, 8, 3, 9] list2: [2, 12, 6, 14]
Then the call interleave(list1, list2) should return a new array that stores
the following values:
[1, 2, 8, 12, 3, 6, 9, 14]
The order of the parameters matters. For example, interleave(list2, list1)
should produce the following array as its result:
[2, 1, 12, 8, 6, 3, 14, 9]
One list might be longer than the other, in which case any extra values from
the longer list should simply be ignored. For example, given the following:
list1: [1, 8, 3, 9] list2: [82, 7, 4, 2, 1, 6, 5, 0, 18]
The call interleave(list1, list2) should produce the following result:
[1, 82, 8, 7, 3, 4, 9, 2]
Notice that the first four values of the two arrays have been interleaved
and the excess values from the second list (1, 6, 5, 0, 18) have been
ignored. In this case the second list was longer, but it could be the case
that the first list is longer. Either list could also be empty, in which
case the result should be an empty array. The method should not construct
any extra data structures other than the array to be returned and it should
not alter either of its parameters.
10. Programming, 10 points. Write a static method called count that takes as
parameters a target string and a source string and that returns a count of
the number of occurrences of the target string in the source string. For
example, the following call:
count("i", "Mississippi")
should return 4 because there are 4 occurrences of the string "i" in the
string "Mississippi". Your method should ignore case when comparing
strings. For example, the call:
count("iss", "MISSISSIPPI")
should return 2 because there are two occurrences of "iss" in
"MISSISSIPPI". Your method should consider all possible starting positions
for the target string. For example, given the following call:
count("EE", "EeEeE")
Your method should return the value 4 because there are 4 different
locations where the string "ee" occurs in the string "EeEeE" (starting at
index 0, index 1, index 2, and index 3).
You may assume that the target string is not empty.
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.
CSE142 Final Cheat Sheet
Math Method Description ArrayList Types
-------------------------------------- -----------------
Math.abs(n) absolute value of n ArrayList
Math.max(x, y) maximum of x and y ArrayList
Math.min(x, y) minimum of x and y
Array Construction: int[] data = new int[size];
String Method Description
-----------------------------------------------------------------------------
charAt(index) character at a specific index
endsWith(text) whether or not string ends with text
equals(text) whether or not string equals text
equalsIgnoreCase(text) whether or not string equals text ignoring case
indexOf(text) index of a particular string (-1 if not found)
length() number of characters in string
startsWith(text) whether or not string starts with text
substring(start, stop) characters from start (inclusive) to stop (exclusive)
substring(start) characters from start to end of string
toLowerCase() a new String with lowercase version of original
toUpperCase() a new String with uppercase version of original
ArrayList method Description
-------------------------------------------------------------------------------
add(value) appends value at end of list
add(index, value) inserts value at index, shifting subsequent values right
clear() removes all elements of the list
contains(value) whether the list contains the value
get(index) returns the value at given index
isEmpty() whether list is empty
remove(index) removes value at index, shifting subsequent values left
set(index, value) replaces value at given index with given value
size() returns the number of elements in list
Critter Details
---------------
getMove returns one of Action.HOP, Action.LEFT, Action.RIGHT, Action.INFECT.
There are methods for finding out what kind of critters are around a critter
(one of Neighbor.WALL, Neighbor.EMPTY, Neighbor.SAME, Neighbor.OTHER):
getFront(), getBack(), getLeft(), getRight()
There is a method getDirection() to find out what direction a critter is facing
(one of Direction.NORTH, Direction.SOUTH, Direction.EAST, Direction.WEST)
public class FlyTrap extends Critter {
public String toString() {
return "T";
}
public Action getMove(CritterInfo info) {
if (info.getFront() == Neighbor.OTHER) {
return Action.INFECT;
} else {
return Action.LEFT;
}
}
public Color getColor() {
return Color.RED;
}
}
Stuart Reges
Last modified: Fri Sep 16 16:16:07 PDT 2011