Conditionals-part41 Conditionals – part 4 Barb Ericson Georgia Institute of Technology Dec 2009.

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.
Conditionals-part11 Barb Ericson Georgia Institute of Technology Nov 2009.
Games and Simulations O-O Programming in Java The Walker School
Georgia Institute of Technology Two-Dimensional Arrays and Nested Loops – part 6 Barb Ericson Georgia Institute of Technology August 2005.
TOPIC 9 MODIFYING PIXELS IN A MATRIX: COPYING, CROPPING 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach.
NestedLoops-part31 Nested Loops – part 3 Barb Ericson Georgia Institute of Technology Nov 2009.
TOPIC 7 MODIFYING PIXELS IN A MATRIX NESTED FOR LOOPS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by.
NestedLoops-part11 Nested Loops – part 1 Barb Ericson Georgia Institute of Technology Nov 2009.
CSC1401 Viewing a picture as a 2D image - 1. Review from the last week We used a getPixels() to get all of the pixels of a picture But this has been somewhat.
02-RangesInPictures1 Barb Ericson Georgia Institute of Technology Oct 2010 Working with ranges in pictures.
Georgia Institute of Technology Movies part 5 Barb Ericson Georgia Institute of Technology April 2006.
Georgia Institute of Technology Processing Sound Ranges Barb Ericson Georgia Institute of Technology July 2005.
CompSci Arrays  Aggregate data type  Deal with items of same type  Lists of words  Numbers  Analogies  Mailboxes in post office  CD racks.
TOPIC 11 RETURNING VALUES FROM METHODS PICTURE TRANSFORMATIONS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach.
Georgia Institute of Technology Manipulating Pictures, Arrays, and Loops Barb Ericson Georgia Institute of Technology August 2005.
ManipulatingPictures-Mod6-part61 Manipulating Pictures, Arrays, and Loops: Eliminating color, Inversion, grey scale and adjusting for luminance Barb Ericson.
03-ConditionalsInPictures Barb Ericson Georgia Institute of Technology Feb 2010 Working with ranges in pictures.
Georgia Institute of Technology Two-Dimensional Arrays and Nested Loops – part 5 Barb Ericson Georgia Institute of Technology August 2005.
NestedLoops-part41 Nested Loops – part 4 Barb Ericson Georgia Institute of Technology Nov 2009.
NestedLoops-Mody7-part51 Two-Dimensional Arrays and Nested Loops – part 5 Rotations Barb Ericson Georgia Institute of Technology May 2007.
Georgia Institute of Technology More on Creating Classes part 2 Barb Ericson Georgia Institute of Technology Oct 2005.
CSC1401 Using Decisions in Java - 1. Recall from Alice We only wanted to shoot a lightning bolt at a philosopher So, we used the If statement.
Conditionals-part21 Conditionals – part 2 Barb Ericson Georgia Institute of Technology Nov 2009.
TOPIC 10 THE IF STATEMENT 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson,
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Georgia Institute of Technology What is new in Java 5.0 (1.5)? Barb Ericson Georgia Institute of Technology June 2006.
Georgia Institute of Technology Conditionals – part 2 Barb Ericson Georgia Institute of Technology August 2005.
NestedLoops-part21 Nested Loops – part 2 Barb Ericson Georgia Institute of Technology Nov 2009.
Conditionals-Mod8-part21 Conditionals – part 2 Edge Detection Barb Ericson Georgia Institute of Technology May 2007.
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.
TOPIC 8 MORE ON WHILE LOOPS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson,
Georgia Institute of Technology Two-Dimensional Arrays and Nested Loops – part 2 Barb Ericson Georgia Institute of Technology August 2005.
NestedLoops-Mod7-part61 Two-Dimensional Arrays and Nested Loops – part 6 Enlarge Barb Ericson Georgia Institute of Technology August 2005.
Georgia Institute of Technology Two-Dimensional Arrays and Nested Loops – part 4 Barb Ericson Georgia Institute of Technology August 2005.
Topic 9 Modifying Pixels in a Matrix: Copying, Cropping
Manipulating Pictures, Arrays, and Loops part 2
Manipulating Pictures, Arrays, and Loops part 2
Georgia Institute of Technology
Section 7.1 Logical Operators
Barb Ericson Georgia Institute of Technology Dec 2009
Topic 10 The if statement Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson,
Barb Ericson Georgia Institute of Technology August 2005
Manipulating Pictures, Arrays, and Loops part 2
Manipulating Pictures, Arrays, and Loops part 2
Barb Ericson Georgia Institute of Technology August 2005
Two-Dimensional Arrays and Nested Loops – part 1
Barb Ericson Georgia Institute of Technology August 2005
Two-Dimensional Arrays and Nested Loops – part 1
Georgia Institute of Technology
Workshop for Programming And Systems Management Teachers
Barb Ericson Georgia Institute of Technology June 2006
Chapter 6 Control Statements: Part 2
CSE 113 A February , 2009.
Two-Dimensional Arrays and Nested Loops – part 2
Manipulating Pictures, Arrays, and Loops
Manipulating Pictures, Arrays, and Loops
Two-Dimensional Arrays and Nested Loops – part 6
Georgia Institute of Technology
Manipulating Pictures, Arrays, and Loops
February , 2009 CSE 113 B.
Manipulating Pictures, Arrays, and Loops part 6
CSC1401 Viewing a picture as a 2D image - 2
Two-Dimensional Arrays and Nested Loops – part 6
Barb Ericson Georgia Institute of Technology May 2006
Manipulating Pictures, Arrays, and Loops
Processing Sound Ranges part 3
Manipulating Pictures, Arrays, and Loops part 6
Presentation transcript:

Conditionals-part41 Conditionals – part 4 Barb Ericson Georgia Institute of Technology Dec 2009

Conditionals-part42 Learning Goals Understand at a conceptual and practical level –How to use logical 'and' and 'or' to combine Boolean expressions –What is exclusive or –What is short circuit evaluation –How to use 'or' to test for more than one condition being true –How to use 'and' to stop going out of bounds when working with pictures

Conditionals-part43 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?

Conditionals-part44 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?

Conditionals-part45 Truth Table ConditionalOperand 1Operand 2Result Andtrue Andtruefalse Andfalsetruefalse Andfalse Ortrue Ortruefalsetrue Orfalsetrue Orfalse Exclusive Ortrue false Exclusive Ortruefalsetrue Exclusive Orfalsetrue Exclusive Orfalse

Conditionals-part46 Conditional Operators We can check if several things are true - And –Using && (evaluation stops if the first item is false) Called short circuit evaluation –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 ^

Conditionals-part47 Better Posterize Method In our current posterize method we checked for a range and set the color to the middle of that range –if (redValue < 60) But what if we want more or less ranges? –We would have to add or remove some of the conditionals What if we calculate the endpoints of the ranges? –If the value is between a bottomValue and topValue set it to the middleValue

Conditionals-part48 Better Posterize Method public void posterize(int numLevels) { Pixel pixel = null; int redValue = 0; int greenValue = 0; int blueValue = 0; int increment = (int) (256.0 / numLevels); int bottomValue, topValue, middleValue = 0; // loop through the pixels for (int x = 0; x < this.getWidth(); x++) { for (int y = 0; y < this.getHeight(); y++) { // get the current pixel and colors pixel = this.getPixel(x,y); redValue = pixel.getRed(); greenValue = pixel.getGreen(); blueValue = pixel.getBlue();

Conditionals-part49 Better Posterize Method - cont // loop through the number of levels for (int i = 0; i < numLevels; i++) { // compute the bottom, top, and middle values bottomValue = i * increment; topValue = (i + 1) * increment; middleValue = (int) ((bottomValue + topValue - 1) / 2.0); /* check if current values are in current range and * if so set them to the middle value */ if (bottomValue <= redValue && redValue < topValue) pixel.setRed(middleValue); if (bottomValue <= greenValue && greenValue < topValue) pixel.setGreen(middleValue); if (bottomValue <= blueValue && blueValue < topValue) pixel.setBlue(middleValue); }

Conditionals-part410 Challenge What are the values for bottomValue, topValue and middleValue when the numLevels is 2? What are the values when the numLevels is 4? Try the new posterize method out on a picture with 2 levels and with 4 levels. –What happens to the picture when you increase the number of levels?

Conditionals-part411 Overloading We now have two posterize methods –One that takes no parameters –And one that takes the number of levels as an integer value It is okay to have more than one method with the same name –As long as the parameter list is different Number of parameters Types of parameters Order of the types

Conditionals-part412 Highlight Extremes Challenge Radiologists often miss things in x-rays that are too bright or too dark. Let's highlight all pixels in a picture that are close to black or white –Using a logical 'or' (||) –Using pixelObj.colorDistance

Conditionals-part413 Using and to stop going out of bounds Did you get an java.lang.ArrayIndexOutOfBoundsException? –This means you tried to access an element of an array that was past the bounds of the array: like past the width or height of the picture You probably tried to work with two different size pictures –But only checked that the sourceX or targetX was in the range of one of the pictures –Modify the code to use && to check that the value is within the range of both pictures sourceX < this.getWidth() && sourceX < pict2.getWidth() sourceY < this.getHeight() && sourceY < pict2.getHeight()

Conditionals-part414 Example Out of Bounds Error > String fileName = FileChooser.getMediaPath("caterpillar.jpg"); > Picture p = new Picture(fileName); > System.out.println(p.getWidth()); 329 > System.out.println(p.getHeight()); 150 > p.getPixel(330,160); // 330 is past the width of the picture and 160 is past the height of the picture java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds! at sun.awt.image.ByteInterleavedRaster.getDataElements (Unknown Source) at java.awt.image.BufferedImage.getRGB(Unknown Source) at SimplePicture.getBasicPixel(SimplePicture.java:247) at Pixel.setValuesFromPictureAndLocation(Pixel.java:137) at Pixel. (Pixel.java:57) at SimplePicture.getPixel(SimplePicture.java:270) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke (Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source)

Conditionals-part415 Challenge Modify the general copy method in the last chapter to work even if where you are trying to copy the source picture past the width or height of the target picture –Fix the for loop to stop when you reach the width or height of either picture

Conditionals-part416 Summary 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