CSE 8A Lecture 6 • Reading for next class: 5.2.1-5.2.2 • PSA 3 is up now: Get started! – Color Subtraction and TriColor effect • Compile and run your code JUST BEFORE you submit. You will lose points (lots) if your code doesn’t run. CLICKERS OUT! 1. What line of code would you use to get a Pixel located at (50,50) from an already defined Picture object named pictObj? A. pictObj.getPixel(int 50, int 50); B. pictObj.getPixel(50,50); C. pictObj.getPixels(); D. pictObj.getPixels[50][50]; READING QUIZ – NO TALKING – CORRECTNESS MATTERS 2. In the following code what update is done at the end of each loop iteration? for (int index = 0; index < 5; index++) { //Some code here } A. The value of index doesn’t change B. The value of index is increased by 1 C. The value of index is increased by 2 D. This code produces an error READING QUIZ – NO TALKING – CORRECTNESS MATTERS 3. Can you have a for-loop in the body of another for-loop? A. Yes you can B. Yes, but you need special code at the top of the file to be able to do it. C. No, that will give you a compiler error. READING QUIZ – NO TALKING – CORRECTNESS MATTERS 4. I have a Picture with a height of 300 pixels and width of 300 pixels. If I want to horizontally mirror the Pixel object located at (100,100) which Pixel object would I have to edit? A. The Pixel object located at (100, 0) B. The Pixel object located at (0, 100) C. The Pixel object located at (100,199) D. The Pixel object located at (199,199) READING QUIZ – NO TALKING – CORRECTNESS MATTERS Exam 1 • Median: 85.0 • High score: 10/10 (103 people scored this!) • If you scored lower than 7.5: – Don’t panic, you’re lowest score is dropped – Get help! – Do the reading, with a partner – Make sure you really REALLY understand everything 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 labs Exam 1 Review (1 pt) Why will the following code cause an error? Assume these statements appear consecutively. Circle the correct response: World world1 = new World(200, 100); Turtle nyla = new Turtle( world1 ); nyla = new Picture(); A. The value assigned to the variable nyla is the wrong type B. The variable nyla is declared more than once C. The constructor for Picture needs an argument (the name of a file) D. Once the variable nyla is assigned a value, it cannot be reassigned to a new value Exam 1 Review Your task is to write a method called drawRect that can be called on an object of type Turtle. The method takes no parameters, and returns nothing. The method draws a rectangle whose shorter sides are 50 steps long and whose longer sides are 100 steps long. (1 pt) In what file must you define this method? A. B. C. D. Turtle.java Turtle.class Turtle File.java Exam 1 Review What is wrong with the following definition for drawRect? public void drawRect() { World world1 = new World(200, 100); Turtle maria = new Turtle(world1); maria.forward(100); maria.turnLeft(); maria.forward(50); maria.turnLeft(); maria.forward(100); maria.turnLeft(); maria.forward(50); maria.turnLeft(); } A. B. C. D. Nothing is wrong You shouldn’t create a World object inside the method You shouldn’t create a Turtle object inside the method Both B and C Exam 1 Review (1 pt) You have a Turtle object stored in a variable named myTurtle. What line of code will call the method drawRect on that Turtle object? A. Turtle.drawRect(); B. new Turtle.drawRect(); C. myTurtle.drawRect(); D. myTurtle.drawRect Exam 1 Review (2 pts) What does the following code print? Write what is printed on the blank line next to the line of code that prints it. Assume this code is executed in order. int num1 = 42; int num2 = 64; num2 = num1; System.out.println( num1 == num2 ); _____________________ num1 = 101; System.out.println( num2 ); __________________________ A. True 101 B. True 42 C. False 101 D. 42 101 E. 42 42 Exam 1 Review World world1 = new World(200,200); Turtle maria = new Turtle(50, 100, world1); Turtle jose = new Turtle(100, 100, world1); jose = maria; maria = new Turtle( 150, 100, world1 ); maria.turn(180); jose.forward(60); maria.forward(100); maria jose 1) Solo: (1 min ) 2) Discuss: (2min) 3) Group: (30 sec) Swap red and blue values of each pixel Pixel[] pixelArray = this.getPixels(); int value = 0; int index = 0; while (index < pixelArray.length) { Pixel pix = pixelArray[index]; <<CODE GOES HERE>> index++; } value = pix.getRed(); A. pix.setBlue(pix.getRed()); pix.setRed(value); value = pix.getRed(); C. pix.setRed(pix.getBlue()); pix.setBlue(value); value = pix.getRed(); B. pix.setBlue(value); pix.setRed(pix.getBlue()); value = pix.getRed(); D. pix.setRed(value); pix.setBlue(pix.getRed()); 1) Solo: (30 sec) 2) Discuss: (2min) 3) Group: (30 sec) Why does this code have an error? Pixel[] pixelArray = this.getPixels(); Pixel p; Pixel q; for(int index = 0; index < pixelArray.length; index++) { p = pixelArray[index]; q = pixelArray[index+1]; p.setRed(q.getRed()); p.setBlue(q.getRed()); p.setGreen(q.getGreen()); } A. B. C. D. E. It tries to access pixelArray[-1] It tries to access pixelArray[0] It tries to access pixelArray[pixelArray.length] It tries to access pixelArray[pixelArray.length+1] None of the above CS Concept: Variable Tracing Pixel[] pixelArray = this.getPixels(); Pixel p; Pixel q; for(int index = 0; index < pixelArray.length; index++) { p = pixelArray[index]; q = pixelArray[index+1]; p.setRed(q.getRed()); p.setBlue(q.getRed()); p.setGreen(q.getGreen()); } pixelArray 0 index 1 2 3 4 5 1) Solo: (30 sec) 2) Discuss: (2min) 3) Group: (30 sec) What picture most accurately describes what this code does ? Pixel[] pixelArray = this.getPixels(); Pixel p; Pixel q; for(int index = 0; index < pixelArray.length-1; index++) { p = pixelArray[index]; q = pixelArray[index+1]; p.setRed(q.getRed()); p.setBlue(q.getBlue()); p.setGreen(q.getGreen()); } A. B. C. D. None of these Fill in the for(……) to loop over pixels bottom right to top left • Like this: etc. next first Pixel[] pixArr = this.getPixels(); for ( { //Some code doing set on pixArr[i] } ) 1) Solo: (30 sec) 2) Discuss: (2min) 3) Group: (30 sec) while vs. for vs. for each • True or False: The following code could be written using a for-loop. Pixel[] pixelArray = this.getPixels(); int index = 0; while ( index < pixelArray.length ) { Pixel pix = pixelArray[index]; pix.setGreen(255); index = index + 1; } A. True B. False while Pixel[] pixelArray = this.getPixels(); int index = 0; while ( index < pixelArray.length ) { Pixel pix = pixelArray[index]; pix.setGreen(255); index = index + 1; } for Pixel[] pixelArray = this.getPixels(); for ( int index = 0; index < pixelArray.length; index++ ) { Pixel pix = pixelArray[index]; pix.setGreen(255); } for each Pixel[] pixelArray = this.getPixels(); for ( Pixel pix: pixelArray ) { pix.setGreen(255); } Which do you prefer? Why? 1) Solo: (30 sec) 2) Discuss: (2min) 3) Group: (30 sec) while vs. for vs. for each • So when to use each? Sometimes it’s a matter of style, sometimes it’s ease of functionality Choose the best loop to use in each of these situations A. For B. For each C. While 1. You want to loop through a picture until you find a pixel that is all black. Then you want your loop to stop. 2. You want to loop through all the pixels in a picture and set their red value equal to their green value 3. You want loop through the pixels in the first half of the picture and make them all black. while vs. for vs. for each: Summary Less chance of error • for each – Use when you know you need to access and modify each pixel directly • for – Use when you need to loop through a known number of pixels and need access to their index value • while Greater chance of error – Use when you are not sure how many pixels to loop through Often, though, you can just choose the loop you like best 1) Solo: (30 sec) 2) Discuss: (2min) 3) Group: (30 sec) Nested Loops: How do they work? What order are pixels changed? • A method in Picture.java… what does it print if width is 2 and height is 3? Pixel p; for (int foo = 0; foo < getWidth(); foo++) { for (int bar = 0; bar < getHeight(); bar++) { System.out.println( foo.toString() +“ “+ bar.toString() ); } turns the variable into a string for printing } A 00 01 10 11 20 21 B. 0 0 10 20 01 11 21 C. 0 0 01 02 10 11 12 D. 0 0 11 22 Nested Loops: Tracing code • A method in Picture.java… what does it print if width is 2 and height is 3? Pixel p; for (int foo = 0; foo < getWidth(); foo++) { for (int bar = 0; bar < getHeight(); bar++) { System.out.println( foo.toString() + bar.toString() ); } turns the variable into a string for printing } foo bar 1) Solo: (30 sec) 2) Discuss: (2min) 3) Group: (30 sec) Nested Loops: How do they work? What order are pixels changed? • A method in Picture.java… Pixel p; for (int foo = 0; foo < getWidth(); foo++) { for (int bar = 0; bar < getHeight(); bar++) { p = getPixel(foo, bar); p.setColor(Color.BLACK); } } What do these Picture methods do? What are their return types? • getPixel(int x, int y) • getHeight() • getWidth() 1) Solo: (30 sec) 2) Discuss: (2min) 3) Group: (30 sec) Nested Loops: How do they work? In what order are pixels changed? • A method in Picture.java… Pixel p; for (int bar = 0; bar < getHeight(); bar++) { for (int foo = 0; foo < getWidth(); foo++) { p = getPixel(foo, bar); p.setColor(Color.BLACK); } } 1) Solo: (30 sec) 2) Discuss: (2min) 3) Group: (30 sec) Why does this have an error? • In a method in Picture.java… assume height=50,width=100 Pixel p; for (int bar = 0; bar < getWidth(); bar++) { for (int foo = 0; foo < getHeight(); foo++) { p = getPixel(foo, bar); p.setColor(Color.BLACK); } } A. It doesn’t, this loops across rows, top to bottom B. It doesn’t, this loops down columns, left to right C. It tries to index a pixel off the end of a row (x value too big) D. It tries to index a pixel off the end of a column (y value too big) TODO • Get started and do PSA3 • Read next class: 5.2.1-5.2.2 • Do the reading quiz • PLAY WITH CODE! – Get stuck, then figure out how to get unstuck – it’s how you learn!