CS 1: Lecture 11 – Method Mechanics
Dear students,
Sometimes Java is criticized for being verbose. But one of the great advantages of verbosity in a language is that a text reinforces itself. We may read a passage and not know every word, but there are often many clues lying in the context to help us determine their meaning. Java has these same advantages. We can look at a method and figure out a lot of things from how a values are operated upon.
To gain some sensitivity for reading this context, let’s complete several fill-in-the-blank exercises. Your task is to jot what goes in each blank of the following methods. One of the blanks is the method name. Pick something meaningful. The other blanks are not ambiguous. You should be able to deduce exactly what goes there.
- FIB #1
public static _______ _______(int n) { ______ z = n + ""; return z.startsWith("-"); }
- FIB #2
public static _______ _______(______ ______) { return text + " :)"; }
- FIB #3
public static _______ _______(______ ______) { System.out.printf("[%d]", foo); }
- FIB #4
public static _______ _______(______ ______) { return s.charAt(s.length() - 1); }
- FIB #5
public static _______ _______(int n) { _______ = "" + n; _______ = s.replace("0", ""); _______ = s.length() - t.length(); return d; }
- FIB #6
public static _______ _______(______ ________) { return g.nextInt(6) + g.nextInt(6) + 2; }
Our next task is to develop our mental model of how methods actually behave inside the computer. To make this happen, we will pretend each of the following programs is a screenplay, and each method is acted out by a member of the class. This individual will take notes about the data for which it is responsible, hand off values to methods it calls, and send data back to the method that calls it.
- 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; } }
- Tax
public class Tax { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("> "); double amount = in.nextDouble(); double cost = cost(amount); System.out.println(cost); } public static double cost(double price) { double y = x + tax(x, 0.05); return y; } public static double tax(double price, double rate) { double tax = price * rate; return tax; } }
- 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; } }
- Growth
public class Growth { public static void main(String[] args) { int x = 10; x = dubs(x); x = dubs(x); x = trips(x); System.out.println(x); } public static int dubs(int y) { return y + y; } public static int trips(int y) { int x = y + y + y; return x; } }
- 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; } }
- Adding
public class Adding { public static void main(String[] args) { System.out.println(add(4, 'x', 'o')); } public static String add(int n, char a, char b) { String s = "" + a + b + a + b + a; return s; } }
- Thirty
public class Thirty { public static void main(String[] args) { int x = 4; x = thirty(x); System.out.println(x); } public static void thirty(int n) { n += 30; /* same as n = n + 30 */ } }
Here’s your TODO list for next time:
- Keep chiseling away at homework 2. Expect trouble and confusion, which are the terrain of learning, and give yourself enough time to overcome them.
See you next class!
P.S. It’s time for a haiku!
f on his breakup
“I can’t function anymore…”
“I want my x back!”