More Constructs
> Click or hit Control-Enter to run the code above
Q2 Review: Empty Arrays
boolean g(char[] x, char[] y) {
if (x.length != y.length) {
return false;
}
for (int i = 0; i < x.length; i++) {
if (x[i] != y[i]) {
return false;
}
}
return true;
}
What happens if the function above is called with two empty arrays?
> Click or hit Control-Enter to run the code above
Q2 Review: Javadoc
If you don’t write good Javadoc comments, which of the following will not happen?
Q2 Review: Problems
boolean t(String f, String g) {
for (int i = 0; i < f.length(); i++) {
if (f.charAt(i) != g.charAt(g.length() - i)) {
return false;
}
}
return true;
}
What are problems with the code above?
> Click or hit Control-Enter to run the code above
Q2 Review: Functions
What is not a reason to break your code into functions?
Q2 Review: Quickly
iterate: to say or do again or again and again
(Like in a for
loop.)
void printStuff() {
System.out.println("Stuff");
}
The do-while Loop
Remember the while
loop?
while (condition) {
// do something
}
It also has a friend:
do {
// do something
} while (condition);
What’s the difference?
Split Name
Imagine I have a person’s full name in a string in either one of the following two formats:
-
"First Last", or
-
"Last, First"
Write a function to split the name into two string values: one storing the first name and the second storing the last name.
What’s Our Algorithm?
> Click or hit Control-Enter to run the code above
Announcements
-
MP2 is out and due on Friday. Please get started!
-
The next set of Turing’s Craft exercises (TC6) are due tomorrow at midnight.
-
My office hours start today after class and continue MWF at 11AM in the lounge outside 0226. Please stop by to say hi, chat about the class and computer science in general, or if you have any questions. I look forward to meeting you!