CS 101 - 3/27/12 Exam 2 on Thursday during Lecture Same format as Exam 1 - 20 multiple choice questions - 2 write method question for loops (version of while loops) if statements complex conditions (i.e. boolean operators) - && true if both operands are true - || true if at least one operand is true - ! changes true to false; false to true ( (x < 10) && (y > 3) ) ( (x < 10) || (y > 3) ) ( ! (x < 10) ) if ( ( x > 1 ) && ( x < 10 ) ) { ------------ What is the value of z when this code is finished executing? z = 0; for (x = 1; x <= 2 ; x++ ) { for ( y = 1; y <= 2 ; y++ ) { z = x + y + z ; } } x: 1 2 3 y: 1 2 3 1 2 3 z: 0 2 5 8 12 ------------ What is the value of z when the following code stops executing? x = 1; z = 0; while ( x <= 5) { if ( x > 2) z = z + x; z = z + 1; x = x + 1; } x: 1 2 3 4 5 6 z: 0 1 2 5 6 10 11 16 17 ----------------- Write code: Write a method that will take 1 parameter of type picture and return a picture that is the grayscale version of the picture given in the parameter. public static Picture Q21 (Picture p1) { Picture p2 = new Picture (p1.getWidth(), p1.getHeight() ); int xPos, yPos; for ( xPos = 0 ; xPos < p1.getWidth(); xPos++ ) { for ( yPos = 0 ; yPos < p1.getHeight(); yPos++ ) { Pixel pix1 = p1.getPixel (xPos, yPos); int red = pix1.getRed(); int green= pix1.getGreen(); int blue = pix1.getBlue(); int gray = ( red + green + blue ) / 3; Pixel pix2 = p2.getPixel (xPos, yPos); pix2.setRed( gray ); pix2.setGreen( gray ); pix2.setBlue( gray ); // pix2.setColor ( new Color(gray, gray, gray)); } } return p2; }