CS 111- Fall 2012

Solutions to Exam 2

  1. B
  2. A
  3. C
  4. D
  5. B
  6. D
  7. A
  8. C
  9. A
  10. B
  11. C
  12. C
  13. A
  14. D
  15. C
  16. A
  17. C
  18. D
  19. B
  20. C
  21. public static Picture q21 (Picture p)
    {
    int wid = p.getWidth();
    int hgt = p.getHeight();

    int x;
    int y;

    for ( x = 0 ; x < wid ; x = x + 1 ) // loop for each column
    {
    for ( y = 0 ; y < hgt ; y = y + 1 ) // loop for each pixel in the column
    {
    Pixel pix = p.getPixel (x, y); // access the pixel

    // access the color valuesat the pixel
    int red = pix.getRed();
    int green = pix.getGreen();
    int blue = pix.getBlue();

    // modify the color values
    int grayAmount = (red + green + blue ) / 3;

    if ( grayAmount < 64 )
    {
    pix.setColor ( Color.MAGENTA );
    }
    else if ( grayAmount < 128 )
    {
    pix.setColor ( Color.RED );
    }
    else if ( grayAmount < 192 )
    {
    pix.setColor ( Color.ORANGE );
    }
    else
    {
    pix.setColor ( Color.YELLOW );
    }
    }
    }

    return p;
    } // end of q21
  22. public static Picture q22 (Picture p)
    {
    int wid = p.getWidth();
    int hgt = p.getHeight();

    Picture p2 = new Picture (wid, hgt * 2);

    int x;
    int y;
    Color c;

    for ( x = 0 ; x < wid ; x = x + 1 ) // loop for each column
    {
    for ( y = 0 ; y < hgt ; y = y + 1 ) // loop for each pixel in the column
    {
    Pixel pix = p.getPixel (x, y); // access the pixel
    c = pix.getColor();

    Pixel pix2 = p2.getPixel (x, y); // access the pixel
    pix2.setColor(c);
    pix2 = p2.getPixel (x, y + hgt); // access the pixel
    pix2.setColor(c);
    }
    }

    return p2;
    } // end of q22
  • Exam2.java: Java program containing the code for both Q21 and Q22
Topic attachments
I Attachment Action Size Date Who Comment
Java source code filejava Exam2.java manage 2.5 K 2012-12-03 - 21:27 UnknownUser Java program containing the code for both Q21 and Q22
Topic revision: r1 - 2012-12-03 - 21:27:45 - 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