CS100J, Spring 2001 Tues 9/25 Lecture 8 ------------------------------------------------------------------------------- Announcements: + new? responsible for www.cs.cornell.edu/Courses/cs100j/2001fa/ + E1: put in folder (sort by last name) + P3 due Thurs 10/4 + T1: on Mon 10/1 (rooms not known until Thurs) + Review session: Exams-->Review 1 + MATLAB: See Announcements ------------------------------------------------------------------------------- Topics: + control flow + methods ------------------------------------------------------------------------------- Redundancy and clarity: + Statements: expression, assignment, selection, repetition + very powerful -> can solve many, many problems + problem: spaghetti code! (thing FORTRAN) + solution: pull repeated tasks into their own "chunks" + abstraction: chunkification, move to more abstract levels, hide "the dirt" ------------------------------------------------------------------------------- Mathematical functions: + think function: you write/enter name(inputs), and it generates output + without programming: ____ \/ 4 => 2 sqrt(4) => 2 4 is the input/argument/parameter 2 is the output/returned value the action of performing the function/method: INVOCATION/CALLING/ NOTE: the returned value REPLACES the invoked function how? what about sqrt(4) + 3 ? you would add 2 + 3 -> 5 so, the 2 replaced sqrt(4) ------------------------------------------------------------------------------- Programming functions: + called METHODS in Java + examples: Math.random() System.out.println(_stuff_) System.out.print(_stuff_) main(String[] args) String1.equals(String2) SavitchIn.readInt() Max.max(v1,v2) Max.min(v1,v2) ------------------------------------------------------------------------------- Syntax: + how to write your own? + general structure: header { body } + syntax: modifiers returntype name(arguments) { statements; } + must access a method from a class or from within a class + you may write methods in any order in a class -- Java will find them! ------------------------------------------------------------------------------- Structure: + Need block statement as part of method + no semicolons after the last brace (}) + indent statements (the substructure) underneath the header ------------------------------------------------------------------------------- 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 ------------------------------------------------------------------------------- Name: + use a legal identifier + name should be an action + lowercase for first part of name + ex) getDistance, computeArea ------------------------------------------------------------------------------- Return type: + methods RETURN either a value or nothing (1) nothing to return: - ex) System.out.println(); - cannot use println() in an expression! - note: common source of confusion: output is NOT a returned value proof: try $int x = System.out.print("Hello");$ - use $void$ keyword - may use $return;$ to break from a void method (2) value to return: - the method computes a value and sends it back to the expression that invoked the method - the value replaces the method in the expression - ex) double x = Math.sqrt(4)+10; // x gets 2 + 10, or 12 - Java is strongly typed, so type of returned value must be the returntype ------------------------------------------------------------------------------- Modifiers: + use $public static$ or $private static$ for now + why? We're putting everything in the Main Class + want to hold off on OOP until next week ------------------------------------------------------------------------------- Statements: + Statements execute in method after the method is invoked + follow same control flow ------------------------------------------------------------------------------- Example: public class Example1 { public static void myPrint(int x) { System.out.println("The answer is: "+x); } public static void main(String[] args) { myPrint(10); } } + control flow: - Java starts at $main$ in Main Class (assuming you set Main Class target) - Java works top-down, statement-to-statement - Java invokes myPrint(10): - the value 10 is copied in x - the string and 10 are added together by promoting 10 to "10" - the string "The answer is: 10" is printed - the controld returns to the point where the method was invoked ------------------------------------------------------------------------------ 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 + 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!"); } } ------------------------------------------------------------------------------