Conditionals-part21 Conditionals – part 2 Barb Ericson Georgia Institute of Technology Nov 2009.
-
Upload
emery-greene -
Category
Documents
-
view
214 -
download
0
Transcript of Conditionals-part21 Conditionals – part 2 Barb Ericson Georgia Institute of Technology Nov 2009.
Conditionals-part2 1
Conditionals – part 2
Barb Ericson
Georgia Institute of Technology
Nov 2009
Conditionals-part2 2
Learning Goals
• Understand at a conceptual and practical level– How to use conditionals with two possibilities– How to do simple edge detection– How to use ‘not’ in a conditional
Conditionals-part2 3
Testing for not true
• We usually use a conditional to do some action when a Boolean expression is true– But, what about if we want to do some action
when a Boolean expression is false?
• We can use an if with the Boolean expression negated using '!'> 20 != 20 // not equal
> ! (30 < 20) // 30 not less than 20
Conditionals-part2 4
A Conditional with 2 Outcomes
• You might want to do one thing if a Boolean expression is true and another if it is false– int x = 30;– if (x < 30) System.out.println("x < 30");– if (x >= 30) System.out.println("x >= 30");
• But, then you have to test 2 Boolean expressions– Instead you can use an if with an else
Conditionals-part2 5
Use if and else for two possibilitiesint x = 30
if (x < 30)
System.out.println(“x<30”);
else
System.out.println(“x>=30”);
statement
if (expression)
true
false
Statement or block
Statement or block
else
Conditionals-part2 6
Edge Detection
• Loop through all the pixels in the picture, except the last row– Calculate the average color
for the current pixel and the pixel at the same x but y+1.
– Get the distance between the two averages
– If the absolute value of the distance is greater than some value turn the current pixel black
– Otherwise turn the current pixel white
Conditionals-part2 7
Edge Detection Algorithm
• To find areas of high contrast – Try to loop from y = 0 to y = height – 1
• Loop from x = 0 to x = width– Get the pixel at the x and y (top pixel)– Get the pixel at the x and (y + 1) bottom pixel– Get the average of the top pixel color values– Get the average of the bottom pixel color values– If the absolute value of the difference between the
averages is over a passed limit» Turn the pixel black» Otherwise (else) turn the pixel white
Conditionals-part2 8
Edge Detection Exercise
• Write a method edgeDetection that takes an input limit – And turns all pixels black where the absolute value of
the difference between that pixel and the below pixel is greater than the passed limit
– And turns all pixels white where the absolute value of the difference between that pixel and the below pixel is less than or equal the passed limit
– Pixel has a getAverage() method that returns the average of the three colors at the pixel
Conditionals-part2 9
Testing Edge Detection
String file = FileChooser.getMediaPath(“butterfly1.jpg”);
Picture p = new Picture(file);
p.explore();
p.edgeDetection(10);
p.explore();
Conditionals-part2 10
Exercise
• walk through a flowchart of a loop with a conditional.
Say your name
Set count to 0
count < 3
Throw the dice
dice < 6
Clap your hands count times
Jump count times
Say, "all done! And the value of count"
count = count + 1
true
false
false
true
Conditionals-part2 11
Challenge
• Create another method for simple edge detection– This time compare the current pixel with the
one to the right (x+1)– How do you need to change the nested loop?– Do you get a different result?
Conditionals-part2 12
Summary
• Use if and else if you have two possibilities to deal with
if (test){ // statements to execute when the test is true}else { // statements to execute when the test is false}
• You can use a ! to negate the result of a Boolean expression