290 likes | 408 Views
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!.
E N D
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? READING QUIZ – NO TALKING – CORRECTNESS MATTERS A. pictObj.getPixel(int 50, int 50);B. pictObj.getPixel(50,50);C. pictObj.getPixels();D. pictObj.getPixels[50][50];
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 changeB. The value of index is increased by 1C. The value of index is increased by 2D. 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 canB. 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(); The value assigned to the variable nyla is the wrong type The variable nyla is declared more than once The constructor for Picture needs an argument (the name of a file) 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? • 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(); } • 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? • Turtle.drawRect(); • new Turtle.drawRect(); • myTurtle.drawRect(); • 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 ); __________________________ • True 101 • True 42 • False 101 • 42 101 • 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
Solo: (1 min ) • Discuss: (2min) • 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(); pix.setRed(pix.getBlue()); pix.setBlue(value); value = pix.getRed(); pix.setBlue(pix.getRed()); pix.setRed(value); C. A. value = pix.getRed(); pix.setBlue(value); pix.setRed(pix.getBlue()); value = pix.getRed(); pix.setRed(value); pix.setBlue(pix.getRed()); B. D.
Solo: (30 sec) • Discuss: (2min) • Group: (30 sec) Why does this codehave 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()); } • 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 1 2 3 4 5 index
Solo: (30 sec) • Discuss: (2min) • Group: (30 sec) What picture most accuratelydescribes 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. D. None of these B. C.
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] }
Solo: (30 sec) • Discuss: (2min) • 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; } • True • 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?
Solo: (30 sec) • Discuss: (2min) • 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 • For • For each • While • You want to loop through a picture until you find a pixel that is all black. Then you want your loop to stop. • You want to loop through all the pixels in a picture and set their red valueequal to their green value • 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 • 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 • Use when you are not sure how many pixels to loop through Less chance of error Greater chance of error Often, though, you can just choose the loop you like best
Solo: (30 sec) • Discuss: (2min) • 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 0 0 0 1 1 0 1 1 2 0 2 1 B. 0 0 1 0 2 0 0 1 1 1 2 1 C. 0 0 0 1 0 2 1 0 1 1 1 2 D. 0 0 1 1 2 2
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
Solo: (30 sec) • Discuss: (2min) • 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()
Solo: (30 sec) • Discuss: (2min) • 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); } }
Solo: (30 sec) • Discuss: (2min) • 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); } } It doesn’t, this loops across rows, top to bottom It doesn’t, this loops down columns, left to right It tries to index a pixel off the end of a row (x value too big) 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!