Key to CSE142 Sample Final handout #30
1. Expression Value
---------------------------------------------
4.5/3/2 + 1 1.75
57 % 10/3 + 1.25 * 4 7.0
5 * 6/4 % 3 - 23/(14 % 6) -10
2 + 3 * (4 + 3 * (18 % 4)) 32
8 - 4 + "17" + 7.5 * 2 "41715.0"
2. List Value returned
--------------------------------------
(8) 0
(14, 7) 1
(7, 1, 3, 2, 0, 4) 3
(10, 8, 9, 5, 6) 2
(8, 10, 8, 6, 4, 2) 4
3. Polymorphism. The output produced is as follows.
sock
sock 1
book 2
sock
pen 1
lamp 2
lamp
lamp 1
lamp 2
sock
sock 1
lamp 2
4. Token-Based File Processing. One possible solution appears below.
public static void processData(Scanner input) {
int count = 0;
int sumLength = 0;
while (input.hasNext()) {
String next = input.next();
count++;
sumLength += next.length();
}
double average = (double)sumLength/count;
System.out.println("Total words = " + count);
System.out.println("Average length = " + average);
}
5. Line-Based File Processing. One possible solution appears below.
public static void processFile(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. Arrays. One possible solution appears below.
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. ArrayLists. One possible solution appears below.
public static void markLength4(ArrayList list) {
int index = 0;
while (index < list.size()) {
String next = (String)list.get(index);
if (next.length() == 4) {
list.add(index, "****");
index += 2;
} else
index++;
}
}
8. Critters. One possible solution appears below.
public class Quail implements Critter {
private int myPhase; // 1 for N, 2 for W, 3 for E, 4 for S
private int myMove; // where in phase
private int myLength;
public Quail() {
myPhase = 1;
myMove = 0;
}
public char getChar() {
return 'Q';
}
public int getMove() {
myMove++;
// pick length if at beginning of cycle
if (myPhase == 1 && myMove == 1)
if (Math.random() < 0.5)
myLength = 10;
else
myLength = 20;
int direction;
if (myPhase == 1)
direction = NORTH;
else if (myPhase == 2)
direction = WEST;
else if (myPhase == 3)
direction = EAST;
else // myPhase == 4
direction = SOUTH;
if (myMove == myLength) {
myPhase++;
myMove = 0;
if (myPhase == 5)
myPhase = 1;
}
return direction;
}
}
9. Arrays. One possible solution appears below.
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;
if (count > max)
max = count;
}
return max;
}
10. Programming. One possible solution appears below.
public static int numSame(int[] list1, int[] list2) {
int count = 0;
int index1 = 0;
int index2 = 0;
while (index1 < list1.length && index2 < list2.length)
if (list1[index1] == list2[index2]) {
count++;
index1++;
index2++;
} else if (list1[index1] < list2[index2])
index1++;
else // list1[index1] > list2[index2]
index2++;
return count;
}
The following solution would get 4 points.
public static int numSame(int[] list1, int[] list2) {
int count = 0;
for (int i = 0; i < list1.length; i++)
for (int j = 0; j < list2.length; j++)
if (list1[i] == list2[j])
count++;
return count;
}