CS100J, Spring 2001 Thurs 9/27 Lecture 9 ------------------------------------------------------------------------------- Announcements: + give Alan any waivers and Project1s that need submission + P3 due Thurs 10/4 + T1: on Mon 10/1 - Details (incl rooms): Exams-->Prelim 1 - Review session: Exams-->Review 1 + comprehensive makeup project will cover one missed project - see 9/25 Announcements online + MATLAB: See Announcements + actually, all of this was already announced on Announcements :-) ------------------------------------------------------------------------------- Topics: + commenting a method + method arguments ------------------------------------------------------------------------------- Commenting: + brief: describe action of method above the header + longer: see pp 243-244, App.4 on pg 993 for an example - Precondition: what's true before the method is invoked - Action: what the method does - Postcondition: what's true after the method is invoked + action and postcondition are sometimes the same thing + sometimes it's clearer to just write the action ------------------------------------------------------------------------------- Arguments: + arguments are variables that receive values and are used in method body + Java is strongly typed + arguments is composed of series of inputs with form: type1 var1, type2 var2, ... + can also have NO arguments ex) System.out.println(), which prints a blank line ------------------------------------------------------------------------------- Terminology: + Formal parameters: - variables written inside method header - born when method invoked live during method execution die when method ends + Actual parameters: - anything passed into a method + Local variables: - variables declared inside a method - same rules as formal parameters - variables cannot be "seen" (are invisible) to other methods + think "statement block": header { stuff; } + the "stuff" may have declarations which are local (visible) only in the block! + Call by value: - value of actual parameter copied into formal parameter - method can NOT change the value of an actual parameter + Scope: - the visibility of a element of code (variable, method) ------------------------------------------------------------------------------ Example: public class Example2 { public static void myPrint(String s, int x) { System.out.println(s+" is "+x); } public static int myRandom(int x) { int y = 1; return (int)( Math.random()*(x+y) ); } public static void main(String[] args) { int x = 10; int y = myRandom(x); myPrint(x); myPrint(y); } } ------------------------------------------------------------------------------ Example: public class Example3 { public static void warning(int x) { if (x != 0) { System.out.println("Buh!"); return; else System.out.println("OK!"); } public static void main(String[] args) { warning( (int) Math.random()*2 ); System.out.println("yo!"); } } ------------------------------------------------------------------------------