Key to CSE142 Final, Spring 2016 handout #22
1. Reference Mystery. The program produces the following output.
3 [2, 3, 7]
1 [2, 3, 7]
17 7
2. Original List Final List
--------------------------------------------------------------
{0, 2, 3} {0, 2, 5}
{1, 1, 4, 5} {1, 1, 3, 7}
{3, 1, 2, 9, 5} {3, 1, 3, 7, 5}
{5, 1, 1, 1, 8} {5, 1, 3, 7, 8}
{2, 1, 1, 2, 3, 4} {2, 1, 3, 7, 3, 7}
3. Inheritance. The output produced is as follows.
d a d d
b 1 b 1 d 1 c 1
b 2 b 2 d 2 d 2
4. Token-Based File Processing. One possible solution appears below.
public static void tallyScores(Scanner input) {
while (input.hasNext()) {
String name = input.next();
System.out.print(name + ":");
int points = 0;
int count = 0;
while (input.hasNextInt()) {
int nextPoints = input.nextInt();
points += nextPoints;
count++;
System.out.print(" " + nextPoints);
}
System.out.println();
double average = (double) points / count;
System.out.println("average = " + average);
}
}
5. Line-Based File Processing. One possible solution appears below.
public static void switchNames(Scanner input) {
while (input.hasNextLine()) {
String line = input.nextLine();
if (line.equals("name:")) {
String line2 = input.nextLine();
int commaIndex = line2.indexOf(",");
String last = line2.substring(0, commaIndex);
String first = line2.substring(commaIndex + 2);
System.out.println(first + " " + last);
} else {
System.out.println(line);
}
}
}
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 cloneOddsRemoveEvens(ArrayList numbers) {
for (int i = 0; i < numbers.size(); i++) {
int n = numbers.get(i);
if (n % 2 == 0) {
numbers.remove(i);
i--;
} else {
numbers.add(i, n);
i++;
}
}
}
8. Critters. One possible solution appears below.
public class Spider extends Critter {
private boolean south;
private int moves;
public Action getMove(CritterInfo info) {
if (info.getFront() == Neighbor.OTHER) {
return Action.INFECT;
} else if (south && info.getDirection() != Direction.SOUTH) {
return Action.LEFT;
} else if (!south && info.getDirection() != Direction.NORTH) {
return Action.LEFT;
} else if (info.getFront() == Neighbor.EMPTY) {
moves++;
return Action.HOP;
} else {
south = !south;
moves = 0;
return Action.LEFT;
}
}
public Color getColor() {
return Color.GREEN;
}
public String toString() {
if (south) {
return "S-" + moves;
} else {
return "N-" + moves;
}
}
}
9. Arrays. One possible solution appears below.
public static int[] insert(int[] list1, int[] list2, int index) {
int[] result = new int[list1.length + list2.length];
for (int i = 0; i < index; i++) {
result[i] = list1[i];
}
for (int i = 0; i < list2.length; i++) {
result[index + i] = list2[i];
}
for (int i = index; i < list1.length; i++) {
result[list2.length + i] = list1[i];
}
return result;
}
10. Programming. One possible solution appears below.
public static boolean samePattern(String s1, String s2) {
if (s1.length() != s2.length()) {
return false;
}
for (int i = 0; i < s1.length(); i++) {
for (int j = i + 1; j < s1.length(); j++) {
if (s1.charAt(i) == s1.charAt(j) &&
s2.charAt(i) != s2.charAt(j)) {
return false;
}
if (s2.charAt(i) == s2.charAt(j) &&
s1.charAt(i) != s1.charAt(j)) {
return false;
}
}
}
return true;
}
Stuart Reges
Last modified: Fri Sep 16 16:16:07 PDT 2011