Download presentation
Presentation is loading. Please wait.
Published byNeal Malone Modified over 8 years ago
1
CS 112 Introduction to Programming Nested Loops; Parameterized Methods Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone: 432-6400 Email: yry@cs.yale.edu
2
Outline Admin and recap Nested loops Parameterized methods 2
3
Admin Coding style review 6:30 pm Tuesday PS3 posted (please start early on PS3) 3
4
Recap: for Loops 4 System.out.print("T-minus "); for (int i = 1; i <= 10; i++) { System.out.print(11-i + “, “); } System.out.println("blastoff!"); System.out.print("T-minus "); for (int i = 10; i >= 1; i--) { System.out.print(i + “, “); } System.out.println("blastoff!"); Counting down or up is mostly a personal style.
5
5 final int N = 10; System.out.print("T-minus "); for (int i = 1; i <= N; i++) { System.out.print(N+1-i + “, “); } System.out.println("blastoff!"); Convey your intention to the computer to help you. Recap: for Loops Minimize # of magic numbers (make change easier).
6
Recap: Variable Scope What: The part of a program where a variable exists Basic rule: from its declaration to the end of the enclosing { } braces Why: Encapsulation so that different methods can use the same variable name without the need for coordination Basic rule: Declare a variable with a minimal scope
7
Recap: Nested Loop for (int set = 1; set <= 5; set++) { for (int rps = 1; rps <= set; rps++) { System.out.print("*"); } System.out.println(); } There can be another loop inside one loop to form a nested loop Nested loops are common, important programming patterns.
8
Nested Loop Design Example: Drawing Complex Patterns A good practice of nested for loops is to draw ASCII art Why draw ASCII art? m Real graphics will require some finesse (shortly) m ASCII art has complex patterns m Can focus on the algorithms == \ / /\ == \ / /\ == \ / /\ == SIZE 4 SIZE 3 SIZE 5
9
Drawing ASCII X A size SIZE ASCII X of 2 * SIZE rows, 2*Size columns == \ / /\ == \ / /\ == \ / /\ == SIZE 4 SIZE 3 SIZE 5
10
Drawing ASCII X A size SIZE ASCII X of 2 * SIZE rows, 2*Size columns 1. Bound ==, (SIZE – 2)*2 spaces, == 2. Top half (V) 3. Bottom half (top half upside-down) 4. Bound == \ / /\ == \ / /\ == \ / /\ == SIZE 4 SIZE 3 SIZE 5
11
Top Half (V) Observation: V can be produced using a nested for loop outer loop (loops SIZE – 1 times) == \ / /\ ==
12
Outer and inner loop First write the outer loop, from 1 to the number of lines. for (int line = 1; line <= SIZE-1; line++) {... } Now look at the line contents. Each line has a pattern: m some white space m\m\ m/m/ == == 1 \ / 2 \ / 3 \/ /\ == ==
13
Final Pseudo Code 1. Bound ==, (SIZE – 2)*2 spaces, == 2. for line = 1 to SIZE -1 line spaces \ 2 SIZE – 2 – 2 * line spaces / 3. for line = 1 to SIZE – 1 SIZE – line spaces / (line – 1) * 2 spaces \ 4. Bound == == 1 \ / 2 \ / 3 \/ 1 /\ 2 / \ 3 / \ == ==
14
Implementation Problem 1. Bound ==, (SIZE – 2)*2 spaces, == 2. for line = 1 to SIZE -1 line spaces \ 2 SIZE – 2 – 2 * line spaces / 3. for line = 1 to SIZE – 1 SIZE – line spaces / (line – 1) * 2 spaces \ 4. Bound Drawing spaces is a reusable function, but need to draw different numbers of spaces.
15
Method Parameterization Specify a parameter to control the behavior of a method Methods with parameters solve an entire class of similar problems Redundancy removal/abstraction through generalization m The more general a building block, the easier to reuse it m We will learn more techniques on generalization/abstraction 15
16
Parameterization parameter: A value passed to a method by its caller, e.g., m When declaring a method, we will state that it requires a parameter for the number of spaces. m When calling the method, we will specify the number.
17
Syntax: Declaring a Method with a Parameter public static void ( ) { (s) ; } The parameter is called the formal argument Example: public static void sayPasswcode(int code) { System.out.println("The passcode is: " + code); }
18
How Parameters are Passed When a method with a formal argument is called: m A value is passed to the formal argument m The passed value is called the actual argument m The method's code executes using that value. public static void main(String[] args) { chant(3); chant(3+4); } public static void chant(int times) { for (int i = 1; i <= times; i++) { System.out.println("Just a salad..."); } 3 7
19
Common Errors If a method accepts a parameter, it is illegal to call it without passing any value for that parameter. chant(); // ERROR: parameter value required The value passed to a method must be of the correct type. chant(3.7); // ERROR: must be of type int
20
Method Exercise Exercise: Design and implement the DrawX program.
21
Multiple Parameters A method can accept multiple parameters. (separate by, ) m When calling it, you must pass values for each parameter. Declaration: public static void (,..., ) { (s) ; } Call: (,,..., );
22
Multiple Parameters Example public static void main(String[] args) { printNumber(4, 9); printNumber(17, 6); printNumber(8, 0); printNumber(0, 8); } public static void printNumber(int number, int count) { for (int i = 1; i <= count; i++) { System.out.print(number); } System.out.println(); } Output: 444444444 171717171717 00000000
23
23 Multiple Parameter Invocation Corresponding actual argument in the invocation is copied into the corresponding formal argument public static void printNumber(int number, int count) { for (int i = 1; i <= count; i++) { System.out.print(number); } System.out.println(); } printNumber(2, 5); // equiv: number = 2; count = 5;
24
Out-Class Read: Another Way to Motivate Methods With Parameters 24
25
Example: Draw Matrix..1.2. 3......1...2...3...4... 5.... int N = 5; for (int line = 1; line <= N; line++) { // initial dots for (int j = 1; j <= (-1 * line + N); j++) { System.out.print("."); } // the number System.out.print(line); // the second set of dots for (int j = 1; j <= (line - 1); j++) { System.out.print("."); } System.out.println(); } Print two copies of inverse diagonal matrix
26
Solution 1 26 public class DrawMatrix1 { public static void main(String[] args) { drawMatrix3(); drawMatrix5(); } public static void drawMatrix3() { int N = 3; for (int line = 1; line <= N; line++) { for (int j = 1; j <= (-1 * line + N); j++) { // initial dots System.out.print("."); } System.out.print(line); // the number for (int j = 1; j <= (line - 1); j++) { // the second set of dots System.out.print("."); } System.out.println(); } } // end of drawMatrix3 public static void drawMatrix5() { int N = 5; for (int line = 1; line <= N; line++) { for (int j = 1; j <= (-1 * line + N); j++) { // initial dots System.out.print("."); } System.out.print(line); // the number for (int j = 1; j <= (line - 1); j++) { // the second set of dots System.out.print("."); } System.out.println(); } } // end of drawMatrix5 } This code is redundant.
27
Remove Redundancy 27 public class DrawMatrix2 { } public static void drawMatrix() { for (int line = 1; line <= N; line++) { for (int j = 1; j <= (-1 * line + N); j++) { // initial dots System.out.print("."); } System.out.print(line); // the number for (int j = 1; j <= (line - 1); j++) { // the second set of dots System.out.print("."); } System.out.println(); } // end of for line } // end of drawMatrix public static void main(String[] args) { drawMatrix(); // should print 3 drawMatrix(); // should print 5 } // end of main int N = 3; N = 5; ERROR: N is out of scope
28
Solution 2 28 public class DrawMatrix2 { } public static void drawMatrix() { for (int line = 1; line <= N; line++) { for (int j = 1; j <= (-1 * line + N); j++) { // initial dots System.out.print("."); } System.out.print(line); // the number for (int j = 1; j <= (line - 1); j++) { // the second set of dots System.out.print("."); } System.out.println(); } // end of for line } // end of drawMatrix public static void main(String[] args) { drawMatrix(); // should print 3 drawMatrix(); // should print 5 } // end of main static int N; // introduce a class scope variable N = 3; N = 5;
29
Problem of Class Variable Indirection 29 public class DrawMatrix2 { } public static void drawMatrix() { for (int line = 1; line <= N; line++) { for (int j = 1; j <= (-1 * line + N); j++) { // initial dots System.out.print("."); } System.out.print(line); // the number for (int j = 1; j <= (line - 1); j++) { // the second set of dots System.out.print("."); } System.out.println(); } // end of for line } // end of drawMatrix public static void main(String[] args) { drawMatrix(); } // end of main static int N; // introduce a class scope variable N = 3; N = 5; Not self clear; Implicit, depends on context;
30
Solution 3: Parameterization Specify a parameter to control the behavior of a method Methods with parameters solve an entire class of similar problems Redundancy removal/abstraction through generalization m The more general a building block, the easier to reuse it m We will learn more techniques on generalization/abstraction 30
31
End of Out-Class Read 31
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.