Key to CSE142 Midterm, Winter 2006 handout #22
1. Expression Value
---------------------------------------------
1 * 2 + 6 / 3 4
12 % (14 / 5) 0
4 + "5 + 6" + 7 "45 + 67"
5 / 2+3 / 2.0 3.5
(int) 4.8 + 5 9
2. Parameter Mystery. The program produces the following output.
four, three, two
five, two, 2
three, fourfive, two4
3. Method Call Output Produced
------------------------------------------
mystery(0); 1 0 0
mystery(3); 2 2 1
mystery(5); 3 3 3
mystery(15); 3 13 3
mystery(10032346); 4 10032343 6
4. (Note: the last 2 columns are swapped on some exams)
x <= 0 b > 0 a == 6
+---------------------+---------------------+---------------------+
Point A | SOMETIMES | NEVER | NEVER |
+---------------------+---------------------+---------------------+
Point B | SOMETIMES | NEVER | NEVER |
+---------------------+---------------------+---------------------+
Point C | NEVER | SOMETIMES | SOMETIMES |
+---------------------+---------------------+---------------------+
Point D | NEVER | ALWAYS | SOMETIMES |
+---------------------+---------------------+---------------------+
Point E | ALWAYS | SOMETIMES | SOMETIMES |
+---------------------+---------------------+---------------------+
5. Three possible solutions appear below.
public static void printElapsed(int startMin, int startSec,
int endMin, int endSec) {
int startTotalSeconds = 60*startMin + startSec;
int endTotalSeconds = 60*endMin + endSec;
int elapsedTotalSeconds = endTotalSeconds - startTotalSeconds;
int elapsedMin, elapsedSec;
elapsedMin = elapsedTotalSeconds / 60;
elapsedSec = elapsedTotalSeconds % 60;
System.out.print(elapsedMin + ":");
if (elapsedSec < 10) {
System.out.print("0");
}
System.out.println(elapsedSec);
}
public static void printElapsed(int startMin, int startSec,
int endMin, int endSec) {
int elapsedMin, elapsedSec;
if (endSec >= startSec) {
elapsedSec = endSec - startSec;
elapsedMin = endMin - startMin;
} else {
elapsedSec = endSec - startSec + 60;
elapsedMin = endMin - startMin - 1;
}
System.out.print(elapsedMin + ":");
if (elapsedSec < 10) {
System.out.print("0");
}
System.out.println(elapsedSec);
}
public static void printElapsed(int startMin, int startSec,
int endMin, int endSec) {
int elapsedMin = endMin - startMin;
int elapsedSec = endSec - startSec;
if (elapsedSec < 0) {
elapsedSec += 60;
elapsedMin--;
}
System.out.print(elapsedMin + ":");
if (elapsedSec < 10) {
System.out.print("0");
}
System.out.println(elapsedSec);
}
6. Three possible solutions appears below.
public static void walkStairs() {
Random r = new Random();
int currentStep = 5;
int numSteps = 0;
System.out.println("Stair-step = " + currentStep);
while ((currentStep != 1) && (currentStep != 9)) {
if (r.nextInt(2) == 0) {
currentStep++;
} else {
currentStep--;
}
System.out.println("Stair-step = " + currentStep);
numSteps++;
}
System.out.println("Time to reach goal: " + numSteps + " steps");
}
public static void walkStairs() {
Random r = new Random();
int currentStep = 5;
int numSteps = 0;
System.out.println("Stair-step = " + currentStep);
while ((currentStep != 1) && (currentStep != 9)) {
currentStep += 2*r.nextInt(2) - 1;
System.out.println("Stair-step = " + currentStep);
numSteps++;
}
System.out.println("Time to reach goal: " + numSteps + " steps");
}
public static void walkStairs() {
Random r = new Random();
int currentStep = 5;
int numSteps = 0;
do {
currentStep += 2*r.nextInt(2) - 1;
numSteps++;
System.out.println("Stair-step = " + currentStep);
} while ((currentStep != 1) && (currentStep != 9));
System.out.println("Time to reach goal: " + numSteps + " steps");
}
7. Two possible solutions appear below.
public static int longestSequence(String s) {
int max = 1, count = 1;
for (int i=1; i max) {
// yes it is, so update the max sequence length so far
max = count;
}
count = 1;
} else {
// no! we're still in the old sequence, so update length
count++;
}
}
// was the last sequence in the string the longest?
if (count > max) {
max = count;
}
return max;
}
public static int longestSequence(String s) {
int max = 0, count = 1;
char last = s.charAt(0);
for (int i=1; i max) {
// yes it is, so update the max sequence length so far
max = count;
}
count = 1;
last = s.charAt(i);
} else {
// no! we're still in the old sequence, so update length
count++;
}
}
// was the last sequence in the string the longest?
if (count > max) {
max = count;
}
return max;
}