CS 111 - Program Design I

Exam 2 Solutions

  1. A
  2. B
  3. B
  4. D
  5. C
  6. D
  7. C
  8. A
  9. B
  10. A
  11. C
  12. B
  13. A
  14. D
  15. A
  16. D
  17. B
  18. A
  19. B
  20. C

  21. public static Picture q21 ( Picture p )
    {
    int width = p.getWidth();
    int height = p.getHeight();

    int Xindex;
    int Yindex;

    for ( Xindex = 0 ; Xindex < width ; Xindex = Xindex + 1 )
    {
    for ( Yindex = 0 ; Yindex < height ; Yindex = Yindex + 1 )
    {
    Pixel pix = p.getPixel(Xindex, Yindex);

    int red = pix.getRed();
    int green = pix.getGreen();
    int blue = pix.getBlue();

    int brightness = (int)(red*0.299 + green*0.587 + blue*0.114) ;

    if ( brightness < 51 )
    {
    pix.setColor (Color.MAGENTA);
    }
    else if ( brightness < 102 )
    {
    pix.setColor (Color.RED);
    }
    else if ( brightness < 153 )
    {
    pix.setColor (Color.ORANGE);
    } else if ( brightness < 204 )
    {
    pix.setColor (Color.YELLOW);
    }
    else
    {
    pix.setColor (Color.PINK);
    }
    }
    }
    return p;
    }

  22. public static Picture q22( Picture p )
    {
    int width = p.getWidth();
    int height = p.getHeight();
    Picture pic2 = new Picture (width, height*3);

    int Xindex;
    int Yindex;

    for ( Xindex = 0 ; Xindex < width ; Xindex = Xindex + 1 )
    {
    for ( Yindex = 0 ; Yindex < height ; Yindex = Yindex + 1 )
    {
    // access my original/from pixel
    Pixel pix1 = p.getPixel(Xindex, Yindex);
    Color c1 = pix1.getColor();

    // access my to pixel
    int modifiedX = Xindex;
    int modifiedY = Yindex;
    Pixel pix4 = pic2.getPixel(modifiedX, modifiedY);
    pix4.setColor (c1);

    // access my to pixel
    modifiedX = Xindex;
    modifiedY = Yindex + (1 * height);
    pix4 = pic2.getPixel(modifiedX, modifiedY);
    pix4.setColor (c1);

    // access my to pixel
    modifiedX = Xindex;
    modifiedY = Yindex + (2 * height);
    pix4 = pic2.getPixel(modifiedX, modifiedY);
    pix4.setColor (c1);
    }
    }

    return pic2;
    }
-- Main.troy - 2014-04-22


This topic: CS111 > WebHome > CS111Spring2014 > Exam2SoultionsS14
Topic revision: r1 - 2014-04-22 - 18:49:30 - Main.troy
 
Copyright 2016 The Board of Trustees
of the University of Illinois.webmaster@cs.uic.edu
WISEST
Helping Women Faculty Advance
Funded by NSF