Key to CSE142B Final, Winter 2013 handout #26
1. Expression Value
---------------------------------------------------
43 % 15 / 3 + 15 / 2 11
6.2 * 5 / 10 + 3.5 6.6
6 * 2.5 / 4 + (2.3 + 2.7) / 4 5.0
"18" + 3 * 4 + (8 + 5) "181213"
59 % 10 / (2 + 2) * 2.5 / 2 2.5
2. Original List Final List
--------------------------------------------------------------
{8, 3} {9, 3}
{2, 5, 4} {3, 5, 4}
{3, 4, 5, 7} {4, 4, 6, 7}
{2, 5, 4, 2} {3, 5, 5, 2}
{4, 5, 7, 9, 3} {5, 5, 8, 9, 3}
3. Inheritance. The output produced is as follows.
a c a c
a 1 c 1 a 1 d 1
b 2 c 2 c 2 c 2
4. Token-Based File Processing. One possible solution appears below.
public static void switchData(Scanner input) {
String label = input.next();
System.out.print(label);
while (input.hasNextInt()) {
int n1 = input.nextInt();
if (input.hasNextInt()) {
int n2 = input.nextInt();
System.out.print(" " + n2);
}
System.out.print(" " + n1);
}
System.out.println();
}
5. Line-Based File Processing. One possible solution appears below.
public static void reportBlankLines(Scanner input) {
int line = 0;
int count = 0;
while (input.hasNextLine()) {
String text = input.nextLine();
line++;
if (text.length() == 0) {
System.out.println("line " + line + " is blank");
count++;
}
}
System.out.println("total blank lines = " + count);
}
6. Arrays. One possible solution appears below.
public static boolean isPalindrome(int[] list) {
for (int i = 0; i < list.length / 2; i++) {
if (list[i] != list[list.length - 1 - i]) {
return false;
}
}
return true;
}
7. ArrayLists. Two possible solutions appear below.
public static void split(ArrayList list) {
for (int i = 0; i < list.size(); i += 2) {
int n = list.get(i);
list.set(i, n / 2 + n % 2);
list.add(i + 1, n / 2);
}
}
public static void split(ArrayList list) {
for (int i = 0; i < list.size(); i += 2) {
int n = list.remove(i);
list.add(i, n / 2 + n % 2);
list.add(i + 1, n / 2);
}
}
8. Critters. One possible solution appears below.
public class Lizard extends Critter {
private int count;
private boolean increasing;
public Lizard() {
count = 1;
increasing = true;
}
public Action getMove(CritterInfo info) {
if (increasing) {
count++;
if (count == 10) {
increasing = false;
}
} else {
count--;
if (count == 1) {
increasing = true;
}
}
if (info.getFront() == Neighbor.OTHER) {
return Action.INFECT;
} else if (info.getFront() == Neighbor.EMPTY) {
return Action.HOP;
} else
return Action.RIGHT;
}
public Color getColor() {
return Color.GREEN;
}
public String toString() {
return "<" + count + ">";
}
}
9. Arrays. One possible solution appears below.
public static int[] interleave(int[] list1, int[] list2) {
int min = Math.min(list1.length, list2.length);
int[] result = new int[2 * min];
for (int i = 0; i < min; i++) {
result[2 * i] = list1[i];
result[2 * i + 1] = list2[i];
}
return result;
}
10. Programming. Two possible solutions appear below.
public static int count(String target, String source) {
target = target.toUpperCase();
source = source.toUpperCase();
int count = 0;
for (int i = 0; i < source.length(); i++) {
if (source.substring(i).startsWith(target)) {
count++;
}
}
return count;
}
public static int count(String target, String source) {
int count = 0;
for (int i = 0; i < source.length() - target.length() + 1; i++) {
String sub = source.substring(i, i + target.length());
if (sub.equalsIgnoreCase(target)) {
count++;
}
}
return count;
}
Stuart Reges
Last modified: Fri Sep 16 16:16:07 PDT 2011