TWiki> CS111 Web>CS111Spring2015>SolnExam2s15 (2015-04-30, Main.troy)

CS 111 - Program Design I

Spring 2015: Exam 2 Results and Solutions

Results:

 Score Range Count Count: 101 100: 2 Max: 100 x2 90's: 13 Min: 20 80's: 16 Ave: 65.93 70's: 15 Median: 68.00 60's: 17 St. Dev: 20.69 50's: 15 40's: 10 30's: 7 20's: 6

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. c
19. b
20. c
Question 21.

```public static Picture q21 ( Picture p )
{

int x;
int y;

//  loop for all of the columns in the picture

for (  x = 0 ; x < p.getWidth() ; x++ )
{
// access column x in the picture
for (  y = 0 ; y < p.getHeight() ;  y++ )
{
Pixel pix = p.getPixel(x, y);

// manipulate pix as desired
int r = pix.getRed();
int g = pix.getGreen();
int b = pix.getBlue();

int grayAmount = (int)(r*0.299 + g*0.587 + b*0.114);

if ( grayAmount < 85 )    // 255 / 3 is 85  ==> BLUE
{
pix.setColor (Color.BLUE);
}
else if ( grayAmount < 170 )   // 255 * 2 / 3 is 170  ==> GREEN
{
pix.setColor (Color.GREEN);
}
else                      // ==> CYAN
{
pix.setColor (Color.CYAN);
}
}
}

return p;
}
```

Question 22.

```public static Picture q22 ( Picture p )
{
Picture result;

int width = p.getWidth();
int height = p.getHeight();

result = new Picture ( width * 2, height );
int x;
int y;

//  loop for all of the columns in the picture

for (  x = 0 ; x < width ; x++ )
{
// access column x in the picture
for (  y = 0 ; y < height ;  y++ )
{
Pixel pix = p.getPixel(x, y);
Color c1 = pix.getColor();

// copy the first pixel position
int resultX = x;
int resultY = y;

Pixel resultPix = result.getPixel ( resultX, resultY );
resultPix.setColor ( c1 );

// copy into the second pixel position
resultX = width + width-1 - x;
resultY = y;

resultPix = result.getPixel ( resultX, resultY );
resultPix.setColor ( c1 );
}
}

return result;
}
```

-- Main.troy - 2015-04-24

Topic revision: r2 - 2015-04-30 - 13:58:40 - Main.troy

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