CPSC1301 Computer Science 1 Chapter 4 Manipulating Pictures, Arrays, and Loops part 5.

Slides:



Advertisements
Similar presentations
Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
Advertisements

ManipulatingPictures-Mod6-part21 Manipulating Pictures, Arrays, and Loops part 2 Barb Ericson Georgia Institute of Technology.
Georgia Institute of Technology Movies part 3 Barb Ericson Georgia Institute of Technology April 2006.
Georgia Institute of Technology Manipulating Pictures, Arrays, and Loops part 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:
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
Conditionals-part11 Barb Ericson Georgia Institute of Technology Nov 2009.
Intro-Sound-part21 Introduction to Processing Digital Sounds part 2 Barb Ericson Georgia Institute of Technology Oct 2009.
Programming with C# Iteration LECTURE 3. Summary of last lecture SequenceSelectionif and switch statementsCastingRandom.
ManipulatingPictures-part11 Manipulating Pictures, Arrays, and Loops part 1 Barb Ericson Georgia Institute of Technology Nov 2009.
Georgia Institute of Technology Movies part 5 Barb Ericson Georgia Institute of Technology April 2006.
TOPIC 11 RETURNING VALUES FROM METHODS PICTURE TRANSFORMATIONS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach.
ManipulatingPictures-Mod6-part11 Manipulating Pictures, Arrays, and Loops part 1 Barb Ericson Georgia Institute of Technology.
Georgia Institute of Technology Manipulating Pictures, Arrays, and Loops Barb Ericson Georgia Institute of Technology August 2005.
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/
NestedLoops-Mod7-part31 Two-Dimensional Arrays and Nested Loops – part 3 Bugs in the garden Originally by Barb Ericson Georgia Institute of Technology.
ManipulatingPictures-Mod6-part61 Manipulating Pictures, Arrays, and Loops: Eliminating color, Inversion, grey scale and adjusting for luminance Barb Ericson.
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.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
CPSC1301 Computer Science 1 Chapter 8 Introduction to Processing Digital Sounds part 3.
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.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Georgia Institute of Technology Creating Classes part 2 Barb Ericson Georgia Institute of Technology June 2006.
Georgia Institute of Technology What is new in Java 5.0 (1.5)? Barb Ericson Georgia Institute of Technology June 2006.
Georgia Institute of Technology Manipulating Pictures, Arrays, and Loops Barb Ericson Georgia Institute of Technology August 2005.
Week 10 - Wednesday.  What did we talk about last time?  Method example  Roulette simulation  Types in Java.
CMSC 150 LOOPS CS 150: Fri 20 Jan Representing DNA AGTCCAGTGTCAA.
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.
Intro-Sound-Mod10-part31 Introduction to Processing Digital Sounds part 3 while loop, tracing, for loop, parameters Barb Ericson Georgia Institute of Technology.
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.
Barbara Ericson Georgia Tech Sept 2005
Chapter 4 Repetition Statements (loops)
Manipulating Pictures, Arrays, and Loops part 2
Manipulating Pictures, Arrays, and Loops part 2
Topic 6 Modifying Pictures Using Loops
Manipulating Pictures, Arrays, and Loops part 3
Manipulating Pictures, Arrays, and Loops part 2
Manipulating Pictures, Arrays, and Loops part 2
Georgia Institute of Technology
Manipulating Pictures, Arrays, and Loops
Barb Ericson Georgia Institute of Technology August 2005
Two-Dimensional Arrays and Nested Loops – part 1
Barb Ericson Georgia Institute of Technology August 2005
Workshop for Programming And Systems Management Teachers
Outline Altering flow of control Boolean expressions
Introduction to Processing Digital Sounds part 2
Manipulating Pictures, Arrays, and Loops part 5
Two-Dimensional Arrays and Nested Loops – part 1
CSE 8A Lecture 6 Reading for next class:
Manipulating Pictures, Arrays, and Loops
Manipulating Pictures, Arrays, and Loops part 3
Introduction to Processing Digital Sounds part 3
Variables variable: A piece of the computer's memory that is given a name and type, and can store a value. Like preset stations on a car stereo, or cell.
Manipulating Pictures, Arrays, and Loops
Manipulating Pictures, Arrays, and Loops
Building Java Programs
Georgia Institute of Technology
Manipulating Pictures, Arrays, and Loops
Manipulating Pictures, Arrays, and Loops part 6
Building Java Programs
Barb Ericson Georgia Institute of Technology May 2006
Manipulating Pictures, Arrays, and Loops
Building Java Programs
CSC1401 Manipulating Pictures 2
Manipulating Pictures, Arrays, and Loops part 6
Presentation transcript:

CPSC1301 Computer Science 1 Chapter 4 Manipulating Pictures, Arrays, and Loops part 5

Learning Goals Understand at a conceptual and practical level  How to change more than one color in a method  How to use a for loop  How to print out values in a loop  How to convert a while loop into a for loop

Faking a Sunset If you want to make an outdoor scene look like it happened during sunset  You might want to increase the red But you can’t increase past 255  Another idea is to reduce the blue and green To emphasize the red Try to reduce the blue and green by 30%

Faking a Sunset Algorithm Reduce the blue and green by 30% 1. Get the array of pixels from the picture 2. Set up an index to start at 0 3. Loop while the index is less than the length of the array 1. Get the pixel at the current index from the array of pixels 2. Set the blue value at the pixel to 0.7 times the original value 3. Set the green value at the pixel to 0.7 times the original value 4. Increment the index and go back to step 3

Faking a Sunset Method /** * Method to simulate a sunset by * decreasing the green * and blue */ public void makeSunset() { Pixel[] pixelArray = this.getPixels(); Pixel pixelObj = null; int value = 0; int i = 0; // loop through all the pixels while (i < pixelArray.length) { // get the current pixel pixelObj = pixelArray[i]; // change the blue value value = pixelObj.getBlue(); pixelObj.setBlue((int) (value * 0.7)); // change the green value value = pixelObj.getGreen(); pixelObj.setGreen((int) (value * 0.7)); // increment the index i++; }

Testing makeSunset String file = “c:/intro-prog-java/mediasources/beach.jpg”; Picture pictureObj = new Picture(file); pictureObj.explore(); pictureObj.makeSunset(); pictureObj.explore();

For Loops Programmers like shortcuts  Especially those that reduce errors  And mean less typing We have been using a while loop with an index  We had to declare the index variable and initialize it before the loop If you forget this there will be a compiler error  We had to increment the index in the loop If you forget this it will be an infinite loop The shortcut for this is a For Loop

For Loop Syntax for (initialization area; continuation test; change area)  Initialization area Declare variables and initialize them  Continuation test If true do body of loop If false jump to next statement after the loop  Change area Change the loop variables  Increment or decrement them

Comparison of While and For Loops int index = 0; while (index < pixelArray.length) { statements. index++; } for (int i=0; i < pixelArray.length; i++) { statements. }

Change clearBlue() to use a For Loop /** * Method to clear the blue (set * the blue to 0 for all pixels) */ public void clearBlue() { Pixel pixelObj = null; // get the array of pixels Pixel[] pixelArray = this.getPixels(); // loop through all the pixels for (int i = 0; i < pixelArray.length; i++) { // get the current pixel pixelObj = pixelArray[i]; // set the blue on the pixel to 0 pixelObj.setBlue(0); }

Using System.out.println() in a Loop One way to check what is happening in your program is to add  System.out.println(expression); You might add this to the loop to check the value of ‘i’ during it.  And to verify that the increment happens after the last statement in the loop

Change to For Loop Exercise Edit makeSunset() and change it from using a while loop to using a for loop  Move the declaration of the index to the for loop initialization area  Move the index increment to the for loop change area  Execute the code to make sure it still works

Summary You can change more than one color in a method  Like in make sunset You can use a for loop to repeat a series of Java statements until a test is false for (initialization area; continuation test; change area)  Executes the same way a while loop does But less error prone when executing a loop at set number of times  You can convert code that uses a while loop into using a for loop  You can print values in a loop using System.out.println(expression);