Key to CSE142 Sample Final, Summer 2015
1. Expression Value
---------------------------------------------------
38 % 10 / 3 + 5/3 3
23.0 / 2 - 3 * 0.25 10.75
8 - 3 + "=" + 8 / 3 + 8 + 3 "5=283"
(34 / 10 * 2.0 + 5) / 2 5.5
5 % 8 + 8 % 5 - 17 % 4 7
2. Original List Final List
--------------------------------------------------------------
{2, 3, 1} {2, 1, 1}
{2, 6, 5, 2} {2, 4, 1, 2}
{3, 9, 7, 9} {3, 6, 1, 9}
{2, 4, 5, 6, 8} {2, 2, 3, 3, 8}
{1, 5, 8, 4, 8, 9} {1, 4, 4, 4, 4, 9}
3. Inheritance. The output produced is as follows.
b d d b
b 1 a 1 b 1 b 1
c 2 a 2 c 2 b 2
4. Token-Based File Processing. One possible solution appears below.
public static void reportRunningSum(Scanner input) {
double sum = input.nextDouble();
double max = sum;
System.out.print("running sum = " + sum);
while (input.hasNextDouble()) {
sum += input.nextDouble();
System.out.print(" " + sum);
if (sum > max) {
max = sum;
}
}
System.out.println();
System.out.println("max sum = " + max);
}
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 void switchPairs(int[] list) {
for (int i = 0; i < list.length - 1; i += 2) {
int temp = list[i];
list[i] = list[i + 1];
list[i + 1] = temp;
}
}
7. ArrayLists. Two possible solutions appear below.
public static void oddsToBack(ArrayList list) {
int lastEven = -1;
for (int i = 0; i < list.size(); i++) {
if (list.get(i) % 2 == 0) {
int n = list.remove(i);
lastEven++;
list.add(lastEven, n);
}
}
}
public static void oddsToBack(ArrayList list) {
int negatives = 0;
for (int i = 0; i < list.size() - negatives; i++) {
if (list.get(i) % 2 != 0) {
int n = list.remove(i);
list.add(n);
i--;
negatives++;
}
}
}
8. Critters. One possible solution appears below.
public class Iguana extends Critter {
private Random r;
private String display;
private int count;
public Iguana() {
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[] splitPairs(int[] list) {
int half = list.length / 2;
int[] result = new int[list.length];
for (int i = 0; i < list.length; i++) {
if (i % 2 == 0) {
result[i / 2] = list[i];
} else if (list.length % 2 == 0) {
result[i / 2 + half] = list[i];
} else {
result[i / 2 + half + 1] = list[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;
}
Stuart Reges
Last modified: Fri Sep 16 16:16:07 PDT 2011