Manipulating Pictures, Arrays, and Loops part 2

Slides:



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

Georgia Institute of Technology Manipulating Pictures, Arrays, and Loops part 1.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Intro-Sound-part21 Introduction to Processing Digital Sounds part 2 Barb Ericson Georgia Institute of Technology Oct 2009.
ManipulatingPictures-part11 Manipulating Pictures, Arrays, and Loops part 1 Barb Ericson Georgia Institute of Technology Nov 2009.
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.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
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.
February ,  2/16: Exam 1 Makeup Papers Available  2/20: Exam 2 Review Sheet Available in Lecture  2/27: Lab 2 due by 11:59:59pm  3/2:
CPSC1301 Computer Science 1 Chapter 4 Manipulating Pictures, Arrays, and Loops part 5.
Counting Loops.
 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 Manipulating Pictures, Arrays, and Loops Barb Ericson Georgia Institute of Technology August 2005.
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 Introduction to Java, and DrJava part 1 Dr Usman Saeed Assistant Professor Faculty of Computing and Information Technology.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Georgia Institute of Technology Dr Usman Saeed Assistant Professor Faculty of Computing and Information Technology North Jeddah Branch King Abdulaziz University.
11 Making Decisions in a Program Session 2.3. Session Overview  Introduce the idea of an algorithm  Show how a program can make logical decisions based.
PHP Condtions and Loops Prepared by Dr. Maher Abuhamdeh.
Barbara Ericson Georgia Tech Sept 2005
Manipulating Pictures, Arrays, and Loops part 1
Scratch for Interactivity
Manipulating Pictures, Arrays, and Loops part 2
Topic 6 Modifying Pictures Using Loops
Introduction to Java part 2
Georgia Institute of Technology
Manipulating Pictures, Arrays, and Loops part 3
Georgia Institute of Technology
Barb Ericson Georgia Institute of Technology Dec 2009
Learning Java with Alice 3.0 Game Design Kathy Bierscheid
Manipulating Pictures, Arrays, and Loops part 2
Manipulating Pictures, Arrays, and Loops part 2
Georgia Institute of Technology
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Arrays, For loop While loop Do while loop
Manipulating Pictures, Arrays, and Loops
Barb Ericson Georgia Institute of Technology August 2005
Two-Dimensional Arrays and Nested Loops – part 1
Workshop for Programming And Systems Management Teachers
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 part 4
Manipulating Pictures, Arrays, and Loops
Manipulating Pictures, Arrays, and Loops part 3
Introduction to Media Computation
Manipulating Pictures, Arrays, and Loops part 1
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
Workshop for Programming And Systems Management Teachers
Manipulating Pictures, Arrays, and Loops
Manipulating Pictures, Arrays, and Loops
Manipulating Pictures, Arrays, and Loops
Manipulating Pictures, Arrays, and Loops part 6
February , 2009 CSE 113 B.
Barb Ericson Georgia Institute of Technology May 2006
Manipulating Pictures, Arrays, and Loops
Barb Ericson Georgia Institute of Technology Oct 2005
CSC1401 Manipulating Pictures 2
Manipulating Pictures, Arrays, and Loops part 6
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Looping and Repetition
Presentation transcript:

Manipulating Pictures, Arrays, and Loops part 2 Dr Usman Saeed Assistant Professor Faculty of Computing and Information Technology  North Jeddah Branch King Abdulaziz University Georgia Institute of Technology

Georgia Institute of Technology Learning Goals Understand at a conceptual and practical level What a 2-dimensional array is How to get a pixel at a given x and y location How to use the picture explorer Why we need some way to repeat a series of statements What a for-each loop is What a while loop is Georgia Institute of Technology

Georgia Institute of Technology Pictures are 2-D Arrays They have columns and rows (x and y) You can get a pixel at a particular x and y location Pixel pixelObj = picture.getPixel(x,y); The columns and rows start with index 0 end with num -1 X Y Georgia Institute of Technology

2-Dimensional Array Exercise Have two students play battleship. Have other students keep track of the play based on the rows and columns (I-5) or even switch this to (5-I) so that it is (x,y) Or play Bingo And let everyone have practice with looking up the right position Georgia Institute of Technology

Changing a Picture Exercise > import java.awt.Color; > String fileName = "C:/intro-prog-java/mediasources/caterpillar.jpg"; > Picture pictureObj = new Picture(fileName); > pictureObj.show(); > pictureObj.getPixel(10,100).setColor(Color.black); > pictureObj.getPixel(11,100).setColor(Color.black); > pictureObj.getPixel(12,100).setColor(Color.black); > pictureObj.getPixel(13,100).setColor(Color.black); > pictureObj.getPixel(14,100).setColor(Color.black); > pictureObj.getPixel(15,100).setColor(Color.black); > pictureObj.getPixel(16,100).setColor(Color.black); > pictureObj.getPixel(17,100).setColor(Color.black); > pictureObj.getPixel(18,100).setColor(Color.black); > pictureObj.getPixel(19,100).setColor(Color.black); > pictureObj.repaint(); Type the above code in the interactions pane and see what happens. You can use the up arrow to pull up the last statement executed in the interactions pane and then use the left arrow to get to the number to change. Georgia Institute of Technology

How do we Know if it Worked? A very important part of programming is testing the result Just because code compiles and runs without error doesn’t mean it is correct There could be an error in the logic It could fail under certain conditions It could even return the correct answer but for the wrong reason Georgia Institute of Technology

Georgia Institute of Technology The Picture Explorer Tool that creates a copy of the current picture and lets you explore it See the color, x, and y values at the cursor To use the tool on a picture object pictureObj.explore(); Use it to see if the colors have changed You can type in the X and Y value that you want to check the color of and press enter and it will display the red, green, and blue values at that position. The crosshair will also display at that position. Georgia Institute of Technology

Changing the Red in a Picture One way to change a picture is to reduce the amount of red in it What if we want to decrease it by half? If we have a value of 200 what should the new value be? How do we reduce any value by half? What if we want to increase it by 25%? If we have a value of 100 what should the new value be? How do we increase any value by 25%? To decrease the red value by half multiply it by 0.5 or divide it by 2. To increase the red value by 25% multiply it by 1.25. Georgia Institute of Technology

Changing all the Pixels in a Picture There are 329 * 150 = 49,350 pixels in the caterpillar picture Do we really want to write the code to change each one of these? Get the current pixel Get the red value of the current pixel Change the red value of the current pixel to half the original value (value / 2) Put the new red value in the current pixel Georgia Institute of Technology

We Need a Loop (Iteration) A way to execute a series of statements in the body of the loop With something changing each time the statements are executed Different pixel to change And some way to tell when we are done with the repetition Some test to see if the loop should stop Done with all of the items in an array Georgia Institute of Technology

Georgia Institute of Technology For-each Loop In Java if we want to do something to each item in an array We can use the for-each loop for (Type : variableName arrayName) { // body of the loop } Which means for each element in the array do the statements in the body of the loop Georgia Institute of Technology

Georgia Institute of Technology Method to Decrease Red Loop through all the pixels in an array of Pixel objects and change the red in each public void decreaseRed() { Pixel[] pixelArray = this.getPixels(); int value = 0; // loop through all the pixels in the array for (Pixel pixelObj : pixelArray) // get the red value value = pixelObj.getRed(); // decrease the red value by 50% (1/2) value = value / 2; // set the red value of the current pixel to the new value pixelObj.setRed(value); } The body of the loop is in {} Georgia Institute of Technology

Georgia Institute of Technology Testing decreaseRed Add decreaseRed to Picture.java Before the last } Compile Picture.java Click on Compile All Button Type the following in the interactions pane > String fileName = FileChooser.pickAFile(); > Picture pictObj = new Picture(fileName); > pictObj.explore(); > pictObj.decreaseRed(); Use the two picture explorers to compare the red values. Georgia Institute of Technology

Georgia Institute of Technology How This Works First we have the method declaration public void decreaseRed() This is a public method that doesn't return anything and the name is decreaseRed. This method does not take any parameters since there is nothing in the () Next we start the body of the method { Inside of the body of the method are the statements to be executed when the method is called (executed) Next we declare an array of pixels and get them from the current Picture object Pixel[] pixelArray = this.getPixels(); Next we declare a primitive variable to hold the current red value at the pixel int value = 0; Georgia Institute of Technology

Georgia Institute of Technology How This Works - Cont Next start the for-each loop for (Pixel pixelObj : pixelArray) { Each time through the loop set the variable pixelObj to refer to the next Pixel object in the array of Pixel objects called pixelArray. Execute the statements in the body of the loop. Get the red value from the pixelObj value = pixelObj.getRed(); Decrease the red by 1/2 value = value / 2; Set the red value at the pixelObj to the new value pixelObj.setRed(value); End the for-each loop body and the body of the method } Georgia Institute of Technology

Georgia Institute of Technology Exercise Write a method increaseRed() to loop through all the pixels in a picture and double the red values Multiply by 2 To try this method do the following: String fileName = FileChooser.pickAFile(); Picture pictObj = new Picture(fileName); pictObj.explore(); pictObj.increaseRed(); Georgia Institute of Technology

Georgia Institute of Technology 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? Georgia Institute of Technology

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? The counter is also often referred to as an index. Georgia Institute of Technology

Georgia Institute of Technology 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 picture is a flowchart which shows the order the statements are executed in a program. A while loop allows you to repeat a series of statements until an expression is false. Georgia Institute of Technology

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? Georgia Institute of Technology

Georgia Institute of Technology 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); When you type the above code into DrJava you will need to do Shift-Enter when you type the while loop code in (so that it doesn’t try to evaluate the while loop till you have all the code entered. Georgia Institute of Technology

Georgia Institute of Technology 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 When you type the above code into DrJava you will need to do Shift-Enter when you type the while loop code in (so that it doesn’t try to evaluate the while loop till you have all the code entered. Georgia Institute of Technology

Georgia Institute of Technology Parts of a While Loop 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); Declaration and initialization of variables This test is done each time and when It is true the loop body will be executed This is the body of the loop. It Starts with a ‘{‘ and ends with a ‘}’. If there is just one statement In a loop body the ‘{‘ and ‘}’ aren’t needed. When you type the above code into DrJava you will need to do Shift-Enter when you type the while loop code in (so that it doesn’t try to evaluate the while loop till you have all the code entered. Georgia Institute of Technology

Exercise Have students walk through this flowchart int count = 0; int diceValue = rollDice(); Roll a die to get this value while (diceValue < count) false 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); Georgia Institute of Technology

Georgia Institute of Technology Summary A 2d array has columns and rows You can get a pixel at a particular x and y location You can use a for-each loop to execute a series of statements on each item in an array You can use a while loop to repeat a series of Java statements while some test is true Often you will use a counter to get track of how many times the loop has executed Declare the counter before the loop and increment it at the end of the loop Georgia Institute of Technology