CSc 110, Spring 2017 Lecture 5: Constants and Parameters

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 Class constants and scope reading: 2.4 self-check: 28 exercises: 11 videos: Ch. 2 #5.
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
Building Java Programs Chapter 2 Primitive Data and Definite Loops.
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.
Building Java Programs Chapter 3 Parameters and Objects Copyright (c) Pearson All rights reserved.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-3: Loop Figures and Constants reading: self-checks: 27 exercises:
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 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:
Building Java Programs Chapter 2 Primitive Data and Definite Loops.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
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.
Copyright 2010 by Pearson Education Building Java Programs Chapter 3 Lecture 3-1: Parameters reading: 3.1.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 2: Primitive Data and Definite Loops.
CS 112 Introduction to Programming Nested Loops; Parameterized Methods Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone:
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
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs Chapter 3
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs
Building Java Programs Chapter 2
Building Java Programs
Building Java Programs
CSCI 161 – Introduction to Programming I William Killian
Building Java Programs
CSc 110, Autumn 2017 Lecture 5: The for Loop and user input
CSc 110, Spring 2017 Lecture 6: Parameters (cont.) and Graphics
Building Java Programs
Building Java Programs Chapter 2
CSc 110, Autumn 2016 Lecture 5: Loop Figures and Constants
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
CSc 110, Spring 2017 Lecture 4: Nested Loops and Loop Figures
Redundant recipes Recipe for baking 20 cookies:
Building Java Programs
Building Java Programs
CSc 110, Spring 2018 Lecture 7: input and Constants
CSc 110, Spring 2018 Lecture 5: The for Loop and user input
Building Java Programs
Building Java Programs
CSc 110, Spring 2018 Lecture 5: Loop Figures and Constants
Topic 6 loops, figures, constants
Building Java Programs
Building Java Programs
CSc 110, Spring 2018 Lecture 8: input and Parameters
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
Building Java Programs
Chapter 2 Lecture 2-3: Loop Figures and Constants reading:
Presentation transcript:

CSc 110, Spring 2017 Lecture 5: Constants and Parameters Adapted from slides by Marty Stepp and Stuart Reges

Pseudocode algorithm 1. Line 2. Top half # , 16 =, # 2. Top half | spaces (decreasing) <> dots (increasing) spaces (same as above) 3. Bottom half (top half upside-down) 4. Line #================# | <><> | | <>....<> | | <>........<> | |<>............<>|

Functions from pseudocode def main(): line() top_half() bottom_half() def top_half(): for line in range(1, 5): # contents of each line def bottom_half() { def line(): # ...

2. Tables A table for the top half: Compute spaces and dots expressions from line number line spaces line * -2 + 8 dots 4 * line - 4 1 6 2 4 3 8 12 line spaces dots 1 6 2 4 3 8 12 #================# | <><> | | <>....<> | | <>........<> | |<>............<>|

3. Writing the code Useful questions about the top half: Number of (nested) loops per line? #================# | <><> | | <>....<> | | <>........<> | |<>............<>|

Solution for top_half() # Prints the expanding pattern of <> for the top half of the figure. def top_half(): for line in range(1, 5): print("|", end="") for space in range(1, line * -2 + 9): print(" ", end="") print("<>", end="") for dot in range(1, line * 4 - 3): print(".", end="") print("|")

Class constants and scope 11/27/2018 Class constants and scope 7

Scaling the mirror Let's modify our Mirror program so that it can scale. The current mirror (left) is at size 4; the right is at size 3. We'd like to structure the code so we can scale the figure by changing the code in just one place. #================# | <><> | | <>....<> | | <>........<> | |<>............<>| #============# | <><> | | <>....<> | |<>........<>|

Constants constant: A fixed value visible to the whole program. value should only be set only at declaration; shouldn't be reassigned Syntax: Just like declaring a normal variable: name = value name is usually in ALL_UPPER_CASE Examples: DAYS_IN_WEEK = 7 INTEREST_RATE = 3.5 SSN = 658234569

11/27/2018 Constants and figures Consider the task of drawing the following scalable figure: +/\/\/\/\/\/\/\/\/\/\+ | | | | Multiples of 5 occur many times +/\/\/\/\+ | | | | The same figure at size 2 10

Repetitive figure code 11/27/2018 Repetitive figure code def main(): draw_line() draw_body() def draw_line(): print("+", end="") for i in range(1, 11): print("/\\", end="") print("+") def draw_body(): for line in range(1, 6): print("|", end="") for spaces in range(1, 21): print(" ", end="") print("|") 11

Adding a constant HEIGHT = 5 def main(): draw_line() draw_body() 11/27/2018 Adding a constant HEIGHT = 5 def main(): draw_line() draw_body() def draw_line(): print("+", end="") for i in range(1, HEIGHT * 2 + 1): print("/\\", end="") print("+") def draw_body(): for line in range(1, HEIGHT + 1): print("|", end="") for spaces in range(1, HEIGHT * 4 + 1): print(" ", end="") print("|") 12

Complex figure w/ constant Modify the Mirror code to be resizable using a constant. A mirror of size 4: #================# | <><> | | <>....<> | | <>........<> | |<>............<>| A mirror of size 3: #============# | <><> | | <>....<> | |<>........<>|

Loop tables and constant Let's modify our loop table to use SIZE This can change the amount added in the loop expression #================# #============# | <><> | | <><> | | <>....<> | | <>....<> | | <>........<> | |<>........<>| |<>............<>| |<>........<>| |<>............<>| | <>....<> | | <>........<> | | <><> | | <>....<> | #============# | <><> | #================# SIZE line spaces dots 4 1,2,3,4 6,4,2,0 0,4,8,12 3 1,2,3 4,2,0 0,4,8

Partial solution SIZE = 4; # Prints the expanding pattern of <> for the top half of the figure. def top_half(): for line in range(1, SIZE): print("|", end="") for space in range(1, line * -2 + (2*SIZE) + 1): print(" ", end="") print("<>", end="") for dot in range(1, line * 4 - 3): print(".", end="") print("|")

Observations about constant 11/27/2018 Observations about constant The constant can change the "intercept" in an expression. Usually the "slope" is unchanged. SIZE = 4; for space in range(1, line * -2 + (2 * SIZE)): print(" ", end="") It doesn't replace every occurrence of the original value. for dot in range(1, line * 4 – 4 + 1): print(".", end="")

Promoting reuse Programmers build increasingly complex applications Enabled by existing building blocks, e.g. functions The more general a building block, the easier to reuse Abstraction: focusing on essential properties rather than implementation details Algebra is all about abstraction Functions solve an entire class of similar problems

Figures with repetition Consider the task of printing the following lines/boxes: ************* ******* *********************************** ********** * * ***** * *

A solution with repetition This code has repetition. Would variables help? Would constants help? What is a better solution? def main(): line_of_13() line_of_7() line_of_35() box10x3() box5x4() def line_of_13(): for i in range(1, 14): print("*", end="") print() def line_of_7(): for i in range(1, 8): def line_of_35(): for i in range(1, 36): ... line - A function to draw a line of any number of stars. box - A function to draw a box of any size.

Parameterization parameter: A value passed to a function by its caller. Instead of line_of_7, line_of_13, write line to draw any length. When declaring the function, we will state that it requires a parameter for the number of stars. When calling the function, we will specify how many stars to draw. main line ******* 7 ************* 13

Stating that a function requires a parameter in order to run Declaring a parameter Stating that a function requires a parameter in order to run def <name> (<name>): <statement>(s) Example: def say_password(code): print("The password is: " + code) When say_password is called, the caller must specify the code to print.

Calling a function and specifying values for its parameters Passing a parameter Calling a function and specifying values for its parameters <name>(<expression>) Example: say_password(42) say_password(12345) Output: The password is 42 The password is 12345

Parameters and loops A parameter can guide the number of repetitions of a loop. chant(3) def chant(times): for i in range(0, times): print("Just a salad...") Output: Just a salad...

How parameters are passed When the function is called: The value is stored into the parameter variable. The function's code executes using that value. chant(3) chant(7) def chant(times): for i in range(0, times): print("Just a salad...") 3 7

Common errors If a function 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 function must be of a type that will work. chant(3.7) # ERROR: must be of type int if it # is used as a range bound Exercise: Change the stars program to use a parameterized function for drawing lines of stars.

Stars solution # Prints several lines of stars. # Uses a parameterized function to remove repetition. def main(): line(13) line(7) line(35) # Prints the given number of stars plus a line break. def line(count): for i in range(0, count): print("*", end="") print()

Multiple parameters A function can accept multiple parameters. (separate by , ) When calling it, you must pass values for each parameter. Declaration: def <name>(<name>, ..., <name>): <statement>(s) Call: <name>(<exp>, <exp>, ..., <exp>)

Multiple parameters example def main(): print_number(4, 9) print_number(17, 6) print_number(8, 0) print_number(0, 8) def print_number(number, count): for i in range(0, count): print(number, end="") print() Output: 444444444 171717171717 00000000 Modify the stars program to draw boxes with parameters.

Stars solution # Prints a box of stars of the given size. # Prints several lines and boxes made of stars. # Third version with multiple parameterized methods. def main(): line(13) line(7) line(35) print() box(10, 3) box(5, 4) box(20, 7) # Prints the given number of #stars plus a line break. def line(count): for i in range(0, count): print("*", end="") # Prints a box of stars of the given size. def box(width, height): line(width) for line in range(0, height - 2): print("*", end="") for space in range(0, width - 2): print(" ", end="") print("*")

Strings as parameters Output: say_hello("Allison") teacher = "Bictolia" say_hello(teacher) def say_hello(name): print("Welcome, " + name) Output: Welcome, Allison Welcome, Bictolia Modify the stars program to use string parameters. Use a function named repeat that prints a string many times.

Stars solution # Prints a box of stars of the given size. # Prints several lines and boxes made of stars. # Fourth version with String parameters. def main(): line(13) line(7) line(35) print() box(10, 3) box(5, 4) box(20, 7) # Prints the given number of # stars plus a line break. def line(count): repeat("*", count) # Prints a box of stars of the given size. def box(width, height): line(width) for line in range(height – 2): print("*", end="") repeat(" ", width - 2) print("*") # Prints the given String the given # number of times. def repeat(s, times): for i in range(0, times): print(s, end="")

Value semantics value semantics: When numbers and strings are passed as parameters, their values are copied. Modifying the parameter will not affect the variable passed in. def strange(x): x = x + 1 print("1. x = " + x) x = 23 strange(x) print("2. x = " + x) ... Output: 1. x = 24 2. x = 23

A "Parameter Mystery" problem def main(): x = 9 y = 2 z = 5 mystery(z, y, x) mystery(y, x, z) def mystery(x, z, y): print(str(z) + " and " + str(y - x)) Output: 2 and 4 9 and 3