CS 145 Lecture 23 – Loops V
October 24, 2014 by Chris Johnson. Filed under cs145, fall 2014, lectures.
Agenda
- what ?s
- what does this do?
- blackboxes
- loops in other languages
TODO
- Share a quiz question before the quiz in Monday’s lab. Pretend that your question might actually appear in the quiz: make it short, direct, and on topic. Worth a 1/4 sheet.
What Does This Do?
public static String ?(String raw) {
String cooked = "";
for (int i = 0; i < raw.length(); ++i) {
if (Character.isLetterOrDigit(raw.charAt(i))) {
cooked += raw.charAt(i);
} else {
cooked += "_";
}
}
return cooked;
}
show
public static boolean ?(int n) {
for (int i = 2; i < n; ++i) {
if (n % i == 0) {
return false;
}
}
return true;
}
show
public static void ?() {
for (int r = 1; r <= 5; ++r) {
for (int c = r; c >= 0; --c) {
System.out.print(c);
}
System.out.println();
}
}
show
Blackboxes
Code
ForFor.java
package lecture1024;
public class ForFor {
public static void main(String[] args) {
mystery();
}
public static void mystery() {
for (int r = 1; r <= 5; ++r) {
for (int c = r; c >= 0; --c) {
System.out.print(c);
}
System.out.println();
}
}
}
Haiku
on routine:
You’re an NPC
It’s weather, Cubs, work, and sleep
It’s time to break
out
show