Creative Commons Attribution Non-Commercial Share Alike License Original Developer: Beth Simon, 2009 bsimon@cs.ucsd.edu
THIS LECTURE GOT WRITTEN OVER! Re develop from older ones – there were some new slides in Fa 2009, so get those from UP.
CSE8A Lecture 15 Read next class: read pg 197-202 Quizzes pick up
About group quizzes and learning Group quizzes are great, I really learn a lot Group quizzes are good, it helps me solidify my learning to discuss the questions with others Group quizzes are OK, I’m not sure how much I learn by doing them Group quizzes are not helpful to my learning at all.
By the end of today’s class you should be able to… LG30: Write and read code that either loops over a subset of pixels or loops over all pixels and controls changes to pixels with and if statement LG31: Use parameters to control looping in conditional setting of pixels LG32: Identify the flow of control in the three types of Java if statements: if, if-else, and if-else if-else LG34: Compare and contrast two solutions to a problem using for loops and if statements
Changing pixels all over, conditionally //Inside loop over all pixels topP = this.getPixel(x,y); botP = this.getPixel(x,y+1); topAvg = topP.getAverage(); botAvg = botP.getAverage(); if (Math.abs(topAv – botAv) < 10) topP.setColor(Color.WHITE); else topP.setColor(Color.BLACK); What is it doing? Comparing 2 pixels side by side and, if they are similar make the pixel white, otherwise black Comparing 2 pixels one on top of the other and, if they are similar make the pixel white, otherwise black Comparing 2 pixels side by side and, if they are different make the pixel white, otherwise black Comparing 2 pixels one on top of the other and, if they are different make the pixel white, otherwise black
What’s the result of running that code on this input?
A DIFFERENT edgeDetect //Inside loop over all pixels topP = this.getPixel(x,y); botP = this.getPixel(x+1,y); // was (x,y+1) topAvg = topP.getAverage(); botAvg = botP.getAverage(); if (Math.abs(topAv – botAv) < 10) topP.setColor(Color.WHITE); else topP.setColor(Color.BLACK);
A DIFFERENT edgeDetect //Inside loop over all pixels topP = this.getPixel(x,y); botP = this.getPixel(x+1,y); // was (x,y+1) topAvg = topP.getAverage(); botAvg = botP.getAverage(); if (Math.abs(topAv – botAv) < 10) topP.setColor(Color.WHITE); else topP.setColor(Color.BLACK);
Which is most true about ONE execution of this code (for a specific diffValue) int diffValue = Math.abs(topAv – botAv); if (diffValue < 10) topP.setColor(Color.WHITE); else topP.setColor(Color.BLACK); Section A AND Section B may BOTH be executed If Section B is executed then Section A is not executed Neither Section is ever executed It is possible neither Section will be executed (but sometimes one might be).
Which is most true about ONE execution of this code (for a specific diffValue) int diffValue = Math.abs(topAv – botAv); if (diffValue < 10) topP.setColor(Color.WHITE); else if (diffValue < 50) topP.setColor(Color.GREY); else topP.setColor(Color.BLACK); Section A can be executed AND Section B may BOTH be executed but then C can’t be executed If Section A is executed then neither Section B nor C can be All sections can be executed for a single diffValue It’s possible no section is executed for a given diffValue Confused? See page 187 for execution flow diagram
In Lab 5: Which best describes the conditions under which we change pixel color? public void makeConvict() { for (int x = 0; x < this.getWidth(); x++) for (int y = 0; y < this.getHeight(); y++) Pixel currentPix = this.getPixel(x,y); if ( (currentPix.getGreen() > 200) && (y%2==0)) currentPix.setColor(Color.BLACK); } else if( (currentPix.getGreen() > 200) && y%2 == 1) currentPix.setColor(Color.WHITE); Based on the coordinates of the Pixel Based on the color of the Pixel Based on the coordinates for some Pixels, the color for other Pixels Based on a compound condition of color and coordinates of the Pixel