CS 100, Summer 2001 Thursday, 7/5 Lecture 7 ----------------------------------------------------------------------- Announcements: + Assignment 2 due Friday (7/6) + PRELIM 1 on Monday (7/9): covers Savitch Ch.1-3, all lectures, exercises, assignments 1 and 2 + Leave office hours as is - if you cannot make them, please make an appointment by email + reading for tomorrow: bring questions! ----------------------------------------------------------------------- Some random issues: - print vs. println (includes newline) - error in PrimeNumbers.java: Algorithm description should check div for 2 to checkNum. - results of minute paper - I'll start bringing printouts of code examples to class - declaring int more than once inside a loop: this is ok! but it may not be what you expected - it's a different variable each time through the loop. (we'll talk about scope later today, maybe) - most confusing: Matlab and methods. This is unsurprising, since both were covered very quickly and shallowly! More today. - Ben's post on random and his comments on Ex 4 are *required reading*. ----------------------------------------------------------------------- Topics: + SpaceInvadersDoWhile + named constants + Matlab + methods ----------------------------------------------------------------------- ----------------------------------------------------------------------- + assignment: named constants - use variables for commonly used numbers (classic example is pi) - style: improves reuse and readability - but not truly constant because you could reassign + can make variables "unvariable" with $final$: $final$ type variable = value - final varibles known as CONSTANTS - won't be able to reassign a constant ----------------------------------------------------------------------- - to further refine our view of loops... - note do-while in SpaceInvaders requires some extra checks - for: MirrorNumber? - don't be intimidated! - do read it. Anyone have questions?? - style: add // comments if closing brace is far from opening brace. ----------------------------------------------------------------------- ----------------------------------------------------------------------- MATLAB + DIS tutorial, part 1 (matlab1.m) - you know enough know to pick up what's there much more easily - same issues: variables, operators, strings, simple functions - we won't go into MATLAB in depth here, but welcome questions - later, we'll talk about conditional statements and loops in MATLAB + using a semicolon in Matlab - if you don't, you'll see the "result" of your command - if you do, the output is suppressed - both are fine. ----------------------------------------------------------------------- ----------------------------------------------------------------------- + Methods - think function: name(inputs) -> generates output - three parts: input, processing, and output (same as with algorithms!) - methods allow you to "delegate" work and tend to shorten the program - examples: - "open the door" method (no input; action: opening door) - "read this book" method (input: book, action: reading) - "take this money and buy my groceries for me" method: (input: money, grocery list; action: purchasing, transport back; output: groceries (and change!)) - without programming: ____ \/ 4 => 2 method: 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) ---------------------------------------------------------------------- Examples in Java: Math.random(): see also random.java System.out.println(_stuff_); System.out.print(_stuff_); main(String[] args) + interesting features: - not all have inputs - some don't actually give a value (just perform an action) + 2 general kinds: - performs action without returning a value - returns value (and maybe also performs action) ---------------------------------------------------------------------- How to use: + perform action: System.out.println("Hello"); note: common source of confusion: output is NOT a returned value e.g., try int x = System.out.print("Hello"); + return a value: int x = Math.random()*10; + order of actions for operations that use methods (Control Flow of Methods) - Java invokes methods - replaces method call with the returned value ---------------------------------------------------------------------- How to design your own methods: + put your method before the main() method + invent a name + decide on the inputs to the method + write tasks for BODY of method (what the method does) + decide if there's a value to return + add comments before the method that indicate a) what it does b) what the inputs are c) what the output (return value) is EXAMPLE: Methods.java + control flow: - Java starts at main() in Main Class (assuming you set Main Class target) - Java works top-down, statement-to-statement - the myPrint call: o do expressions in methods (follow precedence, associativity) o do method call o find myPrint o check if type of input matches expected type o "bind" arguments to input variables (so "Kiri" gets "bound" to "s") o perform actions in invoked method o return a value (if specified) - return control back to the point when/where method was invoked ---------------------------------------------------------------- * pick up graded Exercise 4. * bring questions for TA review tomorrow!