Key to CSE142 Final, Spring 2022 handout #20
1. Reference Mystery. The program produces the following output.
3 [2, 3, 7]
1 [2, 3, 7]
17 7
2. Original List Final List
--------------------------------------------------------------
{1} {1}
{2, 5, 9} {2, 7, 9}
{2, 3, 7, 2} {2, 5, 2, 2}
{2, 4, 5, 4, 3} {2, 6, -1, 3, 3}
{5, 1, 8, 4, 8, 4} {5, 6, 2, 6, 2, 4}
3. Inheritance. 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. One possible solution appears 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;
}
6. Arrays. One possible solution appears below.
public static boolean isAllPairs(int[] list) {
if (list.length % 2 == 1) {
return false;
}
for (int i = 1; i < list.length; i += 2) {
if (list[i] != list[i - 1]) {
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 Sponge extends Critter {
private int dashCount;
private int turnCount;
public Sponge() {
dashCount = 1;
turnCount = 0;
}
public Action getMove(CritterInfo info) {
if (info.getFront() == Neighbor.OTHER) {
dashCount++;
return Action.INFECT;
} else if (info.getFront() == Neighbor.EMPTY) {
return Action.HOP;
} else {
dashCount = Math.max(dashCount - 1, 1);
turnCount++;
if (turnCount % 3 == 1) {
return Action.LEFT;
} else {
return Action.RIGHT;
}
}
}
public Color getColor() {
return Color.YELLOW;
}
public String toString() {
String result = "[";
for (int i = 0; i < dashCount; i++) {
result = result + "-";
}
return result + "]";
}
}
9. Arrays. One possible solution appears below.
public static int[] surroundWithN(int[] list1, int[] list2, int n) {
int[] result = new int[list1.length + 2 * n];
for (int i = 0; i < n; i++) {
result[i] = list2[i];
}
for (int i = 0; i < list1.length; i++) {
result[i + n] = list1[i];
}
for (int i = 0; i < n; i++) {
result[list1.length + n + i] = list2[i];
}
return result;
}
10. Programming. One possible solution appears below.
public static String acronym(String s) {
boolean inWord = false;
s = s.toUpperCase();
String result = "";
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if (ch == ' ' || ch == '-') {
inWord = false;
} else if (!inWord) {
inWord = true;
result += ch;
}
}
return result;
}