Manipulating Pictures Arrays and Loops part 2 Barb
Manipulating Pictures, Arrays, and Loops part 2 Barb Ericson Georgia Institute of Technology Nov 2009 Manipulating. Pictures-part 2 1
Learning Goals • Understand at a conceptual and practical level – What is a while loop? – How to translate an algorithm to code – What is a comment? – How to trace a method Manipulating. Pictures-part 2 2
Loop Exercise • Ask a person to clap 12 times – How does s/he know when to stop? – What changes each time s/he claps? • If you are following a recipe that asks you to stir the ingredients 50 times how would you do this? • What if you were trying to break a sit-up record – How would you know if you did break it? Manipulating. Pictures-part 2 3
Loops often need Counters • If you want to do something x times you often need a counter – That starts at 0 – And you add 1 to it each time you finish doing the thing you are repeating – When the counter reaches the number you are trying to do you stop the loop • What is the value of the counter the last time the statements of the loop are executed? Manipulating. Pictures-part 2 4
While Loops • In Java one way to repeat a block of statements while an expression is true is to use a while loop • Create a counter and set it to the start value • Check that the counter is less then the stop value • If it is less than execute the statements in the loop • Add one to the counter and go back to check that the counter is less than the stop value Manipulating. Pictures-part 2 5
Total the Numbers from 1 to 100 • What if you want to add all the numbers from 1 to 100? – You will need something to hold the total • What type should it be? • What value should it start out with? – You will need something that counts from 1 to 100 • And add that value to the total • Stop when you get to 100 • What type should it be? What value should it start with? Manipulating. Pictures-part 2 6
While Loop Syntax • Adding up the numbers from 1 to 100 int total = 0; int num = 1; while (num <= 100) { total = total + num; num = num + 1; } System. out. println(total); Manipulating. Pictures-part 2 7
While Loop Syntax • Adding up the numbers from 1 to 100 int total = 0; // declare and initialize the total int num = 1; // declare and init the number while (num <= 100) // do while num <= 100 { total = total + num; // add num to total num = num + 1; // increment the num } System. out. println(total); // print the total Manipulating. Pictures-part 2 8
Parts of a While Loop • Adding up the numbers from 1 to 100 int total = 0; Declaration and initialization of variables int num = 1; This test is done each time and when while (num <= 100) It is true the loop body will be executed { This is the body of the loop. It total = total + num; Starts with a ‘{‘ and ends with a ‘}’. If there is just one statement num = num + 1; In a loop body the ‘{‘ and ‘}’ aren’t needed. } System. out. println(total); Manipulating. Pictures-part 2 9
Exercise • Have students walk through this flowchart int count = 0; int dice. Value = roll. Dice(); Roll a die to get this value false while (dice. Value > count) true System. out. println("My name is " + name); Sytem. out. println("The count is " + count); count = count + 1 ; System. out. println("After the loop the count is " + count); Manipulating. Pictures-part 2 10
Changing from For-each to While • In a for-each loop something needs to keep track of the current pixel – And change each time through the loop – To sure that we have gone through all of the pixels • We can loop through all elements in an array by starting with index 0, then index 1, and so on till index (length – 1) – And get the pixel at the current index value Manipulating. Pictures-part 2 11
Reduce Red Algorithm • Get the array of Pixel objects from the current picture • Declare a variable to hold the red value • Declare the index variable and set it to 0 • Declare a variable to refer to the current pixel • Loop while index is less than the length of the array – – – Get the pixel at the index value Get the current red value from the pixel Divide the current red value by 2 Set the red for the current pixel to the changed value Increment the index Manipulating. Pictures-part 2 12
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; – Add a variable to refer to the current pixel Pixel pixel. Obj = null; – Loop while the index is less than the length of the array while (index < pixel. Array. length) – Get the current pixel from the array of pixels for the current index pixel. Obj = pixel. Array[index]; Manipulating. Pictures-part 2 13
Loop Algorithm to Code - Continued – Get the red value at the pixel value = pixel. Obj. get. Red(); – Divide the red value by 2 value = value / 2; – Set the pixel red value pixel. set. Red(value); – Add one to (increment) the index • index = index + 1; Manipulating. Pictures-part 2 14
Decrease Red Method public void decrease. Red() { Pixel[] pixel. Array = this. get. Pixels(); int value = 0; Pixel pixel. Obj = null; int index = 0; // loop through all the pixels while(index < pixel. Array. length) { // get the current pixel. Obj = pixel. Array[index]; // get the red value = pixel. Obj. get. Red(); // decrease the red value = value / 2; // set the pixel red pixel. Obj. set. Red(value); // increment the index = index + 1; } Manipulating. Pictures-part 2 15
Method Names • If we add this new method to Picture. java and compile will we get an error? – We will have two methods with the same name and both take no parameters – We can have two methods with the same name • As long as the parameter list is different • We can also name one of them differently – Rename the first one to decrease. Red. For. Each Manipulating. Pictures-part 2 16
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 */ Manipulating. Pictures-part 2 17
Decrease Red Exercise • In Dr. Java – Add the method decrease. Red() to Picture. java • Before the last } which ends the class definition – Compile the method • Click the Compile All button – Test it with the following String file. Name = "C: /intro-prog-java/mediasources/caterpillar. jpg"; Picture picture 1 = new Picture(file. Name); picture 1. explore(); picture 1. decrease. Red(); picture 1. explore(); Manipulating. Pictures-part 2 18
decrease. Red Method Result • Before method • After method Manipulating. Pictures-part 2 19
Put 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 – They are known in the block they are declared in Manipulating. Pictures-part 2 20
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; Manipulating. Pictures-part 2 21
Tracing Code • An important skill to develop is the ability to trace code – Also called walking through or stepping through your code – Look at each line and predict what will happen – Show the variables and their values Manipulating. Pictures-part 2 22
Step Through decrease. Red() • A picture object was created from the file “caterpillar. jpg” and then was sent the message decrease. Red() • The picture object was implicitly passed to the method decrease. Red() and can be referred to by this • The array of pixel objects was returned from sending get. Pixels() to the picture object Pixel[] pixel. Array = this. get. Pixels(); • Some variables were declared for later use in the loop Pixel pixel. Obj = null; int index = 0; int value = 0; Manipulating. Pictures-part 2 23
Step Through decrease. Red() - cont • The while loop tests if the index is less than the length of the array while (index < pixel. Array. length) { – And if so it executes the statements in the body of the loop {} • It sets the variable pixel. Obj to the pixel at the current index in the array of pixels pixel. Obj = pixel. Array[index]; • It gets the red value of that pixel value = pixel. Obj. get. Red(); • it sets the value to the integer part of (red value * 0. 5) value = (int) (value * 0. 5); • It sets the pixel’s red to the new value pixel. Obj. set. Red(value); • It increments the index value index++; Manipulating. Pictures-part 2 24
Memory Map of decrease. Red() width=329 height=150 • What does memory this look like the first time through the loop? • How about the 2 nd pixel. Array R=252, G=254, time through? B=251, X=0, pixel rd • How about the 3 Y=0 time through? • How about the last value = 252 time through? index = 0 Picture: Class get. Pixels() … R=253, R=254, G=255, G=254, B=254, X=1, X=2, Y=0 … Pixel: Class get. Red() set. Red()… Manipulating. Pictures-part 2 25
Summary • A while loop can be used to repeat a block of statements while a condition is true • 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 • You should declare variables before the body of the loop – That you will need in the loop • Being able to trace your code is an essential skill Manipulating. Pictures-part 2 26
- Slides: 26