1 / 14

CSC1401 Manipulating Pictures 2

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

jsherman
Download Presentation

CSC1401 Manipulating Pictures 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSC1401Manipulating Pictures 2

  2. Recall last class • We introduced arrays • We used a for loop to change each pixel’s color

  3. 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

  4. 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?

  5. Another example • Removing the blue from our beach scene • How can we do this? • We’ll create another method in the Picture class

  6. 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

  7. 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

  8. 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)

  9. 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

  10. 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!

  11. Using While • How could we re-write our removeBlue() method to use a while loop instead of a for loop?

  12. removeBlue() using while

  13. 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;

  14. Assignment • Read Media Computation Chapter 4, Section 3

More Related