Georgia Institute of Technology Manipulating Pictures, Arrays, and Loops Barb Ericson Georgia Institute of Technology August 2005.

Slides:



Advertisements
Similar presentations
ManipulatingPictures-Mod6-part21 Manipulating Pictures, Arrays, and Loops part 2 Barb Ericson Georgia Institute of Technology.
Advertisements

TOPIC 12 CREATING CLASSES PART 1 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson,
Georgia Institute of Technology Manipulating Pictures, Arrays, and Loops part 1.
Georgia Institute of Technology Manipulating Pictures, Arrays, and Loops part 1 Barb Ericson Georgia Institute of Technology August 2005.
Conditionals-part11 Barb Ericson Georgia Institute of Technology Nov 2009.
Georgia Institute of Technology Introduction to Java part 2 Barb Ericson Georgia Institute of Technology May 2006.
Intro-Sound-part21 Introduction to Processing Digital Sounds part 2 Barb Ericson Georgia Institute of Technology Oct 2009.
01- Intro-Java-part1 1 Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology June 2008.
Georgia Institute of Technology Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology Aug 2005.
CreatingClasses-part11 Creating Classes part 1 Barb Ericson Georgia Institute of Technology Dec 2009.
Georgia Institute of Technology Creating Classes part 1 Barb Ericson Georgia Institute of Technology Oct 2005.
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.
Georgia Institute of Technology Declaring Variables – Mod 4 Barb Ericson Georgia Institute of Technology August 2005.
ManipulatingPictures-Mod6-part11 Manipulating Pictures, Arrays, and Loops part 1 Barb Ericson Georgia Institute of Technology.
TOPIC 6 MODIFYING PICTURES USING LOOPS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and.
ManipulatingPictures-Mod6-part61 Manipulating Pictures, Arrays, and Loops: Eliminating color, Inversion, grey scale and adjusting for luminance Barb Ericson.
NestedLoops-Mody7-part51 Two-Dimensional Arrays and Nested Loops – part 5 Rotations Barb Ericson Georgia Institute of Technology May 2007.
Georgia Institute of Technology Declaring Variables – Mod 4 Barb Ericson Georgia Institute of Technology August 2005.
CPSC1301 Computer Science 1 Chapter 4 Manipulating Pictures, Arrays, and Loops part 5.
Georgia Institute of Technology Creating Classes part 4 Barb Ericson Georgia Institute of Technology May 2006.
CreatingClasses-SlideShow-part41 Creating Classes part 4 Barb Ericson Georgia Institute of Technology Dec 2009.
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 Speed part 4 Barb Ericson Georgia Institute of Technology May 2006.
Georgia Institute of Technology Manipulating Pictures, Arrays, and Loops Barb Ericson Georgia Institute of Technology August 2005.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
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.
Georgia Institute of Technology Two-Dimensional Arrays and Nested Loops – part 2 Barb Ericson Georgia Institute of Technology August 2005.
Georgia Institute of Technology Introduction to Java, and DrJava part 1 Dr Usman Saeed Assistant Professor Faculty of Computing and Information Technology.
NestedLoops-Mod7-part61 Two-Dimensional Arrays and Nested Loops – part 6 Enlarge Barb Ericson Georgia Institute of Technology August 2005.
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
Barbara Ericson Georgia Tech Sept 2005
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
Creating and Modifying Text part 2
Barb Ericson Georgia Institute of Technology Dec 2009
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
Barb Ericson Georgia Institute of Technology August 2005
Workshop for Programming And Systems Management Teachers
Introduction to Processing Digital Sounds part 2
Manipulating Pictures, Arrays, and Loops part 5
Introduction to Java, and DrJava part 1
Manipulating Pictures, Arrays, and Loops part 4
Manipulating Pictures, Arrays, and Loops
Manipulating Pictures, Arrays, and Loops part 3
Introduction to Processing Digital Sounds part 3
Barb Ericson Georgia Institute of Technology Oct 2005
Workshop for Programming And Systems Management Teachers
Manipulating Pictures, Arrays, and Loops
Manipulating Pictures, Arrays, and Loops
Introduction to Java, and DrJava
Manipulating Pictures, Arrays, and Loops
Manipulating Pictures, Arrays, and Loops part 6
Barb Ericson Georgia Institute of Technology May 2006
More on Creating Classes
Manipulating Pictures, Arrays, and Loops
Introduction to Java, and DrJava
Introduction to Java, and DrJava part 1
Barb Ericson Georgia Institute of Technology Oct 2005
Manipulating Pictures, Arrays, and Loops part 6
Presentation transcript:

Georgia Institute of Technology Manipulating Pictures, Arrays, and Loops Barb Ericson Georgia Institute of Technology August 2005

Georgia Institute of Technology Learning Goals Understand at a conceptual and practical level –What is an algorithm? –What is a program? –How to translate an algorithm to code –What is a comment? –What happens if you multiply by 0.5 instead of divide by 2? –How to improve a method

Georgia Institute of Technology Our First Picture Manipulation Digital cameras can produce pictures that are too red Let’s write a method to modify all the pixels in a picture –Reduce the red value at each pixel by 50% Get the array of pixels from the picture Loop through all the pixels in the picture –Get the red value at the current pixel –Calculate the new red value –Set the red value of the current pixel to the new value

Georgia Institute of Technology Decrease Red Algorithm To decrease the red value in a picture by 50% 1.Get the array of pixels from the picture 2.We want to loop through all the pixels in a picture so we need to declare and initialize some variables 1.Set up an index to start at 0 3.Check if the index is less than the length of the array 1.Get the pixel at the current index from the array of pixels 2.Get the red value at the pixel 3.Divide the red value by 2 4.Set the red value at the pixel to the reduced red value 5.Increment the index and go back to step 3

Georgia Institute of Technology What is an Algorithm? An algorithm is a description of the steps needed to do a task –Can be written in English –A recipe is an algorithm A program is an implementation of an algorithm –in a particular computer language

Georgia Institute of Technology From Algorithm to Program (code) How do we get the array of pixels from the current picture object? –We have used Pixel[] pixelArray = picture.getPixels(); –But we want to get the array of pixels from the current object So we can use the keyword this Pixel[] pixelArray = this.getPixels(); –Or we can leave off the this Pixel[] pixelArray = getPixels();

Georgia Institute of Technology Loop Algorithm to Code How to write (code) the loop? –Use a while loop with a counter for the index starting at 0 int index = 0; –Loop while the index is less than the length of the array while (index < pixelArray.length) –Get the current pixel from the array of pixels for the current index Pixel pixelObj = pixelArray[index];

Georgia Institute of Technology Loop Algorithm to Code - Continued –Get the red value at the pixel int value = pixelObj.getRed(); –Divide the red value by 2 value = value / 2; –Set the pixel red value pixel.setRed(value); –Add one to (increment) the index index = index + 1;

Georgia Institute of Technology Decrease Red Method public void decreaseRed() { Pixel[] pixelArray = this.getPixels(); Pixel pixelObj = null; int index = 0; // loop through all the pixels while(index < pixelArray.length) { // get the current pixel pixelObj = pixelArray[index]; // get the red value int value = pixelObj.getRed(); // decrease the red value value = value / 2; // set the pixel red pixelObj.setRed(value); // increment the index index = index + 1; }

Georgia Institute of Technology Comments Comments are explanations of your code to help people understand your code –They are ignored by the compiler There are several kinds in Java // a comment that lasts to the end of the line /* a comment that can take up several lines until a */ /** a Javadoc comment that is used to create html documentation of your code */

Georgia Institute of Technology Decrease Red Exercise In DrJava –Add the method decreaseRed() to Picture.java Before the last } which ends the class definition –Compile the method Click the Compile All button –Test it by doing the following in the interactions pane > String fileName = "C:/intro-prog-java/mediasources/caterpillar.jpg"; > Picture picture1 = new Picture(fileName); > picture1.explore(); > picture1.decreaseRed(); > picture1.explore();

Georgia Institute of Technology decreaseRed Method Result Before method After method

Georgia Institute of Technology Using Multiplication by 0.5 You could have also multiplied the red value by 0.5 –value = value * 0.5; Change the line that divided by 2 and try to compile this –In the decreaseRed method

Georgia Institute of Technology Loss of Precision If you try the code on the previous slide you will get a compiler error –Possible loss of precision It is complaining about putting a double value into a int variable –Loss of fractional part

Georgia Institute of Technology Casting to Solve Loss of Precision Error It will compile if we tell the compiler we know about the possible loss of precision –And that it is intended By using a cast to int value = (int) (value * 0.5); Notice that we cast the result of the multiplication back to an integer Casting is forcing a value into a type (type) expression

Georgia Institute of Technology Move Declarations Outside Loops When you need a variable in a loop it is best to declare it before the loop –Otherwise you are declaring a new variable each time through the loop Which is slower than just changing the value associated with the variable In some languages you must declare all variables at the beginning of a method (function) In Java you can declare variables anywhere in a method –As long as you declare them before you use them

Georgia Institute of Technology Shortcuts for Common Operations In programming you often need to add one to a value index = index + 1; You may use the shortcut index++; or ++index; If you wanted to subtract 1 instead index = index – 1; index--; or -- index;

Georgia Institute of Technology Decrease Red Method Version 2 /** * Method to decrease the red * by 50% in a picture */ public void decreaseRed() { Pixel[] pixelArray = this.getPixels(); Pixel pixelObj = null; int index = 0; int value = 0; // loop through all the pixels while(index < pixelArray.length) { // get the current pixel pixelObj = pixelArray[index]; // get the red value value = pixelObj.getRed(); // decrease the red value value = (int) (value * 0.5); // set the pixel red pixelObj.setRed(value); // increment the index index++; }

Georgia Institute of Technology Decrease Red Exercise In DrJava –Modify the decreaseRed method to match version 2 –Compile the method Click the Compile All button –Test it by doing the following in the interactions pane > String fileName = "C:/intro-prog-java/mediasources/caterpillar.jpg"; > Picture picture1 = new Picture(fileName); > picture1.explore(); > picture1.decreaseRed(); > picture1.explore();

Georgia Institute of Technology Summary An algorithm is a description of how to solve a problem A program implements an algorithm in a programming language A comment is an explanation of your code to help people understand it –Comments are ignored by the computer If you multiply by a floating point number you can’t put this in an integer variable –Unless you cast to integer You should declare variables before a while loop