CSE 8A Lecture 8 Reading for next class: 6.1-6.2 PSA4: Collage and Picture Flip, DON’T WAIT (it’s longer than the previous PSAs)

Slides:



Advertisements
Similar presentations
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Advertisements

F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
Chapter 7: User-Defined Functions II
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
What have we learned so far… Preprocessor directives Introduction to C++ Variable Declaration Display Messages on Screen Get Information from User Performed.
AU/MITM/1.6 By Mohammed A. Saleh 1. Arguments passed by reference  Until now, in all the functions we have seen, the arguments passed to the functions.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT.
Inline Functions Sometimes you need the same piece of code at multiple places in your task. Inline functions are copied at each place they are used. In.
TOPIC 9 MODIFYING PIXELS IN A MATRIX: COPYING, CROPPING 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach.
CSE 8A Lecture 8 Reading for next class: None Prepare for In-term exam 2 PSA4: Collage and Picture Flip, DON’T WAIT (it’s longer than the previous PSAs)
NestedLoops-part31 Nested Loops – part 3 Barb Ericson Georgia Institute of Technology Nov 2009.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
Java: Chapter 1 Computer Systems Computer Programming II.
Creative Commons Attribution Non-Commercial Share Alike License sa/3.0/
Creative Commons Attribution Non-Commercial Share Alike License sa/3.0/
COSC 1P02 Introduction to Computer Science 3.1 Cosc 1P02 Week 3 Lecture slides Birthdays are good for you. Statistics show that the people who have the.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 6 Functions.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
ITEC 352 Lecture 18 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Exam –Average 76 Methods for functions in assembly.
Log In to your User Start: Photoshop Elements. Start a new document 700x120 pixels with white background. Create a new layer by clicking Layer -> New.
CSE8A Lecture3 TODO: –Finish PSA1 individually (no partner!) and turn it in with the bundlePSA1 command GET AN INTERVIEW for PSA1 from a tutor See tutor.
“In mathematics and computer programming, Index notation is used to specify the elements of an array of numbers”
Agenda Review C++ Library Functions Review User Input Making your own functions Exam #1 Next Week Reading: Chapter 3.
IReport Demo Spring 2008 OEDSA Conference. Report Properties.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
CPS120: Introduction to Computer Science Functions.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Creative Commons Attribution Non-Commercial Share Alike License sa/3.0/
Creative Commons Attribution Non-Commercial Share Alike License sa/3.0/
NestedLoops-Mody7-part51 Two-Dimensional Arrays and Nested Loops – part 5 Rotations Barb Ericson Georgia Institute of Technology May 2007.
February ,  2/16: Exam 1 Makeup Papers Available  2/20: Exam 2 Review Sheet Available in Lecture  2/27: Lab 2 due by 11:59:59pm  3/2:
(6-3) Modular Programming H&K Chapter 6 Instructor - Andrew S. O’Fallon CptS 121 (October 2, 2015) Washington State University.
CS1101 Group1 Discussion 7 Lek Hsiang Hui comp.nus.edu.sg
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Lecture 2 Functions. Functions in C++ long factorial(int n) The return type is long. That means the function will return a long integer to the calling.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
CSE 8A Lecture 14 Reading for next class: None (INTERM EXAM #3) Today’s topics: –More Sounds! PSA 6 due tonight PSA 7 (sounds) due next Monday (11/19)
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
CSE8A Lecture 5 TODO: –FINISH PSA2 WITH YOUR PARTNER! Read next class: Section 5.1. PLAY WITH CODE! –Get stuck, then figure out how to get unstuck – it’s.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
1 Sections 5.1 – 5.2 Digital Image Processing Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
SUMMARY OF CHAPTER 2: JAVA FUNDAMENTS STARTING OUT WITH JAVA: OBJECTS Parts of a Java Program.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
CompSci 4 Java 4 Apr 14, 2009 Prof. Susan Rodger.
Georgia Institute of Technology Two-Dimensional Arrays and Nested Loops – part 4 Barb Ericson Georgia Institute of Technology August 2005.
Topic 9 Modifying Pixels in a Matrix: Copying, Cropping
Chapter 7: User-Defined Functions II
CSE 8A Lecture 17 Reading for next class: None (interm exam 4)
Chapter 10: Void Functions
Programming Fundamentals Lecture #7 Functions
Using local variable without initialization is an error.
Peer Instruction 6 Java Arrays.
CSI-121 Structured Programming Language Lecture 14 Functions (Part 2)
To have your National or Local Organization’s logo appear automatically on every new slide, follow the instructions below. Click on View-> Master -> Slide.
6.12 Default Arguments.
6 Chapter Functions.
CSE 8A Lecture 6 Reading for next class:
Two-Dimensional Arrays and Nested Loops – part 4
Start: Photoshop Elements
February , 2009 CSE 113 B.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Java Programming Language
Lesson 25 Miscellaneous Topics
Parameters and Arguments
Presentation transcript:

CSE 8A Lecture 8 Reading for next class: PSA4: Collage and Picture Flip, DON’T WAIT (it’s longer than the previous PSAs)

From the book: Cropping A Picture (page ) – we’ll change a bit Example of: –Working with both the calling object and a parameter object in a method Extra information is passed to methods through parameters. The calling object is something like an extra parameter, named this –Doing something to a subset of the possible pixels in a picture

What part of Katie are we copying? (slight mod from the book) public void copyKatiesXXX(Picture sourcePic) { Pixel sPixel = null, tPixel = null; for (int sX = 40, tX = 100; sX < 110; sX++, tX++) { for (int sY = 350, tY = 100; sY < 400; sy++, tY++) { sPixel = sourcePic.getPixel(sX,sY); tPixel = this.getPixel(tX,tY); tPixel.setColor(sPixel.getColor(); } 1)Solo: (1 min) 2)Discuss: (2 min) 3)Group: (30 sec) A.Feet B.Part of dress C.Hands D.Part of Couch E.Face

Parameters: getting information in to methods It’s nice to have code that is “user controllable”… We have been hard-coding constants (40, 3, 100, for example) a lot, but we can write more flexible code using PARAMETERS This lets us write code to do things like “cropping and pasting into a blank canvas”, but letting the user specify what part of the source picture to crop, and where to place it in the canvas.

Underline the values you would change into parameters and write a new method header public void copyKatiesXXX( ) { Pixel sPixel, tPixel = null; for (int sX = 40, tX = 100; sX < 110; sX++, tX++) { for (int sY = 350, tY = 100; sY < 400; sY++, tY++) { sPixel = sourcePic.getPixel(sX,sY); tPixel = this.getPixel(tX,tY); tPixel.setColor(sPixel.getColor(); }

Using parameters In Picture.java… In main… Picture fish = new Picture( "fish.jpg" ); Picture blank = new Picture(); Write the code to copy the square at position (10, 50) in fish to the blank canvas (vote on next slide) public void copyRegionTo (Picture target, int xSource, int ySource, int xTarget, int yTarget ) { Pixel sPixel, tPixel = null; for (int sX = xSource, tX = xTarget; sX < 100+xSource; sX++, tX++) { for (int sY = ySource, tY = yTarget; sY < 100+ySource; sY++, tY++) { sPixel = this.getPixel(sX,sY); tPixel = target.getPixel(tX,tY); tPixel.setColor(sPixel.getColor()); }

Using parameters In Picture.java… public void copyRegionTo (Picture target, int xSource, int ySource, int xTarget, int yTarget ) { // Body omitted to save space } In main… Picture fish = new Picture( "fish.jpg" ); Picture blank = new Picture(); Write the code to copy the square at position (10, 50) in fish to the blank canvas at position (30, 30) A.fish.copyRegionTo(blank, 10, 50, 30, 30) B.fish.copyRegionTo(blank) C.fish.copyRegionTo()

Parameters and scope In Picture.java… public void copyRegionTo (Picture target, int xSource, int ySource, int xTarget, int yTarget ) { // Body omitted to save space } In main… Picture fish = new Picture( "fish.jpg" ); Picture blank = new Picture(); fish.copyRegionTo(blank, 10, 50, 30, 30); Variables only exist in the region they are defined. I.e. variables in main cannot be accessed in copyRegion and vice versa The region where a variable exists is called its scope

Parameters and scope In Picture.java… public void copyRegionTo (Picture target, int xSource, int ySource, int xTarget, int yTarget ) { Pixel sPixel, tPixel = null; for (int sX = xSource, tX = xTarget; sX < 100+xSource; sX++, tX++) { for (int sY = ySource, tY = yTarget; sY < 100+ySource; sY++, tY++) { sPixel = this.getPixel(sX,sY); tPixel = target.getPixel(tX,tY); tPixel.setColor(sPixel.getColor()); } In main… Picture fish = new Picture( "fish.jpg" ); Picture blank = new Picture(); fish.copyRegionTo(blank, 10, 50, 30, 30); Main’s variables fish blank

Parameters and scope In Picture.java… In main… Picture fish = new Picture( "fish.jpg" ); Picture blank = new Picture(); fish.copyRegionTo(blank, 10, 50, 30, 30); Main’s variables fish blank copyRegionTo’s variables target this startxSource startySource startxTarget startyTarget Also sX, tX, sY, sY, sPixel, tPixel fish blank public void copyRegionTo (Picture target, int xSource, int ySource, int xTarget, int yTarget ) { Pixel sPixel, tPixel = null; for (int sX = xSource, tX = xTarget; sX < 100+xSource; sX++, tX++) { for (int sY = ySource, tY = yTarget; sY < 100+ySource; sY++, tY++) { sPixel = this.getPixel(sX,sY); tPixel = target.getPixel(tX,tY); tPixel.setColor(sPixel.getColor()); }

Parameters and scope In Picture.java… In main… Picture fish = new Picture( "fish.jpg" ); Picture blank = new Picture(); fish.copyRegionTo(blank, 10, 50, 30, 30); Main’s variables fish blank public void copyRegionTo (Picture target, int xSource, int ySource, int xTarget, int yTarget ) { Pixel sPixel, tPixel = null; for (int sX = xSource, tX = xTarget; sX < 100+xSource; sX++, tX++) { for (int sY = ySource, tY = yTarget; sY < 100+ySource; sY++, tY++) { sPixel = this.getPixel(sX,sY); tPixel = target.getPixel(tX,tY); tPixel.setColor(sPixel.getColor()); }

Parameters and return values In Picture.java… public Picture copyRegionToNew(int xSource, int ySource, int xTarget, int yTarget ) { Picture newCanvas = new Picture(); Pixel sPixel, tPixel = null; for (int sX = xSource, tX = xTarget; sX < 100+xSource; sX++, tX++) { for (int sY = ySource, tY = yTarget; sY < 100+ySource; sY++, tY++) { sPixel = this.getPixel(sX,sY); tPixel = newCanvas.getPixel(tX,tY); tPixel.setColor(sPixel.getColor()); } In main… Picture fish = new Picture( "fish.jpg" ); Picture newCanvas = fish.copyRegionToNew(10, 30, 50, 50); newCanvas.show(); What error will the following code produce? A.This code will not compile B.The line “Picture newCanvas = fish.copyRegionToNew…” in main will cause an error C.The line newCanvas.show() will cause an error

Parameters and return values In Picture.java… public Picture copyRegionToNew(int xSource, int ySource, int xTarget, int yTarget ) { Picture newCanvas = new Picture(); Pixel sPixel, tPixel = null; for (int sX = xSource, tX = xTarget; sX < 100+xSource; sX++, tX++) { for (int sY = ySource, tY = yTarget; sY < 100+ySource; sY++, tY++) { sPixel = this.getPixel(sX,sY); tPixel = newCanvas.getPixel(tX,tY); tPixel.setColor(sPixel.getColor()); } In main… Picture fish = new Picture( "fish.jpg" ); Picture newCanvas = fish.copyRegionToNew(10, 30, 50, 50); newCanvas.show(); fish newCanvas Some of copyRegionTo’s variables main’s variables newCanvas this

Some comments on comments /* A method to copy a 100x100 region of the calling object’s * image to a blank canvas. * xSource, ySource: the upper left corner of the * region to be copied. * xTarget, yTarget: the upper left corner where the region * will appear in the new canvas. * returns a new canvas with the region copied into it. */ public Picture copyRegionToNew(int xSource, int ySource, int xTarget, int yTarget ) { Picture newCanvas = new Picture(); Pixel sPixel, tPixel = null; for (int sX = xSource, tX = xTarget; sX < 100+xSource; sX++, tX++) { for (int sY = ySource, tY = yTarget; sY < 100+ySource; sY++, tY++) { sPixel = this.getPixel(sX,sY); tPixel = newCanvas.getPixel(tX,tY); tPixel.setColor(sPixel.getColor()); } return newCanvas; } Header (method) comments required. Inline comments if necessary

Challenge: Using methods (to do cool stuff) public void copyRegionTo (Picture target, int xSource, int ySource, int xTarget, int yTarget ) Using the method copyRegionTo (header above) write a method that will copy a pattern of 20x20 squares from a source image to a target image (see below). Assume copyRegionTo has been modified to copy a 20x20 square instead of 100x100

TODO Reading for next class: Finish PSA4