CS 112 Introduction to Programming Nested Loops; Parameterized Methods Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone:

Slides:



Advertisements
Similar presentations
BUILDING JAVA PROGRAMS CHAPTER 3 PARAMETERS AND OBJECTS.
Advertisements

Nested for loops Cont’d. 2 Drawing complex figures Use nested for loops to produce the following output. Why draw ASCII art? –Real graphics require a.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs Lecture 3-1: Parameters (reading: 3.1)
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos:
Copyright 2006 by Pearson Education 1 reading: 4.1 Cumulative sum.
Copyright 2008 by Pearson Education 1 Class constants and scope reading: 2.4 self-check: 28 exercises: 11 videos: Ch. 2 #5.
CS305j Introduction to ComputingNested For Loops 1 Topic 6 Nested for Loops "Complexity has and will maintain a strong fascination for many people. It.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs.
Topic 7 parameters Based on slides bu Marty Stepp and Stuart Reges from "We're flooding people with information. We.
Building Java Programs
Copyright 2009 by Pearson Education Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos: Ch.
CS 112 Introduction to Programming Variable Scoping; Nested Loops; Parameterized Methods Yang (Richard) Yang Computer Science Department Yale University.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-3: Loop Figures and Constants reading: self-checks: 27 exercises:
CS 112 Introduction to Programming Graphics; Animation Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone:
1 BUILDING JAVA PROGRAMS CHAPTER 2 Pseudocode and Scope.
Building Java Programs Parameters and Objects. 2 Redundant recipes Recipe for baking 20 cookies: –Mix the following ingredients in a bowl: 4 cups flour.
Building Java Programs Chapter 3 Lecture 3-1: Parameters reading: 3.1.
1 Building Java Programs Chapter 3: Introduction to Parameters and Objects These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They.
CS 112 Introduction to Programming Variables; Type Casting; Using Variables in for Loops Yang (Richard) Yang Computer Science Department Yale University.
CS 112 Introduction to Programming Variable Scoping; Nested Loops; Parameterized Methods Yang (Richard) Yang Computer Science Department Yale University.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 4: Loop Figures and Constants reading:
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos:
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos:
Building Java Programs Chapter 2 Primitive Data and Definite Loops Copyright (c) Pearson All rights reserved.
Nested for loops.
Printing with for Loops To print a character multiple times, use a for loop. for (int j = 1; j
The for loop.
Topic 6 loops, figures, constants Based on slides bu Marty Stepp and Stuart Reges from "Complexity has and will maintain.
Copyright 2008 by Pearson Education 1 Nested loops reading: 2.3 self-check: exercises: videos: Ch. 2 #4.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
Copyright 2010 by Pearson Education Building Java Programs Chapter 3 Lecture 3-1: Parameters reading: 3.1.
CS305j Introduction to Computing Parameters and Methods 1 Topic 8 Parameters and Methods "We're flooding people with information. We need to feed it through.
CS 112 Introduction to Programming Loop Examples; Variable Scoping; Nested Loops; Yang (Richard) Yang Computer Science Department Yale University 208A.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 2: Primitive Data and Definite Loops.
CS 112 Introduction to Programming Java Graphics Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone:
CS 112 Introduction to Programming Summary of Method Definition and Invocation; printf/format Formatting Yang (Richard) Yang Computer Science Department.
Copyright 2009 by Pearson Education Building Java Programs Chapter 3 Lecture 3-1: Parameters reading: 3.1 self-check: #1-6 exercises: #1-3 videos: Ch.
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs Chapter 3
Building Java Programs
Building Java Programs
Building Java Programs
CSCI 161 – Introduction to Programming I William Killian
Building Java Programs
Building Java Programs
CSc 110, Spring 2017 Lecture 5: Constants and Parameters
Topic 7 parameters "We're flooding people with information. We need to feed it through a processor. A human must turn information into intelligence or.
Building Java Programs
Redundant recipes Recipe for baking 20 cookies:
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Topic 6 loops, figures, constants
Building Java Programs
Building Java Programs
Building Java Programs
CSE 142 Lecture Notes Global Constants, Parameters, Return Values
Building Java Programs
Building Java Programs
Suggested self-checks:
Building Java Programs
Drawing complex figures
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
CIS 110: Introduction to Computer Programming
Chapter 2 Lecture 2-3: Loop Figures and Constants reading:
Presentation transcript:

CS 112 Introduction to Programming Nested Loops; Parameterized Methods Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone:

Outline  Admin and recap  Nested loops  Parameterized methods 2

Admin  Coding style review 6:30 pm Tuesday  PS3 posted (please start early on PS3) 3

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 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).

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

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.

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

Drawing ASCII X A size SIZE ASCII X of 2 * SIZE rows, 2*Size columns == \ / /\ == \ / /\ == \ / /\ == SIZE 4 SIZE 3 SIZE 5

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

Top Half (V)  Observation: V can be produced using a nested for loop outer loop (loops SIZE – 1 times) == \ / /\ ==

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 \/ /\ == ==

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 / \ == ==

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.

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

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.

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); }

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

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

Method Exercise  Exercise: Design and implement the DrawX program.

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: (,,..., );

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:

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;

Out-Class Read: Another Way to Motivate Methods With Parameters 24

Example: Draw Matrix 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

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.

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

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;

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;

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

End of Out-Class Read 31