Key to CSE142A Final, Winter 2013 handout #25
1. Expression Value
---------------------------------------------------
17 / 3 + 55 % 20 / 2 12
19.0 / 2 + 2 * 2.25 14.0
2 + 2 + "." + 2 + 2 + 4 * 5 "4.2220"
73 / 10 * 4 / 3 / 2.0 * 3 13.5
3 / 2 * (1.5 + 2.5) + 98 % 5 / 2 5.0
2. Original List Final List
--------------------------------------------------------------
{2, 5, 8} {2, 5, 10}
{3, 4, 7, 5} {3, 4, 10, 5}
{2, 4, 6, 2, 6} {2, 4, 8, 2, 14}
{4, 5, 8, 9, 3} {4, 5, 12, 9, 15}
{1, 2, 8, 5, 10, 5, 4} {1, 2, 9, 5, 19, 5, 23}
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 printStrings(Scanner input) {
while (input.hasNextInt()) {
int times = input.nextInt();
String word = input.next();
for (int i = 0; i < times; i++) {
System.out.print(word);
}
System.out.println();
}
}
5. Line-Based File Processing. One possible solution appears below.
public static void flipLines(Scanner input) {
while (input.hasNextLine()) {
String first = input.nextLine();
if (input.hasNextLine()) {
String second = input.nextLine();
System.out.println(second);
}
System.out.println(first);
}
}
6. Arrays. One possible solution appears below.
public static void minToFront(int[] list) {
if (list.length > 0) {
int min = 0;
for (int i = 0; i < list.length; i++) {
if (list[i] < list[min]) {
min = i;
}
}
int temp = list[0];
list[0] = list[min];
list[min] = temp;
}
}
7. ArrayLists. Two possible solutions appear below.
public static void reverse3(ArrayList list) {
for (int i = 0; i < list.size() - 2; i += 3) {
int n1 = list.get(i);
int n3 = list.get(i + 2);
list.set(i, n3);
list.set(i + 2, n1);
}
}
public static void reverse3(ArrayList list) {
for (int i = 0; i < list.size() - 2; i += 3) {
list.add(i, list.remove(i + 2));
list.add(i + 2, list.remove(i + 1));
}
}
8. Critters. One possible solution appears below.
public class Raccoon extends Critter {
private Random r;
private String display;
private int count;
public Racoon() {
r = new Random();
display = "<->";
}
public Action getMove(CritterInfo info) {
count++;
if (info.getFront() == Neighbor.OTHER) {
display = "";
return Action.INFECT;
} else {
int flip = r.nextInt(2);
if (flip == 0) {
display = "";
return Action.LEFT;
} else {
display = "";
return Action.RIGHT;
}
}
}
public Color getColor() {
if (count % 2 == 0) {
return Color.BLUE;
} else {
return Color.RED;
}
}
public String toString() {
return display;
}
}
9. Arrays. One possible solution appears below.
public static int[] collapse(int[] list) {
int[] result = new int[list.length / 2 + list.length % 2];
for (int i = 0; i < list.length; i++)
result[i / 2] += list[i];
return result;
}
10. Programming. Two possible solutions appear 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;
}
Stuart Reges
Last modified: Fri Sep 16 16:16:07 PDT 2011