CS 145 Lecture 11 – Methods x4
Dear students,
Let’s do one more of the exercises from last time!
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; } }
This problem introduces the ideas of promotion, upcasting, and downcasting. We will do a little science experiment to illustrate when we need to cast values between types.
Let’s order the types from greatest (most general) to least (least general):
double
float
long
int
short
char
byte
The rule of promotion is this: when an operator combines two operands of a lesser type and greater type, the result is always a value of the greater type.
double x = 45;
int i = 101; double x = i;
short s = 0; int i = s;
int i = 899; long j = 900; int k = i + j;
byte b = 127; String s = b + " <- result";
public static void main(String[] args) { short s = randomEven100(); } public static int randomEven100() { Random g = new Random(); return g.nextInt(51) * 2; }
public static void main(String[] args) { int i = 54; labeledPrint("i", i); } public static void labeledPrint(String label, long l) { System.out.printf("%s: %d%n", label, l); }
public static void main(String[] args) { int i = getInputInt(); int j = getRandomInt(); } public static int getInputInt() { Scanner in = new Scanner(); return in; } public static int getRandomInt() { Random generator = new Random(); return in; }
One of the nice things about this class is that we never set aside anything that we discuss. In chapter n, we still need all the concepts we discussed in chapters 1 through n – 1. That said, we close out our official focus on methods today. We have seen the Computer as Calculator, a lover of number crunching. We have seen the Computer as Chef, organizing its tasks into self-contained recipes. Next week we see the Computer as Philosopher, seeking truth from our data.
But that’s next week. Today, let’s have a look at some more methods. We start with some 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.
public static _______ _______(int n) { ______ z = n + ""; return z.startsWith("-"); }
public static _______ _______(______) { return text + " :)"; }
public static _______ _______(______) { n = -n; }
public static _______ _______(______) { return s.charAt(s.length() - 1); }
public static _______ _______(int n) { _______ = "" + n; _______ = s.replace("0", ""); _______ = s.length() - t.length(); return d; }
We next turn to a problem that one of you asked on a quarter sheet. Is there anything like charAt
for integers? Not exactly, but there’s nothing stopping us from writing our own method to accomplish this task!
Let me share with you a quotation from Maurice Wilkes, one of the developers of the first computers:
By June 1949 people had begun to realize that it was not so easy to get programs right as at one time appeared. I well remember when this realization first came on me with full force. The EDSAC was on the top floor of the building and the tape-punching and editing equipment one floor below… It was on one of my journeys between the EDSAC room and the punching equipment that “hesitating at the angles of stairs” the realization came over me with full force that a good part of the remainder of my life was going to be spent in finding errors in my own programs.
That “hesitating at the angles of stairs” is Wilkes alluding to the poetry of T.S. Eliot.
If we are going to be an inventor—even an inventor of software—we must care to do it right. We must make sure our software does its task well. To ease this burden, we’ll talk a little bit about test-driven development and write a primitive testing framework.
See you next class!