Presentation is loading. Please wait.

Presentation is loading. Please wait.

Creative Commons Attribution Non-Commercial Share Alike License sa/3.0/http://creativecommons.org/licenses/by-nc-

Similar presentations


Presentation on theme: "Creative Commons Attribution Non-Commercial Share Alike License sa/3.0/http://creativecommons.org/licenses/by-nc-"— Presentation transcript:

1 Creative Commons Attribution Non-Commercial Share Alike License http://creativecommons.org/licenses/by-nc- sa/3.0/http://creativecommons.org/licenses/by-nc- sa/3.0/ Original Developer: Beth Simon, 2009 bsimon@cs.ucsd.edu

2 CSE8A Lecture 8 Read next class: read pg 134-142 Finish PSA2! Get an interview before Thurs! Take advantage of resources! –Tutors – talk to them about book, lecture, being a major, etc –TAs: Go to office hours –Come to my office hours! –Form study groups!

3 You may not be *really* understanding… Suggestions: –PLAY with code in the compiler. Ask yourself – what will happen if I change this variable value from 5 to 10? Why? Type in code from book and change it. But don’t just accept that you can type it in Do you know WHY greyScale works? Do you know WHY negative works? –Read the book WITH someone else in class. Talk after every page. Try to explain the text to the other person in your own words Don’t “just read” it and think, yeah I get that (you don’t) –Redo the clicker questions WITH someone else –Redo labs

4 By the end of today’s class you should be able to… LG16: REVIEW: Find bugs in code including: a common “empty loop” bug, using the “how many times does this statement get executed” analysis technique. LG17: Argue about the best location of a print statement to debug a problem with a loop. LG18: Apply knowledge of how people pass racketballs to swap values of variables LG20: Trace the execution of code through multiple method calls. LG19: Read and trace execution of a code with a for loop (using a single array of pixels).

5 REVIEW:DEBUGGING: How many times is each set of code executed? Pixel[] pixelArray = this.getPixels(); int value = 0; Pixel p = null; int index = 0; while (index < pixelArray.length); { value = pixelArray[index].getRed(); value = (int) (value * 0.5); pixelArray[index].setRed(value); index = index + 1; }

6 DEBUGGING: Where is the best place to put a print statement? Pixel[] pixelArray = this.getPixels(); int value = 0; Pixel p = null; int index = 0; while (index < pixelArray.length) { value = pixelArray[index].getRed(); value = (int) (value * 0.5); pixelArray[index].setRed(value); index = index + 1; }

7 DEBUGGING: This code should swap the red and blue components at each Pixel, what does it ACTUALLY do? A.It has a compiler error B.It sets the red value to be the same as blue C.It sets the blue value to be the same as red D.It really does swap Pixel[] pixelArray = this.getPixels(); int value = 0; int index = 0; while (index < pixelArray.length) { Pixel pix = pixelArray[index]; value = pix.getRed(); value = pix.getBlue(); pix.setRed(value); pixelArray[index].setBlue(value); index++; }

8 Building on What you Already Know: Swapping Values

9 Swapping: A better way Pixel[] pixelArray = this.getPixels(); int value = 0; int index = 0; while (index < pixelArray.length) { Pixel pix = pixelArray[index]; > index++; } value = pix.getRed(); pix.setRed(pix.getBlue()); pix.setBlue(value); value = pix.getRed(); pix.setBlue(pix.getRed()); pix.setRed(value); value = pix.getRed(); pix.setRed(value); pix.setBlue(pix.getRed()); value = pix.getRed(); pix.setBlue(value); pix.setRed(pix.getBlue()); Reminder: Handout code

10 Code Order of Execution There’s a lot of code in different places –Picture.java, Turtle.java –Lab2.java –PSA2A.java, PSA2B.java How does Java know which code to execute, when? –In what order are code statements executed? –See Handout.

11 What order are statements executed in*? A.A,B,C,D,E,F B.A,B,E,F,C,D C.A,E,F,C,D,B D.A,E,C,F,D,B E.None of the above


Download ppt "Creative Commons Attribution Non-Commercial Share Alike License sa/3.0/http://creativecommons.org/licenses/by-nc-"

Similar presentations


Ads by Google