conditionals – part 2

21
Conditionals – part 2 Barb Ericson Georgia Institute of Technology August 2005

Upload: zea

Post on 09-Feb-2016

48 views

Category:

Documents


0 download

DESCRIPTION

Conditionals – part 2. Barb Ericson Georgia Institute of Technology August 2005. 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 ‘and’, ‘or’, ‘exclusive or’ and ‘not’ in a conditional - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Conditionals – part 2

Conditionals – part 2

Barb EricsonGeorgia Institute of Technology

August 2005

Page 2: Conditionals – part 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 ‘and’, ‘or’, ‘exclusive or’ and ‘not’

in a conditional– What is De Morgan’s Law?

Page 3: Conditionals – part 2

Edge Detection• Loop through all the

pixels in the picture– 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

Page 4: Conditionals – part 2

Edge Detection Algorithm

• To find areas of high contrast – Try to loop from row = 0 to row = 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 turn the pixel white

Page 5: Conditionals – part 2

Use if and else for two possibilities• Sometimes you want

to do one thing if the expression is true

• and a different thing if it is falseint x = 200;if (x < 128) System.out.println(“<128”);else

System.out.println(“>=128”); statement

if (expression)

true

false

Statement or block

Statement or block

else

Page 6: Conditionals – part 2

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

Page 7: Conditionals – part 2

Testing Edge Detection

• String file = FileChooser.getMediaPath(“butterfly1.jpg”);

• Picture p = new Picture(file);• p.explore();• p.edgeDetection(10);• p.explore();

Page 8: Conditionals – part 2

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?

Page 9: Conditionals – part 2

How many when there is an “And”?

• I want you to get soup, milk, bread, and yogurt at the store.– How many items will you come home with?

• I want you to clean your room and mop the floor in the kitchen and wash the dishes.– How many tasks do you need to do?

• I want a scoop of chocolate scoop and a scoop of vanilla.– How many scoops of ice cream is this?

Page 10: Conditionals – part 2

How many when there is an “Or”

• You need to help clean the house– You can clean the bathroom or the kitchen or

the living room– How many jobs do you have to do?

• You want to get an ice cream– The flavors you can pick from are chocolate,

vanilla, strawberry, or orange sherbet– How many flavors do you need to pick for a

single scoop?

Page 11: Conditionals – part 2

Truth TableConditional Operand 1 Operand 2 Result

And true true true

And true false false

And false true false

And false false false

Or true true true

Or true false true

Or false true true

Or false false false

Exclusive Or true true false

Exclusive Or true false true

Exclusive Or false true true

Exclusive Or false false false

Page 12: Conditionals – part 2

Conditional Exercise

• When are the following true? When are they false?– You can go out if your room is clean and you

did your homework– You can go out if your room is clean or you

did your homework– You can go out if either your room is clean or

you did your homework but not if both of these is true

Page 13: Conditionals – part 2

Conditional Operators• We can check if several things are true - And

– Using && (evaluation stops if the first item is false)– Using & (to always evaluate both operands)

• We can check if at least one of several things are true - Or– Using || (evaluation stops if the first item is true)– Using | (to always evaluate both operands)

• We can check if only one and only one of the things is true – Exclusive Or– Using ^

Page 14: Conditionals – part 2

Conditional Exercise

• What is the result from the following code?int x = 3;int y = 4;if (x < 4 && y > 5)

System.out.println(“and is true”);else

System.out.println(“and is false”);

Page 15: Conditionals – part 2

Conditional Exercise

• What is the result from the following code?int x = 3;int y = 6;if (x < 4 && y > 5)

System.out.println(“and is true”);else

System.out.println(“and is false”);

Page 16: Conditionals – part 2

Conditional Exercise

• What is the result from the following code?int x = 4;int y = 4;if (x < 4 || y > 5)

System.out.println(“or is true”);else

System.out.println(“or is false”);

Page 17: Conditionals – part 2

Conditional Exercise

• What is the result from the following code?int x = 4;int y = 4;if (x <= 4 || y > 5)

System.out.println(“or is true”);else

System.out.println(“or is false”);

Page 18: Conditionals – part 2

Using && (And) and || (Or)• Check that a value is in a range

– Is some value between 0 and 255 (inclusive)• for valid pixel color values

– 0 <= x <= 255 is written as– 0 <= x && x <= 255 // in Java or– x >= 0 && x <= 255 // is the same

• Check if at least one of several things is true– Is this black or white?– True if either it is black or it is white

Page 19: Conditionals – part 2

Not Conditional Operator

• Use ! To change the value to the opposite– !true = false– !false = true

• A not conditional operator applied to a complex conditional changes it– !(op1 && op2) = !op1 || !op2– !(op1 || op2) = !op1 && !op2

• This is known as De Morgan’s Law

Page 20: Conditionals – part 2

De Morgan’s Law Exercise

• What is equivalent to the following?• !(x > 4 && x < 8)• !(y > 2 || y < 10)• !(x == 2 && y == 4)• !(y != 2 && x != 3)• !(x == 3 || x == 5)• !(y == 2 || y < 5)

Page 21: Conditionals – part 2

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}

• Complex conditionals – Use ‘and’ to test for more than one thing being true– Use ‘or’ to test if at least one thing is true– Use ‘exclusive or’ to test that one and only one thing is true– Use ‘not’ to change the result from true to false and false to true