CS171:Introduction to Computer Science II Department of Mathematics and Computer Science Li Xiong 9/7/2012 1
Announcement Introductory/Eclipse Lab, Friday, Sep 7, 2-3pm (today) Hw1 to be assigned Monday, Sep 10
Pretest Postmortem Java Review Roadmap Types, variables, assignments, expressions Control flow statements Methods Arrays OO and Inheritance 3
Declaration Assignments Conditionals Loops Break and continue Statements
Simple if Statements if (booleanexpression) { statement(s); if (!passedtest ) { System.out.println( I ll write good code! ); Boolean Expression false! passedtest false true true Statement(s) System.out.println( I ll write good code! ); (A) (B)
The if...else Statement if (booleanexpression) { statement(s)-for-the-true-case; else { statement(s)-for-the-false-case; 6
World Without Loops is Painful System.out.println( I will write good code! ); System.out.println( I will write good code! ); System.out.println( I will write good code! ); System.out.println( I will write good code! ); System.out.println( I will write good code! ); System.out.println( I will write good code! ); System.out.println( I will write good code! );
A Better Approach: Loops int count=0; while (count < 100){ System.out.println( I will write good code! ); count++;
whileloop Flow Chart while (loop-continuation-condition){ // loop-body; Statement(s); intcount = 0; while (count < 100) { System.out.println( I ll write good code!"); count++; count = 0; Loop Continuation Condition? false (count < 100)? false true Statement(s) (loop body) true System.out.println("I ll write good code!"); count++; (A) (B)
do-whileloop Statement(s) (loop body) true do { // Loop body; Statement(s); while (loop-continuation-condition); Loop Continuation Condition? false 10
forloops for (initial-action; loop-condition; action-after-each-iteration) { // loop body; Statement(s); for(int count=0; count < 100; count++ ){ System.out.println( I will write good code! ); 11
forloops for (initial-action; loopcontinuation-condition; action-after-each-iteration) { // loop body; Statement(s); for (inti= 0; i< 100; i++) { System.out.println( I ll write good code!"); Initial-Action i = 0 Loop Continuation Condition? false (i < 100)? false true Statement(s) (loop body) true System.out.println( "I ll write good code"); Action-After-Each-Iteration i++ (A) (B) 12
Which loop to use? Use the one that is most intuitive and comfortable for you. A for loop may be used if the number of repetitions is known, as, for example, when you need to print a message 100 times. A while loop may be used if the number of repetitions is not known, as in the case of reading the numbers until the input is 0. A do-while loop can be used to replace a while loop if the loop body has to be executed before testing the continuation condition. 13
Question What is the output of the following code fragment?. int sum = 1; for (int i = 0; i <= 5; sum = sum + i++); System.out.print(sum); 15
Bonus question What is the value of x after the following statements? intx = 0, j = 0; boolean done = false; while(!done) { for (inti= 0; i<5; i++) { j = j + i; if (j > 12) { x = j; done = true; 16
Roadmap Lab session Pretest Postmortem Java Review Types, variables, assignments, expressions Control flow statements Methods Arrays OO and Inheritance 17
Levels of Abstraction: Software Design Old times: computer programs manipulated primitive types such as numbers and characters Methods: Encapsulate routine computations to black boxes Object-oriented programming: Encapsulate data fields and methods to black boxes
Example Computing sum intsum = 0; for (inti = 1; i <= 5; i++ ) { sum += i; System.out.println( sum is: + sum);
Example -Print the sums of 1 10, 25 35, 40 50 public static void main(string[] args) { intsum = 0; for (int i = 1; i <= 10; i++) { sum += i; System.out.println("The sum of 1-10 is: " + sum); sum = 0; for (int i = 25; i <= 30; i++) { sum += i; System.out.println("The sum of 25-30 is: " + sum); sum = 0; for (int i = 40; i <= 50; i++) { sum += i; System.out.println("The sum of 40-50 is: " + sum);
Defining a Method sum public static intsum(intstart, intend) { intsum = 0; for (inti = start; i <= end; i++) { sum += i; return sum; // return is required
Using a method Sum public static void main(string[] args) { System.out.println("sum(1, 10) is: " + sum(1, 10) ); System.out.println("sum(25, 30) is: " + sum(25, 30) ); System.out.println("sum(40, 50) is: " + sum(40, 50) );
Defining and Using Methods Define a method give a definition of what the method is to do modifier returntype methodname(list of parameters) { collection of statements; Call or invoke a method use a method methodname(list of parameters) 23
Passing Parameters When calling a method, the arguments must match the parameters in order, number, and compatible type public static void nprintln(string message, int n) { for (int i = 0; i < n; i++) System.out.println(message); public static void main(string[] args) { nprintln( Hello!, 3); nprintln( So that s how the methods work, 10); When invoking a method, the value of the argument is passed to the parameter. The variable itself is not affected. This is referred to as 24 pass-by-value.
Mechanics of the Method-Calling Process 1. 2. 3. 4. 5. Evaluate the argument expressions Copy argument value into the corresponding parameter, (allocated in a newly assigned region of memory called a stack frame) Execute body, using the new stack frame for local variables. On a return statement, compute the return value andsubstitutesthatvalueinplaceofthecall. Discard the stack frame for the method and returns to the caller, continuing where it left off.
Sum Example: Call Stack public static void main(string[] args) { // 1. evaluate arguments System.out.println("sum(1, 10) is: " + sum(1, 10) ); // 1+2+...+10 System.out.println("sum(25, 30) is: " + sum(25, 30) ); //25+26+...+30 System.out.println("sum(40, 50) is: " + sum(40, 50) ); //40+41+...+50 public static intsum(intstart, intend) { // 2. copy args, new SF intsum = 0; // 3. execute the body for (inti= start; i<= end; i++) { sum += i; return sum; //4 and 5. return the value, and discard stack frame
Overloading methods Method overloading: multiple methods can have the same name but different parameter lists Compiler determines which method is used based on the method signature (method name and parameters) Early binding
Overloading Methods public static int max(int num1, int num2) { if (num1 > num2) return num1; else return num2; public static double max(double num1, double num2) { if (num1 > num2) return num1; else return num2; max(1, 3); max(1.0, 3.0); max(1.0, 3); 28
Overloading Methods public static int max(int num1, int num2) { if (num1 > num2) return num1; else return num2; public static double max(double num1, double num2) { if (num1 > num2) return num1; else return num2; max(1, 3); max(1.0, 3.0); max(1.0, 3); 29
Roadmap Lab session Pretest Postmortem Java Review Types, variables, assignments, expressions Control flow statements Methods Arrays OO and Inheritance