CS 145 Lecture 10 – Methods and Memory
Dear students,
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:
Greeter
public class Greeter { public static void main(String[] args) { String s = "Hello, Merle!"; } public static void greet(String s) { System.out.println(s); } }
Rectangler
public class Rectangler { public static void main(String[] args) { String dimensions = "50x100"; Scanner in = new Scanner(dimensions); in.useDelimiter("x"); int width = in.nextInt(); int height = in.nextInt(); getRectangleArea(width * height); } public static int getRectangleArea(int width, int height) { return width * height; } }
RectanglerRedux
public class RectanglerRedux { public static void main(String[] args) { int w = 20; int h = 10; int perimeter = getRectanglePerimeter(w, h); System.out.println(perimeter); } public static int getRectanglePerimeter(int width, int height) { width = 30; height = 70; return 2 * width + 2 * height; } }
Ellipsis
public class Ellipsis { public static void main(String args) { String ellipsis = dots(3); System.out.println(ellipsis); } public static String dots(int n) { String lotsOfDots = "............................................"; String someDots = lotsOfDots.substring(0, n); return someDots; } }
Growth
public class Growth { public static void main(String[] args) { int x = 10; x = double(x); x = double(x); x = triple(x); System.out.println(x); } public static int double(int y) { return y + y; } public static int triple(int y) { int x = y + y + y; return x; } }
Half
public class Half { public static void main(String args) { String message = "Cursive is character-building"; int length = message.length(); System.out.println(halve(length)); } public static double halve(double x) { return x / 2; } }
Unnamed
public class Unnamed { public static void main(String args) { int n = 1834523423; int x = unnamed(n); } public static int unnamed(int z) { String zAsString = "" + z; String y = zString.replace("2", ""); int a = zAsString.length() - y.length(); return a; } }
Middle
public class Middle { public static void main(String args) { char a = 'a'; char e = 'e'; char middle = average(a, e); System.out.println(middle); } public static char average(char c1, char c2) { return (c1 + c2) / 2; } }
Negater
public class Negater { public static void main(String[] args) { int n = 10; negate(n); System.out.println(n); } public static int negate(int n) { n = -n; } }
Wolves
public class Wolves { public static void main(String[] args) { int x = 4; x = wolves(x); } public static void wolves(int n) { n += 30; /* same as n = n + 30 */ } }
Hide
public class Hide { public static void main(String[] args) { String puzzle = "Respect covers the empty hole where love should be."; puzzle = hide(puzzle, 'e'); puzzle = hide(puzzle, 't'); System.out.println(puzzle); } public static String hide(String message, char letter) { message.replace(letter, '_'); return message; } }
Mousing
public class Mousing { private static int x = 0; public static void main(String[] args) throws AWTException { Robot roboto = new Robot(); int x = 0; shift(roboto); shift(roboto); shift(roboto); } public static int shift(Robot xj9000) { x = x + 10; xj9000.mouseMove(x, 0); xj9000.delay(3000); } }
StringLength
public class StringLength { public static void main(String[] args) { String text = "abc"; int length = length(text); System.out.println(length); } public static int length(String s) { /* s is 'a' + "bc", so the length is 1 + length("bc") */ return 1 + 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 twoString
parameters as input and returns a newString
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.
See you next class!
Sincerely,