# CS 111 - Fall 2014

### Solutions to Multiple Choice Questions for Exam 2

1. b
2. c
3. b
4. d
5. c
6. a
7. c
8. c
9. b
10. a
11. c
12. b
13. a
14. d
15. b
16. d
17. d
18. d
19. c
20. a

Q. 21

`  public static Picture makeBlackOrWhite (Picture p)   {     // access the size of the picture     int width = p.getWidth();     int height = p.getHeight();           for  ( int xIndex = 0 ; xIndex < width ; ++xIndex )      {         // do whatever I need to column number xIndex         //   access each pixel in this column         for ( int yIndex = 0 ; yIndex < height ;  ++yIndex )            {               // access the pixel at coordinate               //     (xIndex, yIndex)               Pixel pix = p.getPixel (xIndex, yIndex);               int r, g, b;                              // access the individual color values from the pixel               r = pix.getRed();               g = pix.getGreen();               b = pix.getBlue();                              // modify the individual color amounts               int grayAmount = (int)(r * 0.299 + g * 0.587 + b * 0.114);                              // save the modified values back into the pixel               if ( grayAmount < 64 )               {                 pix.setColor (Color.BLUE);               }               else if ( grayAmount < 128 )               {                 pix.setColor (Color.GREEN);               }               else if ( grayAmount < 192 )               {                 pix.setColor (Color.CYAN);               }               else               {                 pix.setColor (Color.YELLOW);               }                                       }      }     return p;   }`

Q. 22

`  public static Picture makeCollage (Picture p1, Picture p2)   {      int width1 = p1.getWidth();     int height1 = p1.getHeight();     int width2 = p2.getWidth();     int height2 = p2.getHeight();     int height;     if (height1 > height2)       height = height1;     else       height = height2;          Picture result = new Picture (width1 + width2 , height);      // copy the pixels fro, p1 to the result     for ( int xIndex = 0 ; xIndex < width1 ;  xIndex = xIndex + 1)      {         //   access each pixel in this column         for ( int yIndex = 0 ; yIndex < height1 ;  yIndex = yIndex + 1 )         {               // access the pixel at coordinate               //     (xIndex, yIndex)               Pixel pix1 = p1.getPixel (xIndex, yIndex);                     Color c1 = pix1.getColor();                      Pixel pixR;               int xModified = xIndex;               int yModified = yIndex;               pixR = result.getPixel (xModified, yModified);               pixR.setColor (c1);         } // end of inner for loop       }  // end of outer for loop          // copy the pixels from p2 to the result     for ( int xIndex = 0 ; xIndex < width2 ;  xIndex = xIndex + 1)      {         //   access each pixel in this column         for ( int yIndex = 0 ; yIndex < height2 ;  yIndex = yIndex + 1 )         {               // access the pixel at coordinate               //     (xIndex, yIndex)               Pixel pix2 = p2.getPixel (xIndex, yIndex);                     Color c2 = pix2.getColor();                      Pixel pixR;               int xModified = xIndex + width1;               int yModified = yIndex;               pixR = result.getPixel (xModified, yModified);               pixR.setColor (c2);         } // end of inner for loop       }  // end of outer for loop           return result;   }  // end of method`

-- Main.troy - 2014-12-02

This topic: CS111 > WebHome > CS111Fall2014 > Exam2SolnF14
Topic revision: r1 - 2014-12-02 - 20:32:28 - Main.troy

 Copyright 2016 The Board of Trustees of the University of Illinois.webmaster@cs.uic.edu WISESTHelping Women Faculty AdvanceFunded by NSF