CSE 8A Lecture 6 Reading for next class:

Slides:



Advertisements
Similar presentations
CS2984: Introduction to Media Computation Drawing directly on images.
Advertisements

Peer Instruction for the First Time: Experiences of a First Time User in Computer Science Beth Simon Computer Science and Engineering University of California,
Introduction to Computers and Programming Lecture 9: For Loops New York University.
Workshop on Peer Instruction: Making Good Clicker Questions Beth Simon Computer Science and Engineering University of California, San Diego Credits: Sara.
Loops – While, Do, For Repetition Statements Introduction to Arrays
1 CS 177 Week 15 Recitation Slides Review. Announcements Final Exam on Sat. May 8th  PHY 112 from 8-10 AM Complete your online review of your classes.
CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.
Games and Simulations O-O Programming in Java The Walker School
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)
Iteration. Adding CDs to Vic Stack In many of the programs you write, you would like to have a CD on the stack before the program runs. To do this, you.
NestedLoops-part11 Nested Loops – part 1 Barb Ericson Georgia Institute of Technology Nov 2009.
Creative Commons Attribution Non-Commercial Share Alike License sa/3.0/
Creative Commons Attribution Non-Commercial Share Alike License sa/3.0/
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.
CSE8A Lecture2 Check the class web page (and discussion board, linked form webpage) for news and info: cse8a.weebly.com Lab starts this week. Discussion.
Creative Commons Attribution Non-Commercial Share Alike License sa/3.0/
11 Working with Images Session Session Overview  Find out more about image manipulation and scaling when drawing using XNA  Start to implement.
Creative Commons Attribution Non-Commercial Share Alike License sa/3.0/
TOPIC 6 MODIFYING PICTURES USING LOOPS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and.
Creative Commons Attribution Non-Commercial Share Alike License sa/3.0/
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
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:
CPSC1301 Computer Science 1 Chapter 4 Manipulating Pictures, Arrays, and Loops part 5.
Often being different. Control flow By default Java (and therefore Processing) executes lines of a program one after the other –Doesn’t matter what happened.
CSE 110 Review Session Hans Hovanitz, Kate Kincade, and Ian Nall.
Copyright © Curt Hill Further Picture Manipulation Considering position.
CSE 8A Lecture 13 Reading for next class: Chapter 9 Today’s topics: –Sounds! Finish PSA 6: Chromakey! DUE TUESDAY Interm exam 3 next Thursday.
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)
Creative Commons Attribution Non-Commercial Share Alike License sa/3.0/
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.
ManipulatingPictures-part31 Manipulating Pictures, Arrays, and Loops part 3 Barb Ericson Georgia Institute of Technology Nov 2009.
04-ManipulatingPictures-part21 Manipulating Pictures, Arrays, and Loops part 2 Barb Ericson Georgia Institute of Technology June 2008.
CompSci 4 Java 4 Apr 14, 2009 Prof. Susan Rodger.
1. Understanding the execution of local variable declaration (in a method body) new expression ( 3 steps ) method call (method frames, call stack) examples.
Barbara Ericson Georgia Tech Sept 2005
Pixels, Colors and Shapes
User-Written Functions
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Manipulating Pictures, Arrays, and Loops part 2
Manipulating Pictures, Arrays, and Loops part 2
Topic 6 Modifying Pictures Using Loops
CSE 8A Lecture 17 Reading for next class: None (interm exam 4)
Creative Commons Attribution Non-Commercial Share Alike License
Manipulating Pictures, Arrays, and Loops part 3
EGR 2261 Unit 4 Control Structures I: Selection
Chapter 5: Control Structures II
Loop Structures.
Lecture 4: Program Control Flow
Topics Introduction to Repetition Structures
Manipulating Pictures, Arrays, and Loops part 2
Manipulating Pictures, Arrays, and Loops part 2
CS 177 Week 15 Recitation Slides
Manipulating Pictures, Arrays, and Loops
Manipulating Pictures, Arrays, and Loops part 5
Georgia Institute of Technology
Topic 1: Problem Solving
Manipulating Pictures, Arrays, and Loops part 3
Manipulating Pictures, Arrays, and Loops
Manipulating Pictures, Arrays, and Loops
How to Start This PowerPoint® Tutorial
Creative Commons Attribution Non-Commercial Share Alike License
February , 2009 CSE 113 B.
Creative Commons Attribution Non-Commercial Share Alike License
Topics Introduction to Repetition Structures
IPC144 Introduction to Programming Using C Week 4 – Lesson 2
More on Creating Classes
Manipulating Pictures, Arrays, and Loops
Announcements Lab 3 was due today Assignment 2 due next Wednesday
Loops CGS3416 Spring 2019 Lecture 7.
Agenda for Unit 3: Functions
Presentation transcript:

CSE 8A Lecture 6 Reading for next class: 5.2.1-5.2.2 PSA 3 is up now: Get started! Color Subtraction and TriColor effect Compile and run your code JUST BEFORE you submit. You will lose points (lots) if your code doesn’t run. CLICKERS OUT!

1.  What line of code would you use to get a Pixel located at (50,50) from an already defined Picture object named pictObj? A. pictObj.getPixel(int 50, int 50); B. pictObj.getPixel(50,50); C. pictObj.getPixels(); D. pictObj.getPixels[50][50]; Tuesday, Oct 16 Topic: Locating a pixel in the image READING QUIZ – NO TALKING – CORRECTNESS MATTERS

2. In the following code what update is done at the end of each loop iteration? for (int index = 0; index < 5; index++) { //Some code here } A. The value of index doesn’t change B. The value of index is increased by 1 C. The value of index is increased by 2 D. This code produces an error Tuesday, Oct 16 Topic: Understanding of for loops READING QUIZ – NO TALKING – CORRECTNESS MATTERS

3. Can you have a for-loop in the body of another for-loop? A. Yes you can B. Yes, but you need special code at the top of the file to be able to do it. C. No, that will give you a compiler error. Tuesday, Oct 16 Topic: Understanding of nested for loops READING QUIZ – NO TALKING – CORRECTNESS MATTERS

4. I have a Picture with a height of 300 pixels and width of 300 pixels. If I want to horizontally mirror the Pixel object located at (100,100) which Pixel object would I have to edit? Tuesday, Oct 16 Topic: Mirroring A. The Pixel object located at (100, 0) B. The Pixel object located at (0, 100) C. The Pixel object located at (100,199) D. The Pixel object located at (199,199) READING QUIZ – NO TALKING – CORRECTNESS MATTERS

Exam 1 Median: 85.0 High score: 10/10 (103 people scored this!) If you scored lower than 7.5: Don’t panic, you’re lowest score is dropped Get help! Do the reading, with a partner Make sure you really REALLY understand everything

You may not be really understanding… Suggestions: PLAY with code in the compiler. Ask yourself – what will happen if I change this variable value from 5 to 10? Why? Type in code from book and change it. But don’t just accept that you can type it in. Do you know WHY greyScale works? Do you know WHY negative works? Read the book with someone else in class. Talk after every page. Try to explain the text to the other person in your own words Don’t “just read” it and think, yeah I get that (you don’t) Redo the labs

Exam 1 Review (1 pt) Why will the following code cause an error? Assume these statements appear consecutively. Circle the correct response:   World world1 = new World(200, 100); Turtle nyla = new Turtle( world1 ); nyla = new Picture(); The value assigned to the variable nyla is the wrong type The variable nyla is declared more than once The constructor for Picture needs an argument (the name of a file) Once the variable nyla is assigned a value, it cannot be reassigned to a new value

Exam 1 Review Your task is to write a method called drawRect that can be called on an object of type Turtle. The method takes no parameters, and returns nothing. The method draws a rectangle whose shorter sides are 50 steps long and whose longer sides are 100 steps long.   (1 pt) In what file must you define this method? Turtle.java Turtle.class Turtle File.java

Exam 1 Review What is wrong with the following definition for drawRect?  public void drawRect() { World world1 = new World(200, 100); Turtle maria = new Turtle(world1); maria.forward(100); maria.turnLeft(); maria.forward(50); } Nothing is wrong You shouldn’t create a World object inside the method You shouldn’t create a Turtle object inside the method Both B and C

Exam 1 Review Turtle.drawRect(); new Turtle.drawRect(); (1 pt) You have a Turtle object stored in a variable named myTurtle. What line of code will call the method drawRect on that Turtle object? Turtle.drawRect(); new Turtle.drawRect(); myTurtle.drawRect(); myTurtle.drawRect

Exam 1 Review True 101 True 42 False 101 42 101 42 42 (2 pts) What does the following code print? Write what is printed on the blank line next to the line of code that prints it. Assume this code is executed in order. int num1 = 42; int num2 = 64; num2 = num1; System.out.println( num1 == num2 ); _____________________ num1 = 101; System.out.println( num2 ); __________________________   True 101 True 42 False 101 42 101 42 42

Exam 1 Review World world1 = new World(200,200); Turtle maria = new Turtle(50, 100, world1); Turtle jose = new Turtle(100, 100, world1); jose = maria; maria = new Turtle( 150, 100, world1 ); maria.turn(180); jose.forward(60); maria.forward(100); maria jose

Swap red and blue values of each pixel Solo: (1 min ) Discuss: (2min) Group: (30 sec) Swap red and blue values of each pixel Pixel[] pixelArray = this.getPixels(); int value = 0; int index = 0; while (index < pixelArray.length) { Pixel pix = pixelArray[index]; <<CODE GOES HERE>> index++; } value = pix.getRed(); pix.setRed(pix.getBlue()); pix.setBlue(value); value = pix.getRed(); pix.setBlue(pix.getRed()); pix.setRed(value); C. A. value = pix.getRed(); pix.setBlue(value); pix.setRed(pix.getBlue()); value = pix.getRed(); pix.setRed(value); pix.setBlue(pix.getRed()); B. D.

Why does this code have an error? Solo: (30 sec) Discuss: (2min) Group: (30 sec) Why does this code have an error? Pixel[] pixelArray = this.getPixels(); Pixel p; Pixel q; for(int index = 0; index < pixelArray.length; index++) { p = pixelArray[index]; q = pixelArray[index+1]; p.setRed(q.getRed()); p.setBlue(q.getRed()); p.setGreen(q.getGreen()); } It tries to access pixelArray[-1] It tries to access pixelArray[0] It tries to access pixelArray[pixelArray.length] It tries to access pixelArray[pixelArray.length+1] None of the above

CS Concept: Variable Tracing Pixel[] pixelArray = this.getPixels(); Pixel p; Pixel q; for(int index = 0; index < pixelArray.length; index++) { p = pixelArray[index]; q = pixelArray[index+1]; p.setRed(q.getRed()); p.setBlue(q.getRed()); p.setGreen(q.getGreen()); } pixelArray 0 1 2 3 4 5 index

What picture most accurately describes what this code does ? Solo: (30 sec) Discuss: (2min) Group: (30 sec) What picture most accurately describes what this code does ? Pixel[] pixelArray = this.getPixels(); Pixel p; Pixel q; for(int index = 0; index < pixelArray.length-1; index++) { p = pixelArray[index]; q = pixelArray[index+1]; p.setRed(q.getRed()); p.setBlue(q.getBlue()); p.setGreen(q.getGreen()); } A. D. None of these B. C.

Fill in the for(……) to loop over pixels bottom right to top left Like this: etc. next first Pixel[] pixArr = this.getPixels(); for ( ) { //Some code doing set on pixArr[i] }

Solo: (30 sec) Discuss: (2min) Group: (30 sec) while vs. for vs. for each True or False: The following code could be written using a for-loop. Pixel[] pixelArray = this.getPixels(); int index = 0; while ( index < pixelArray.length ) { Pixel pix = pixelArray[index]; pix.setGreen(255); index = index + 1; } True False

while for for each Which do you prefer? Why? Pixel[] pixelArray = this.getPixels(); int index = 0; while ( index < pixelArray.length ) { Pixel pix = pixelArray[index]; pix.setGreen(255); index = index + 1; } for Pixel[] pixelArray = this.getPixels(); for ( int index = 0; index < pixelArray.length; index++ ) { Pixel pix = pixelArray[index]; pix.setGreen(255); } for each Pixel[] pixelArray = this.getPixels(); for ( Pixel pix: pixelArray ) { pix.setGreen(255); } Which do you prefer? Why?

Solo: (30 sec) Discuss: (2min) Group: (30 sec) while vs. for vs. for each So when to use each? Sometimes it’s a matter of style, sometimes it’s ease of functionality Choose the best loop to use in each of these situations For For each While You want to loop through a picture until you find a pixel that is all black. Then you want your loop to stop. You want to loop through all the pixels in a picture and set their red value equal to their green value You want loop through the pixels in the first half of the picture and make them all black.

while vs. for vs. for each: Summary Less chance of error for each Use when you know you need to access and modify each pixel directly for Use when you need to loop through a known number of pixels and need access to their index value while Use when you are not sure how many pixels to loop through Greater chance of error Often, though, you can just choose the loop you like best

Nested Loops: How do they work? What order are pixels changed? Solo: (30 sec) Discuss: (2min) Group: (30 sec) Nested Loops: How do they work? What order are pixels changed? A method in Picture.java… what does it print if width is 2 and height is 3? Pixel p; for (int foo = 0; foo < getWidth(); foo++) { for (int bar = 0; bar < getHeight(); bar++) System.out.println( foo.toString() +“ “+ bar.toString() ); } turns the variable into a string for printing A 0 0 0 1 1 0 1 1 2 0 2 1 B. 0 0 1 0 2 0 0 1 1 1 2 1 C. 0 0 0 1 0 2 1 0 1 1 1 2 D. 0 0 1 1 2 2

Nested Loops: Tracing code A method in Picture.java… what does it print if width is 2 and height is 3? Pixel p; for (int foo = 0; foo < getWidth(); foo++) { for (int bar = 0; bar < getHeight(); bar++) System.out.println( foo.toString() + bar.toString() ); } turns the variable into a string for printing foo bar

Nested Loops: How do they work? What order are pixels changed? Solo: (30 sec) Discuss: (2min) Group: (30 sec) Nested Loops: How do they work? What order are pixels changed? A method in Picture.java… Pixel p; for (int foo = 0; foo < getWidth(); foo++) { for (int bar = 0; bar < getHeight(); bar++) p = getPixel(foo, bar); p.setColor(Color.BLACK); }

What do these Picture methods do? What are their return types? getPixel(int x, int y) getHeight() getWidth()

Nested Loops: How do they work? In what order are pixels changed? Solo: (30 sec) Discuss: (2min) Group: (30 sec) Nested Loops: How do they work? In what order are pixels changed? A method in Picture.java… Pixel p; for (int bar = 0; bar < getHeight(); bar++) { for (int foo = 0; foo < getWidth(); foo++) p = getPixel(foo, bar); p.setColor(Color.BLACK); }

Why does this have an error? Solo: (30 sec) Discuss: (2min) Group: (30 sec) Why does this have an error? In a method in Picture.java… assume height=50,width=100 Pixel p; for (int bar = 0; bar < getWidth(); bar++) { for (int foo = 0; foo < getHeight(); foo++) p = getPixel(foo, bar); p.setColor(Color.BLACK); } It doesn’t, this loops across rows, top to bottom It doesn’t, this loops down columns, left to right It tries to index a pixel off the end of a row (x value too big) It tries to index a pixel off the end of a column (y value too big)

TODO Get started and do PSA3 Read next class: 5.2.1-5.2.2 Do the reading quiz PLAY WITH CODE! Get stuck, then figure out how to get unstuck – it’s how you learn!