CS 101 - Fall 2010

Exam 2 Solutions

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

  21. public static Picture Q21 (Picture p)
     {
      Picture p2 = new Picture (p.getWidth(), p.getHeight());
    
      for (int x = 0 ; x < p.getWidth() ; x++ )
        for (int y = 0 ; y < p.getHeight() ; y++ )
          {
           Pixel pix1 = p.getPixel (x, y);
           Pixel pix2 = p2.getPixel ( p.getWidth() - x - 1, 
                                      p.getHeight() - y - 1);
    
           pix2.setColor( pix1.getColor() );
          }
    
      return p2;
     }
    

  22. public static Picture Q22 (Picture p)
     {
      for (int x = 0 ; x < p.getWidth() ; x++ )
        for (int y = 0 ; y < p.getHeight() ; y++ )
          {
           Pixel pix = p.getPixel (x, y);
      
           int luminance = ( pix.getRed() +
                             pix.getGreen() +
                             pix.getBlue() ) / 3;
    
           if ( luminance < 64 )
              pix.setColor ( Color.black );
           else if ( luminance < 128 )
              pix.setColor ( Color.blue );
           else if ( luminance < 192 )
              pix.setColor ( Color.green );
           else 
              pix.setColor ( Color.cyan );
          }
    
      return p;
     }