Key to CSE142 Final handout #22
Winter 2010
1. Expression Value
---------------------------------------------------
2 + 3 * (2 + 3) + 17/4 21
67 % 10 % 5 * 6 - 3 % 6 9
7.5 * 2 + 19/2/2.0 - 0.5 - 1/2 19.0
4 + 2 * 2 + "2 * 2" + 2 * 2 + 4 "82 * 244"
43/4/3 * 5.0/2 + 5/2 9.5
2. Original List Final List
--------------------------------------------------------------
{8, 4} {8, 4}
{20, 30, 40} {20, 30, 10}
{1, 2, 3, 4, 5} {1, 2, 1, 3, 2}
{5, 5, 5, 5} {5, 5, 0, 5}
{10, 20, 30, 40, 50} {10, 20, 10, 30, 20]
3. Polymorphism. The output produced is as follows.
d a d b
c 1 a 1 d 1 c 1
d 2 d 2 d 2 d 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. 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. Two possible solutions appear below.
public static boolean sameGap(int[] list) {
if (list.length >= 2) {
int gap = Math.abs(list[0] - list[1]);
for (int i = 2; i < list.length; i++) {
int gap2 = Math.abs(list[i] - list[i - 1]);
if (gap != gap2) {
return false;
}
}
}
return true;
}
public static boolean sameGap(int[] list) {
for (int i = 0; i < list.length - 2; i++) {
int gap1 = Math.abs(list[i] - list[i + 1]);
int gap2 = Math.abs(list[i + 1] - list[i + 2]);
if (gap1 != gap2) {
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 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 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;
}