CS 111- Fall 2012
Solutions to Exam 2
- B
- A
- C
- D
- B
- D
- A
- C
- A
- B
- C
- C
- A
- D
- C
- A
- C
- D
- B
- C
- 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
- 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
This topic: CS111
> WebHome >
CS111Fall2012 > Exam2SolnF21
Topic revision: r1 - 2012-12-03 - 21:27:45 - Main.troy