CSE142 Sample Final handout #26
Spring 2006
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. 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++)
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
------------------------------------------------------------
(5) ____________________________
(4, 7) ____________________________
(2, 3, 4) ____________________________
(2, 4, 6, 8) ____________________________
(9, 7, 5, 3, 1) ____________________________
3. Polymorphism, 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?
4. Token-Based File Processing, 10 points. Write a static method called
reportRunningSum that takes as a parameter a Scanner holding a sequence of
real numbers and that prints to System.out the running sum of the numbers
followed by the maximum running sum. In other words, the n-th number that
you report should be the sum of the first n numbers in the Scanner and the
maximum that you report should be the largest such value that you report.
For example if the Scanner contains the following data:
3.25 4.5 -8.25 7.25 3.5 4.25 -6.5 5.25
your method should produce the following output:
running sum = 3.25 7.75 -0.5 6.75 10.25 14.5 8.0 13.25
max sum = 14.5
The first number reported is the same as the first number in the Scanner
(3.25). The second number reported is the sum of the first two numbers in
the Scanner (3.25 + 4.5). The third number reported is the sum of the first
three numbers in the Scanner (3.25 + 4.5 + -8.25). And so on. The maximum
of these values is 14.5, which is reported on the second line of output.
You are to exactly reproduce the format of this output. You may assume that
there is at least one number to read.
Write your solution to reportRunningSum below.
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 the format of this
output.
Write your solution to reportBlankLines below.
6. Arrays, 10 points. Write a static method called hasAlternatingParity that
returns whether or not an array of integers has alternating parity (true if
it does, false otherwise). The parity of an integer is 0 for even numbers
and 1 for odd numbers. To have alternating parity, a list would have to
alternate between even and odd numbers, as in the following list:
(3, 2, 19, 8, 43, 64, 1, 0, 3)
If an array called "data" had the values above stored in it, then the
following call:
hasAlternatingParity(data)
would return true. If instead the array stored the following values:
(2, 13, 4, 1, 0, 9, 2, 7, 4, 12, 3, 2)
then the call would return false because the list has two even numbers in a
row (4 and 12).
By definition, an empty list or a list of one element has alternating
parity. You may assume that the values in the array are greater than or
equal to 0.
Write your solution to hasAlternatingParity below.
7. ArrayList, 10 points. Write a static method switchPairs that switches the
order of values in an ArrayList of Strings in a pairwise fashion. Your
method should switch the order of the first two values, then switch the
order of the next two, switch the order of the next two, and so on.
For example, if the list initially stores these values:
("four", "score", "and", "seven", "years", "ago")
your method should switch the first pair ("four", "score"), the second pair
("and", "seven") and the third pair ("years", "ago"), to yield this list:
("score", "four", "seven", "and", "ago", "years")
If there are an odd number of values in the list, the final element is not
moved. For example, if the original list had been:
("to", "be", "or", "not", "to", "be", "hamlet")
It would again switch pairs of values, but the final value ("hamlet") would
not be moved, yielding this list:
("be", "to", "not", "or", "be", "to", "hamlet")
You may assume that the ArrayList you are passed contains only Strings.
Recall that the primary methods for manipulating an ArrayList 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
Write your solution to switchPairs below.
8. Critters, 15 points. Write a class Yak that implements the Critter
interface. The instances of the Yak class first pick a random direction and
go one in that direction, then pick a random direction and go two in that
direction, then pick a random direction and go three in that direction, then
pick a random direction and go four in that direction, and so on, each time
picking a random direction and going in that direction one further than it
went in the previous direction. When picking a random direction, each
direction should be equally likely. Each Yak object should return the
letter "Y" for display purposes.
Remember that the Critter interface is defined as follows:
public interface Critter {
public char getChar();
public int getMove(CritterInfo info)
}
Write your solution to the Yak class below.
9. Arrays, 15 points. Write a static method removeZeros that takes an array of
integers as a parameter and that moves any zeros in the array to the end of
the array, otherwise preserving the order of the list. For example, if a
variable called "list" stores the following values:
(7, 2, 3, 0, 4, 6, 0, 0, 13, 0, 78, 0, 0, 19, 14)
then the call:
removeZeros(list);
should rearrange the values in the array so that it stores the following:
(7, 2, 3, 4, 6, 13, 78, 19, 14, 0, 0, 0, 0, 0, 0)
Notice that the six zeros have been moved to the end of the array and the
other values are in the same order as in the original list.
You are not allowed to use an auxiliary data structure such as a temporary
array or ArrayList to solve this problem. You can, however, receive 10 of
15 points if you solve the problem using such a structure.
Write your solution to removeZeros below.
10. Programming, 10 points. Write a static method rearrange that takes an
array of integers as an argument and that rearranges the values so that all
of the multiples of 3 come first followed by numbers that are one greater
than a multiple of 3 followed by numbers that are two greater than a
multiple of 3. For example, if a variable called "list" stores this
sequence of values:
(23, 12, 8, 0, 4, 80, 9, 7, 30, 99, 50, 42, 13, 47, 2, 16, 87, 75)
Then the following call:
rearrange(list);
Should rearrange the values to look something like this:
(12, 0, 9, 30, 99, 42, 87, 75, 4, 13, 16, 7, 23, 47, 2, 50, 8, 80)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~
multiples of 3 1 more than a 2 more than a
multiple of 3 multiple of 3
This is only one possible arrangement. Any arrangement that puts the
multiples of 3 first followed by the values that are one more than a
multiple of 3 followed by the values that are two more than a multiple of 3
is acceptable. You are not allowed to use an auxiliary data structure such
as a temporary array or ArrayList to solve this problem. You may assume
that all values in the array are greater than or equal to 0.
Write your solution to rearrange below.