Key to CSE142 Final handout #24
Winter 2012
1. Expression Value
---------------------------------------------------
8.0 / 4 / 4 + 3 / 4 + 0.5 * 3 2.0
88 % 10 % 5/2 + 7 % 8 8
2 * 3 + 4 + " = " + 5 * 6 + 7 "10 = 307"
54 / 5 / 2.0 / 4 * 3 3.75
2 * (0.75 + 1.25) / 5 * 10 - 7/4 7.0
2. Original List Final List
--------------------------------------------------------------
{8} {8}
{6, 3} {6, 9}
{2, 4, 6} {2, 6, 12}
{1, 2, 3, 4} {1, 3, 6, 10}
{7, 3, 2, 0, 5} {7, 10, 12, 12, 17}
3. Inheritance. The output produced is as follows.
d c c d
d 1 c 1 c 1 d 1
b 2 c 2 a 2 a 2
4. Token-Based File Processing. One possible solution appears below.
public static void reportRunningSum(Scanner input) {
double sum = input.nextDouble();
double max = sum;
System.out.print("running sum = " + sum);
while (input.hasNextDouble()) {
sum += input.nextDouble();
System.out.print(" " + sum);
if (sum > max) {
max = sum;
}
}
System.out.println();
System.out.println("max sum = " + max);
}
5. Line-Based File Processing. Two possible solutions appear below.
public static int analyzeParagraphs(Scanner input) {
int max = 0;
while (input.hasNextLine()) {
String line = input.nextLine();
int count = 0;
while (!line.equals("")) {
count++;
line = input.nextLine();
}
System.out.println(count + "-line paragraph");
if (count > max) {
max = count;
}
}
return max;
}
public static int analyzeParagraphs(Scanner input) {
int max = 0;
int count = 0;
while (input.hasNextLine()) {
String line = input.nextLine();
if (line.equals("
")) {
System.out.println(count + "-line paragraph");
count = 0;
} else {
count++;
}
if (count > max) {
max = count;
}
}
return max;
}
6. Arrays. One possible solution appears below.
public static int numUnique(int[] list) {
if (list.length == 0) {
return 0;
} else {
int count = 1;
for (int i = 1; i < list.length; i++) {
if (list[i] != list[i - 1]) {
count++;
}
}
return count;
}
}
7. ArrayLists. One possible solution appears below.
public static void removeAdjacentMatches(ArrayList list) {
for (int i = 0; i < list.size() - 1; i++) {
if (list.get(i) == list.get(i + 1)) {
list.remove(i + 1);
i--;
}
}
}
8. Critters. One possible solution appears below.
public class Eagle extends Critter {
private int count;
private int max;
public Eagle() {
count = 0;
max = 1;
}
public Action getMove(CritterInfo info) {
count++;
if (count == 2 * max) {
count = 0;
max++;
}
if (info.getFront() == Neighbor.EMPTY) {
return Action.HOP;
} else if (info.getFront() == Neighbor.WALL) {
return Action.RIGHT;
} else {
return Action.INFECT;
}
}
public Color getColor() {
if (count < max) {
return Color.RED;
} else {
return Color.BLUE;
}
}
public String toString() {
return "<>";
}
}
9. Arrays. One possible solution appears below.
public static int[] reverseSublist(int[] list, int from, int to) {
int[] result = new int[to - from];
for (int i = 0; i < result.length; i++) {
result[i] = list[to - i - 1];
}
return result;
}
10. Programming. Two possible solutions appear below.
public static void removeZeros(int[] list) {
for (int i = list.length - 2; i >= 0; i--) {
if (list[i] == 0) {
for (int j = i; j < list.length - 1; j++) {
list[j] = list[j + 1];
}
list[list.length - 1] = 0;
}
}
}
public static void removeZeros(int[] list) {
int numNonZero = 0;
for (int i = 0; i < list.length; i++) {
if (list[i] != 0) {
list[numNonZero] = list[i];
numNonZero++;
}
}
for (int i = numNonZero; i < list.length; i++) {
list[i] = 0;
}
}