******************************************************************************* 0) Last time- string/number conversions, I/O, and Math class - with group, do exercise (from last time) 1) Administrative - hw: codelab Wed, A1 out - review 1.1-1.3, read 4.1-4.3 (no presentations) 2) Split in groups and go over 4.4 and 4.7 3) Highlights of material A) Methods: sets of instructions you can use by "calling" the method - where do you define them? -inside classes - how do you call one from inside the same class? -this.name(...) - "this" is optional and means "this object" - how do you call one from inside a different class? - obj.name(...) or Class.name(...) if static - when you call, computer jumps over to that code, then jumps back B) How do you define a method? visibility [static] returnType name(parameter list) { stmt1; stmt2; } - what does public mean? -can call that method from anywhere - what does private mean? -can only call from within same class - what does static mean? -it's a class method, not linked w/ obj - what do parameters mean? -info it expects to get when called - what does return type mean? -info it promises to pass back C) Inside a method: local variables - variables can either be data members or local variables - data members are permanent to the object; locals aren't - declared inside a method and only exist inside that method - (anything you declare in main - all vars you've worked with) - looking at class, realize that it's not linear code, so even if a var appears higher up it may not exist lower down - you can use data members inside method (do not declare again!) - what if you declare one with same name? local, overrides - can get to them again using this.name (even if private) - parameters are local variables too - "scope" of a variable: where it exists D) Passing and returning - IMPORTANT: what's printing? what's returning? DIFFERENT! - arguments are assigned to corresponding parameters - pass by value: values are copied, no link afterwards int x = 1; public int addOne(int i) { addOne(x); i = i + 1; x = addOne(x); return i; } * first call: nothing gets changed! (i disappears, x same) - imagine addOne(5); (number 5 cannot change) * second call: assigning return value, so now it's changed * parameters are local variables- can assign to them * does it matter what the parameter name is? - no, but it can override data members - return is by value as well: value replaces method call expression - the return statement: by itself, with expression, stops execution - "method composition"- another term for nesting calls - in diagram: parameter types in parens, return type after E) In memory- stack frames (just FYI) - object data held in heap - area called the stack holds local scratch space for methods - each one has a chunk called its stack frame - contains space for params, local vars - when it returns, return value is copied and frame disappears - local vars are truly gone, but data members stay in object F) Questions?? 4) Exercises- fill in main class as we go A) Write a method that takes 3 ints and returns the average as a double. public double average(int a, int b, int c) { return (double)(a+b+c)/3; } B) Write a method that takes a prompt and returns a string of input. Make it static, b/c it won't use any instance data to operate. public static String readInput(String prompt) throws IOException { System.out.print(prompt); BR buf = new BR(new ISR(System.in)); return buf.readLine(); } C) Write a method that takes a prompt and returns an integer from input. It should not duplicate code from before, but just call readInput(). public static int readInt(String prompt) { return Integer.parseInt(getInput(prompt)); } D) Write a method that returns a double. public static double readDouble(String prompt) { return Double.parseDouble(getInput(prompt)); } E) Fill in a main method body that uses these to input three ints and print the average. int a = readInt("Enter first integer: "); int b = readInt("Enter second integer: "); int c = readInt("Enter third integer: "); System.out.println("The average is " + average(a,b,c)); 5) Summary - defining and calling methods - parameters and return values - local variables vs. data members *******************************************************************************