10-ManipulatingPictures-part2.ppt: uploaded 1 April 2016 at 4:01 pm

Download Report

Transcript 10-ManipulatingPictures-part2.ppt: uploaded 1 April 2016 at 4:01 pm

Manipulating Pictures, Arrays, and Loops
part 2
Barb Ericson
Georgia Institute of Technology
Nov 2009
ManipulatingPictures-part2
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
ManipulatingPictures-part2
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?
ManipulatingPictures-part2
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?
ManipulatingPictures-part2
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
ManipulatingPictures-part2
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?
ManipulatingPictures-part2
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);
ManipulatingPictures-part2
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
ManipulatingPictures-part2
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);
ManipulatingPictures-part2
9
Exercise
• Have students walk through this flowchart
int count = 0;
int diceValue = rollDice();
Roll a die to get this value
false
while (diceValue > 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);
ManipulatingPictures-part2
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
ManipulatingPictures-part2
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
ManipulatingPictures-part2
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 pixelObj = null;
– 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
pixelObj = pixelArray[index];
ManipulatingPictures-part2
13
Loop Algorithm to Code - Continued
– Get the red value at the pixel
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;
ManipulatingPictures-part2
14
Decrease Red Method
public void decreaseRed()
{
Pixel[] pixelArray = this.getPixels();
int value = 0;
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
value = pixelObj.getRed();
// decrease the red value
value = value / 2;
// set the pixel red
pixelObj.setRed(value);
// increment the index
index = index + 1;
}
ManipulatingPictures-part2
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 decreaseRedForEach
ManipulatingPictures-part2
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 */
ManipulatingPictures-part2
17
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 with the following
String fileName = "C:/intro-prog-java/mediasources/caterpillar.jpg";
Picture picture1 = new Picture(fileName);
picture1.explore();
picture1.decreaseRed();
picture1.explore();
ManipulatingPictures-part2
18
decreaseRed Method Result
• Before method
• After method
ManipulatingPictures-part2
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
ManipulatingPictures-part2
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;
ManipulatingPictures-part2
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
ManipulatingPictures-part2
22
Step Through decreaseRed()
• A picture object was created from the file “caterpillar.jpg”
and then was sent the message decreaseRed()
• The picture object was implicitly passed to the method
decreaseRed() and can be referred to by this
• The array of pixel objects was returned from sending
getPixels() to the picture object
Pixel[] pixelArray = this.getPixels();
• Some variables were declared for later use in the loop
Pixel pixelObj = null;
int index = 0;
int value = 0;
ManipulatingPictures-part2
23
Step Through decreaseRed() - cont
• The while loop tests if the index is less than the length of the array
while (index < pixelArray.length) {
– And if so it executes the statements in the body of the loop {}
• It sets the variable pixelObj to the pixel at the current index in
the array of pixels
pixelObj = pixelArray[index];
• It gets the red value of that pixel
value = pixelObj.getRed();
• 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
pixelObj.setRed(value);
• It increments the index value
index++;
ManipulatingPictures-part2
24
Memory Map of decreaseRed()
width=329
height=150
• What does memory
this
look like the first time
through the loop?
• How about the 2nd pixelArray 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
getPixels()
…
R=253,
R=254,
G=255,
G=254,
B=254,
B=254,
X=1,
X=2,
Y=0
Y=0
…
Pixel: Class
getRed()
setRed()…
ManipulatingPictures-part2
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
ManipulatingPictures-part2
26