James Tam Programming: Part II In this section of notes you will learn about more advanced programming concepts such as looping, functions.

Slides:



Advertisements
Similar presentations
CS1315: Introduction to Media Computation Making sense of functions.
Advertisements

A Media Computation Cookbook Manipulating Images and Sounds for Use in Alice Part 1: Image Manipulations Part 2: Advanced Image Manipulations, e.g., changing.
CS2984: Introduction to Media Computation Drawing directly on images.
Chapter 3: Editing and Debugging SAS Programs. Some useful tips of using Program Editor Add line number: In the Command Box, type num, enter. Save SAS.
Python: Modifying Pictures Using Loops. Review JES command area – program area Defining/using functions specifying a sequence of steps for what the function.
TOPIC 5 INTRODUCTION TO PICTURES 1 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B.
ManipulatingPictures-Mod6-part21 Manipulating Pictures, Arrays, and Loops part 2 Barb Ericson Georgia Institute of Technology.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Python: Making colors and Using Loops. Review JES command area – program area Defining/using functions specifying a sequence of steps for what the function.
CIS101 Introduction to Computing Week 12. Agenda Your questions Solutions to practice text Final HTML/JavaScript Project Copy and paste assignment JavaScript:
CS 102 Computers In Context (Multimedia)‏ 02 / 06 / 2009 Instructor: Michael Eckmann.
James Tam Breaking Problems Down This section of notes shows you how to break down a large problem into smaller parts that are easier to implement and.
Computer Science 1620 Loops.
James Tam Functions: Decomposition And Code Reuse (Part I) This section of notes shows you how to write functions that can be used to: decompose large.
James Tam Programming: Part I In this section of notes you will learn how to write simple Jython programs using JES.
Chapter 2: Algorithm Discovery and Design
James Tam Programming: Part I In this section of notes you will learn about how to write simple programs using JES.
James Tam Introduction To Problem Solving This section will focus on problem solving strategies.
J. Michael Moore Software Design CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
J. Michael Moore Software Design CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
Programming: Part II In this section of notes you will learn more advanced programming concepts such as branching and repetition as well as how to work.
CS 102 Computers In Context (Multimedia)‏ 02 / 25 / 2009 Instructor: Michael Eckmann.
James Tam Lists In this section of notes you will be introduced to new type of variable that consists of other types.
James Tam Problem Decomposition This section of notes shows you how to break down a large problem into smaller parts that are easier to implement and manage.
Chapter 2: Algorithm Discovery and Design
Chapter 2: Algorithm Discovery and Design
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
TOPIC 9 MODIFYING PIXELS IN A MATRIX: COPYING, CROPPING 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach.
by Chris Brown under Prof. Susan Rodger Duke University June 2012
Java: Chapter 1 Computer Systems Computer Programming II.
Chapter 6: Modifying Pixels by Position. Chapter Learning Goals.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition.
Invitation to Computer Science, Java Version, Second Edition.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 5 Working with Images Starting Out with Games & Graphics in.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Functions.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 7 The Game Loop and Animation Starting Out with Games & Graphics.
Jeopardy Heading1Heading2Heading3Heading4 Heading5 Q $100 Q $200 Q $300 Q $400 Q $500 Q $100 Q $200 Q $300 Q $400 Q $500 Final Jeopardy.
Lecture 4 Pixels, Images and Image Files 1. In this Lecture, you will learn the following concepts: Image files (in particular, the BMP file format) How.
Pixels, Images and Image Files 1 By Dr. HANY ELSALAMONY.
11 Working with Images Session Session Overview  Find out more about image manipulation and scaling when drawing using XNA  Start to implement.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
CSC 1010 Programming for All Lecture 7 Input, Output & Graphics.
Python Programming in Context Chapter 6. Objectives To understand pixel based image processing To use nested iteration To use and understand tuples To.
CSC1401 Using Decisions in Java - 1. Recall from Alice We only wanted to shoot a lightning bolt at a philosopher So, we used the If statement.
Copyright © Texas Education Agency, All rights reserved.1 Web Technologies Motion Graphics & Animation.
Review Expressions and operators Iteration – while-loop – for-loop.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
1 CS 177 Week 7 Recitation Slides Modifying Sounds using Loops + Discussion of some Exam Questions.
Chapter 7 Modularity Using Functions: Part II. A First Book of ANSI C, Fourth Edition 2 Variable Scope If variables created inside a function are available.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
1 Sections 5.1 – 5.2 Digital Image Processing Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Functions: Decomposition And Code Reuse This section of notes shows you how to write functions that can be used to: decompose large problems, and to reduce.
Topic 9 Modifying Pixels in a Matrix: Copying, Cropping
Introduction to Programming
CMPT 120 Topic:  Case Study.
Introduction to Programming
JavaScript: Functions
CSc 110, Spring 2017 Lecture 6: Parameters (cont.) and Graphics
Manipulating Pictures, Arrays, and Loops part 2
Chapter 2 - Introduction to C Programming
Practice with loops! What is the output of each function below?
Introduction to Programming
Chapter 2 - Introduction to C Programming
Functions: Decomposition And Code Reuse
CS 177 Week 3 Recitation Slides
Python Basics with Jupyter Notebook
Chapter 2 - Introduction to C Programming
Introduction to Programming
Presentation transcript:

James Tam Programming: Part II In this section of notes you will learn about more advanced programming concepts such as looping, functions.

James Tam Repetition Problem: what if a program or portion of a program needs to repeat itself. Example: −Allowing a player to re-play a game again. −Continuing to prompt a person to type in value if they enter in an invalid response. Loops: Allows a program or portion of a program to be repeated −There are two main types of loops in Python (for and while). −In this course we’ll focus on the latter.

James Tam The For Loop Format: for in : body Example: Available online and is called “loop1.py ” def loop1 (): total = 0 for i in range (1, 4, 1): total = total + i print "i=", i, " total=", total print "Done!"

James Tam Additional For-Loop Examples Available online and is called “loop2.py ” def loop2 (): for i in range (5, 0, -2): print "i=", i print "Done!“ Available online and is called “loop3.py ” def loop3 (): for i in [5, 2, 3, 10]: print i print "Done!"

James Tam Real: Problem Solving Approaches Bottom up Top down

James Tam Bottom Up Approach To Design Start implementing all details of a solution without first developing a structure or a plan. −Potential problems: –(Generic problems): Redundancies and lack of coherence between sections. –(Programming specific problem): Trying to implement all the details of large problem all at once may prove to be overwhelming. Here is the first of my many witty anecdotes, it took place in a “Tim Horton’s” in Balzac..

James Tam Top Down Design 1.Start by outlining the major parts (structure) 2.Then implement the solution for each part My autobiography Chapter 1: The humble beginnings Chapter 2: My rise to greatness … Chapter 1: The humble beginnings It all started seven and one score years ago with a log-shaped work station…

James Tam Writing Programs Using The Top Down Approach Functions can be used to break a program down into manageable parts. Each part of the program is defined by a function. Each function is written one at a time and tested before being added to the main program.

James Tam A Very Simple Example Of Using Functions Available online and is called “ fun1.py ” def display (): print "Inside display" def main (): print "Starting main" display () print “Display is done, back in main 1.Starting the main program (function called “main”) 2.First statement inside main runs 3.Second statement invokes the function called “display” 4.Statements inside of display execute one-at-a-time. 5.Execution returns back to “main” and the third and final statement here executes.

James Tam Issue: What’s Inside A Function Stays Inside A Function Note: This example won’t translate into binary so it can’t be run. def fun (): print num def main (): num = 10 print num fun () Num??? Never heard of it!!!

James Tam Passing Information Into Functions Parameters/Inputs: Allows the contents of a variable to be passed into a function. (Modified version of the previous example and it will work): def fun (num): print num def main (): num = 10 print num fun (num) Passes the contents of ‘num’ from the main function into function fun The contents of variable ‘num’ in main are stored in a local variable called ‘num’ – it’s num in fun :D

James Tam Passing Multiple Parameters/Inputs Format: def function name (input 1, input 2,...input n ) body Example: def fun (num1, num2, num3, string1) num1 = num2 + num3 string1 = “dude!”

James Tam In A Similar Fashion Values Must Be Returned From Functions def fun (): num = 1 print num def start (): fun () print num Num??? Never heard of it???!!!

James Tam Returning Values From Functions Format (Writing the function being called): def function name (): return value 1, value 2,...value n Examples (Writing the function being called): def fun1 (): num1 = 10 num2 = 20 return num1, num2 def fun2 (num1, num2): num3 = num1 + num2 return num3

James Tam Returning Values From Functions (2) Format (Storing the return values): variable 1, variable 2,...variable n = function name () Examples (Storing the return values): num1, num2 = fun1 () num3 = fun2 (num1, num2)

James Tam Parameter Passing And Return Values: An Example Available online and is called “ interest.py ”: def calculate (principle, rate, time): interest = principle * rate * time total = principle + interest return interest, total def start (): interest, total = calculate (100, 0.1, 5) print "Interest $ ", interest print "Total $", total

James Tam Top Down Approach: Example Of Breaking A Programming Problem Down Into Parts (Functions) Calculate Interest Function: Get information Function: Do calculations Function: Display results

James Tam Using Output Statements To Understand How A Program Works Example: what does the following program do? def start (): for i in range (1, 20, 1): if (i % 5 == 0): j = i print “j=“, j An output statement shows the successive values of ‘i’ inside the loop print “i=“, i An output statement shows the successive values of ‘i’ inside the loop and inside the if- branch

James Tam Information About Pictures In JES Images/pictures consist of pixels Each pixel has a set of coordinates (x, y) that determine it’s location. Example image (200 pixels x 100 pixels) Y coordinate (100) X coordinate (200) Top left (x =0, y = 0) Top right (x =200, y = 0) Bottom left (x =0, y = 100) Bottom right (x =200, y = 100)

James Tam Picture/Image-Related Functions In JES For more details look under the help menu under “Picture functions”. −addLine (picture, startX, startY, endX, endY) −addRect (picture, startX, startY, width, height) −addRectFilled (picture, startX, startY, width, height, color) −addText (picture, xpos, ypos, text): Explanation of the function values picture: the name of the picture that you want to edit startX: the starting pixel coordinate on the x axis startY: the starting pixel coordinate on the y axis endX: the ending pixel coordinate on the x axis endY: the ending pixel coordinate on the y axis width: the width of the rectangle in pixels weight: the height of the rectangle in pixels color: the color to fill the rectangle with (red, green, blue etc.)

James Tam Example Of Editing A Picture Available online and is called “ picture1.py ” def picture1(): lion = makePicture ("lion.jpg") addLine (lion,0,0,100,100) addRectFilled (lion,100,100,100,200,red) addText (lion,200,300,"Lion dance for the Lions Club") show (lion) Important: Typically you want to show a picture only after you have finished all your edits!

James Tam Review: The 24 Bit Color Model Each pixel’s color is specified with 24 bits: −8 bits (256 combinations) for the red component −8 bits (256 combinations) for the blue component −8 bits (256 combinations) for the green component In JES the color value is an integer ranging from 0 – 255. −Smaller numbers result in darker pixels. −Larger numbers result in lighter pixels.

James Tam JES’ Pixel-Related Functions Get functions: find out the color level of a particular pixel −getRed: returns the red level (0 – 255) of a pixel −getBlue: returns the blue level (0 – 255) of a pixel −getGreen: returns the green level (0 – 255) of a pixel Set functions: change the color of a particular pixel −setRed: change the red level of a pixel (to a value from 0 – 255) −setBlue: change the blue level of a pixel (to a value from 0 – 255) −setGreen: change the green level of a pixel (to a value from 0 – 255)

James Tam Example: Seeing The Color Values Of A Picture Available online and is called “ picture2.py ”. It also requires that you download and save the picture “ smallLion.jpg ” into the folder that you run JES from. def picture2 (): picture = makePicture ("smallLion.jpg") show (picture) allPixels = getPixels (picture) for pixel in allPixels: red = getRed (pixel) blue = getBlue (pixel) green = getGreen (pixel) print "RBG:", red, blue, green

James Tam Example: Changing The Color Values Of A Picture Available online and is called “ picture3.py ”. It also requires that you download and save the picture “ mediumLion.jpg ” into the folder that you run JES from. def picture3 (): picture = makePicture ("mediumLion.jpg") show (picture) allPixels = getPixels (picture) for pixel in allPixels: red = getRed (pixel) blue = getBlue (pixel) green = getGreen (pixel) if ((red + 50) <= 255): setRed (pixel, (red+50)) if ((blue + 50) <= 255): setBlue (pixel, (blue+50)) if ((green + 50) <= 255): setGreen (pixel, (green+50)) repaint (picture) Show the original picture loaded from file. Show the original picture after it has been manipulated.

James Tam You Should Now Know What is the purpose of a loop How to write a for-loop in JES How functions are a way of implementing a top down approach to program design How to write and trace through a program that employs functions How to pass information to and from functions via parameters and return values A technique for tracing programs: using output statements Functions for working with images/pictures in JES