Final Sample 2
Final Sample 2
In the left-hand column below are specific arrays of integers. Indicate in the right-hand column what value would be
returned by method arrayMystery if the integer array in the left-hand column is passed as its parameter.
1 of 9
2. Reference Semantics Mystery
The following program produces 4 lines of output. Write the output below, as it would appear on the console.
mystery(a, y, x);
System.out.println(x + " " + y + " " + Arrays.toString(a));
x = y - 1;
mystery(a, y, x);
System.out.println(x + " " + y + " " + Arrays.toString(a));
}
2 of 9
3. Inheritance Mystery
Assume that the following classes have been defined:
public class Pen extends Sock { public class Book extends Sock {
public void method1() { public void method2() {
System.out.print("pen 1 "); System.out.print("book 2 ");
} super.method2();
} }
}
public class Lamp {
public void method1() { public class Sock extends Lamp {
System.out.print("lamp 1 "); public void method1() {
} System.out.print("sock 1 ");
method2();
public void method2() { }
System.out.print("lamp 2 ");
} public String toString() {
return "sock";
public String toString() { }
return "lamp"; }
}
}
Given the classes above, what output is produced by the following code?
Lamp[] elements = {new Book(), new Pen(), new Lamp(), new Sock()};
for (int i = 0; i < elements.length; i++) {
System.out.println(elements[i]);
elements[i].method1();
System.out.println();
elements[i].method2();
System.out.println();
System.out.println();
}
4. File Processing
Write a static method named wordStats that accepts as its parameter a Scanner holding a sequence of words and
that reports the total number of words and the average word length. For example, suppose the Scanner is scanning
an input source that contains the following words:
For the purposes of this problem, we will use whitespace to separate words. That means that some words include
punctuation, as in "be,". (This is the same definition that the Scanner uses for tokens.) For the input above, your
method should produce exactly the following output:
Total words = 10
Average length = 3.2
3 of 9
5. File Processing
Write a static method named flipLines that accepts as its parameter a Scanner for an input file and that writes to
the console the same file's contents with successive pairs of lines reversed in order. For example, if the input file
contains the following text:
The program should print the first pair of lines in reverse order, then the second pair in reverse order, then the third
pair in reverse order, and so on. Therefore your method should produce the following output to the console:
Notice that a line can be blank, as in the third pair. Also notice that an input file can have an odd number of lines, as
in the one above, in which case the last line is printed in its original position. You may not make any assumptions
about how many lines are in the Scanner.
6. Array Programming
Write a static method named minGap that accepts an integer array as a parameter and returns the minimum 'gap'
between adjacent values in the array. The gap between two adjacent values in a array is defined as the second value
minus the first value. For example, suppose a variable called array is an array of integers that stores the following
sequence of values.
The first gap is 2 (3 - 1), the second gap is 3 (6 - 3), the third gap is 1 (7 - 6) and the fourth gap is 5 (12 - 7). Thus, the
call of minGap(array) should return 1 because that is the smallest gap in the array. Notice that the minimum gap
could be a negative number. For example, if array stores the following sequence of values:
(3, 5, 11, 4, 8)
The gaps would be computed as 2 (5 - 3), 6 (11 - 5), -7 (4 - 11), and 4 (8 - 4). Of these values, -7 is the smallest, so it
would be returned.
This gap information can be helpful for determining other properties of the array. For example, if the minimum gap is
greater than or equal to 0, then you know the array is in sorted (nondecreasing) order. If the gap is greater than 0, then
you know the array is both sorted and unique (strictly increasing).
If you are passed an array with fewer than 2 elements, you should return 0.
4 of 9
7. Array Programming
Write a static method named longestSortedSequence that accepts an array of integers as a parameter and that
returns the length of the longest sorted (nondecreasing) sequence of integers in the array. For example, if a variable
named array stores the following values:
int[] array = {3, 8, 10, 1, 9, 14, -3, 0, 14, 207, 56, 98, 12};
then the call of longestSortedSequence(array) should return 4 because the longest sorted sequence in the array
has four values in it (the sequence -3, 0, 14, 207). Notice that sorted means nondecreasing, which means that the
sequence could contain duplicates. For example, if the array stores the following values:
Then the method would return 5 for the length of the longest sequence (the sequence 3, 5, 5, 5, 8). Your method
should return 0 if passed an empty array.
8. Critters
Write a class called Orca that extends the Critter class. The instances of the Orca class follow a pattern of
moving forward four times, then turning around, then moving back four times and turning around again so that they
return to their original position and direction. Each Orca is always either in moving-mode or in turning-mode. They
start out in moving-mode. While in moving-mode, they try to hop forward if possible until they have hopped four
times, at which point they switch into turning-mode. If it is not possible to hop while in moving-mode, an Orca
instead infects whatever is in front of it. When in turning-mode, the Orca turns left twice and then switches back to
moving-mode. Don't worry about the fact that if the Orca encounters a wall while in moving-mode, it gets stuck
trying to infect the wall indefinitely. The Orca displays itself as "M" while in moving-mode and as "T" while in
turning-mode. Its color should be the default color for critters.
5 of 9
9. Classes and Objects
6 of 9
Solutions
1.
Call Value Returned
int[] a1 = {8};
int result1 = arrayMystery(a1); 0
2.
2 3 [0, 0, 17, 0]
3 1 [0, 0, 17, 0]
1 0 [17, 0, 17, 0]
0 1 [17, 0, 17, 0]
3.
sock
sock 1 book 2 lamp 2
book 2 lamp 2
sock
pen 1
lamp 2
lamp
lamp 1
lamp 2
sock
sock 1 lamp 2
lamp 2
4.
public static void wordStats(Scanner input) {
int count = 0;
int sumLength = 0;
while (input.hasNext()) {
String next = input.next();
count++;
sumLength += next.length();
}
7 of 9
5.
public static void flipLines(Scanner input) {
while (input.hasNextLine()) {
String first = input.nextLine();
if (input.hasNextLine()) {
String second = input.nextLine();
System.out.println(second);
}
System.out.println(first);
}
}
6.
public static int minGap(int[] list) {
if (list.length < 2) {
return 0;
} else {
int min = list[1] - list[0];
for (int i = 2; i < list.length; i++) {
int gap = list[i] - list[i - 1];
if (gap < min) {
min = gap;
}
}
return min;
}
}
7.
public static int longestSortedSequence(int[] list) {
if (list.length == 0) {
return 0;
}
int max = 1;
int count = 1;
for (int i = 1; i < list.length; i++) {
if (list[i] >= list[i - 1]) {
count++;
} else {
count = 1;
}
8 of 9
8.
public class Orca extends Critter {
private int count;
public Orca() {
count = 0;
}
x = left;
y = top;
width = right - left;
height = bottom - top;
}
9 of 9