140 likes | 155 Views
CSC1401 Manipulating Pictures 2. Recall last class. We introduced arrays We used a for loop to change each pixel’s color. What is an Array?. 0. 1. 2. 3. 4. 5. Storage for a sequence of items Of the same type You can access items by using an index The index starts at 0
E N D
Recall last class • We introduced arrays • We used a for loop to change each pixel’s color
What is an Array? 0 1 2 3 4 5 • Storage for a sequence of items • Of the same type • You can access items by using an index • The index starts at 0 • The first item is at index 0 • The last item is at index (length – 1) • Arrays know their length (have a public length field) • arrayObj.length 3 7 9 2 1 5 0 1 2 3 8 3 2 6
Turning all of the pixels in our picture to red – using a loop • Notes: • We have created a counter that starts at 0, and goes up to the number of pixels in the picture • Each pixel has its color set to red! • How would this code have looked had it been written as a method inside of the Picture class?
Another example • Removing the blue from our beach scene • How can we do this? • We’ll create another method in the Picture class
The removeBlue method in the Picture class Alternatively, we could have written this method to get the current amount of red and green, and then create a new color consisting of the existing amount of red and the existing amount of green, and 0 blue, and used the setColor method to change the color
Reviewing how a for loop works for (initialization; test; increment) { … } • We do the initialization step • Then we test to see if the expression is true • If it is, we do the code in the body of the loop, and then do the increment • We then do the test again. • If it is, we do the code in the body of the loop, and then do the increment • … This process continues until the test is false
The while loop • Chapter 4, Section 3 of the text uses a different loop – the while loop • The while loop is similar to the for loop (the important distinction is that there is no initialization or increment as part of the loop)
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
The while loop in practice • Generally, the while loop will have the format: initialization; while (test) { … } Note that somewhere in the body of the loop, we need to make sure that an increment occurs, to enable the test to eventually become false!
Using While • How could we re-write our removeBlue() method to use a while loop instead of a for loop?
A note on incrementing a variable • 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;
Assignment • Read Media Computation Chapter 4, Section 3