Whoever you are, you need practice with methods. They are a concept that you probably didn’t encounter in your high school math classes, and you have no prior knowledge on which to build here. So, don’t be too hard on yourself if they don’t click immediately. Today, then, we devote our entire time to inspecting some methods and figure out their mechanics. Our tool for understanding them is memory diagrams. We will draw out how the state of the computer’s RAM changes as our methods get called. Let’s get going with these problems:
publicclassGrowth {
publicstaticvoid main(String[] args) {
int x = 10;
x = double(x);
x = double(x);
x = triple(x);
System.out.println(x);
}
publicstaticintdouble(int y) {
return y + y;
}
publicstaticint triple(int y) {
int x = y + y + y;
return x;
}
}
publicclassWolves {
publicstaticvoid main(String[] args) {
int x = 4;
x = wolves(x);
}
publicstaticvoid wolves(int n) {
n += 30; /* same as n = n + 30 */
}
}
publicclassStringLength {
publicstaticvoid main(String[] args) {
String text = "abc";
int length = length(text);
System.out.println(length);
}
publicstaticint length(String s) {
/* s is 'a' + "bc", so the length is 1 + length("bc") */return1 + s.substring(1);
}
}
Here’s your TODO list to complete before next class:
Read chapter 3 through section 3.2—and also section 3.4.
On a 1/4 sheet, write a method frankenstring that takes two String parameters as input and returns a new String that is the first half of one followed by the second half of the other. Is this method useful? Probably not. But an army does not train by going to war.